<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ include file="/jsp/common/include/localemessage.jsp" %>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setHeader("Expires", "0");

%>
<html>
<head>
   <title></title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <jsp:include page="/jsp/common/include/css.jsp"/>
   <jsp:include page="/jsp/common/include/script.jsp"/>
   <script language="javascript" >

      var url = '<c:url value="/common/acl/role/roleMan.json" />';

      var roleId = window.dialogArguments["roleId"];
      var treeColNames = [];
      var treeColModel = [];

      function isValid() {
         return true;
		}

      function unformatterFunction(cellvalue, options, rowObject) {
         return $('input:checkbox', rowObject).is(':checked') ? "true" : "false";
      }

      function init() {

         return $.ajax({
            url : url,
            type : "POST",
            data : {
               cmd : "LIST_INIT_POPUP"
            },
            dataType : "json",
            success : function(response) {

               treeColNames = [
                  'id',
                  'MENUID',
                  'PRIORMENUID',
                  'SORTORDER',
                  'APPCODE',
                  'renderTarget',
                  'MENUNAME'
               ];

               treeColModel = [
                  { name : 'id'             , hidden:true, key:true },
                  { name : 'MENUID'         , hidden:true  },
                  { name : 'PRIORMENUID'    , hidden:true  },
                  { name : 'SORTORDER'      , hidden:true  },
                  { name : 'APPCODE'      , hidden:true  },
                  { name : 'renderTarget' , hidden:true  },
                  { name : 'MENUNAME'       , align:'left', width:300 }
               ];

               response.columnList.forEach(function(item) {
                  treeColNames.push(item);
                  treeColModel.push({name : item, align:'center', width:80, unformat: unformatterFunction, formatter: authFormatter});
               });

            },
            error : function(e) {
               alert(e.responseText);
            }

         });
		}

      function authFormatter(cellvalue, options, rowObject) {

         var rowId = options["rowId"];
         var appCode = rowObject["APPCODE"];
         var rowServiceType = options.colModel.name;
         var renderTarget = rowObject["renderTarget"];

         for(let i = 0; i < renderTarget.length; i++) {

            if(renderTarget[i] == rowServiceType) {

               var $radio = $(document.createElement('input')).attr('type', 'radio').attr('name', 'radio_'+rowId+'_'+rowServiceType).attr('style', 'margin-left:2px; margin-right:2px;');

               var $radio_N = $radio.clone().attr('value', 'N');
               var $radio_R = $radio.clone().attr('value', 'R');
               var $radio_W = $radio.clone().attr('value', 'W');

               var authInfo = rowObject.serviceType.find(function(item) { return item.serviceType == rowServiceType });

               if(authInfo == undefined) {

                  $radio_N.attr('checked', 'checked');

               } else {

                  if(authInfo.auth == 'R') {
                     $radio_R.attr('checked', 'checked');
                  } else if(authInfo.auth == 'W') {
                     $radio_W.attr('checked', 'checked');
                  }
               }

               return    $radio_N[0].outerHTML + "없음" +
                     $radio_R[0].outerHTML + "R" +
                     $radio_W[0].outerHTML + "W";

            }

			}

         return "";
      }

      function treeGridRender() {

         $('#tree').jqGrid({
            datatype:"json",
            mtype: 'POST',
            url: url,
            postData : {
               cmd : 'LIST_TREE', roleId : roleId
            },
            colNames : treeColNames,
            colModel : treeColModel,
            treeGrid: true,
            treeIcons : {
               plus: "ui-icon-circlesmall-plus",
               minus: "ui-icon-circlesmall-minus",
               leaf : "ui-icon-document"
            },
            cmTemplate: {
               sortable: false,
            },
            treeGridModel: "adjacency",
            ExpandColumn: "MENUNAME",
            height:"650",
            width:"1200",
            rowNum: 10000,
            treeIcons: { leaf:'ui-icon-document-b' },
            jsonReader: {
               repeatitems: false
            },
            loadComplete:function(d) {
            },
            loadError: function(jqXHR, textStatus, errorThrown) {

               var location ='<%=request.getContextPath()%>/';
               comloadError(jqXHR, textStatus, errorThrown ,location);

            },
            ondblClickRow: function(rowId) {
            },
            onSelectRow: function(rowId) {
            }

         });
      }

      $(document).ready(function() {

         $.when(init()).done(function(a1, a2, a3, a4) {
            treeGridRender();
         });

         $("#btn_modify").click(function() {

            if(!isValid()) {
               return;
            }

            var formArr = $("#ajaxForm").serializeArray();

            var postData = new Array();

            postData.push({ name: "cmd"     , value:"TRANSACTION_ROLE_MENU" });
            postData.push({ name: "roleId"  , value:roleId });
            postData.push({ name: "gridData", value:JSON.stringify(formArr) });

            $.ajax({
               type : "POST",
               url:url,
               data:postData,
               success:function(args){
                  alert("<%= localeMessage.getString("common.saveMsg") %>");
                  $("#btn_close").trigger("click");
               },
               error:function(e){
                  alert(e.responseText);
               }
            });

         });

         $("#btn_close").click(function(){
            window.close();
         });

         buttonControl();

		});
   </script>
</head>
<body>
<div class="popup_box">
   <div class="search_wrap">
      <button type="button" class="cssbtn" id="btn_modify" level="W"><i class="material-icons">save</i> <%= localeMessage.getString("button.modify") %></button>
      <button type="button" class="cssbtn" id="btn_close" level="R"><i class="material-icons">close</i> <%= localeMessage.getString("button.close") %></button>
   </div>
   <div class="title"><%= localeMessage.getString("rolePop.title") %></div>
   <form id="ajaxForm">
      <table id="tree"></table>
   </form>
</div><!-- end.popup_box -->
</body>
</html>

