<?xml version="1.0" encoding="UTF-8"?>
<h:screen xmlns:h="http://www.hand-china.com/hap">
    <style>
        #gridBox {
            position: relative;
        }
    </style>
    <link href="${base.contextPath}/resources/css/WECHAT/weChat.css" rel="stylesheet" type="text/css"/>
    <script src="${base.contextPath}/common/code?wechatType=WECHAT.TYPE" type="text/javascript"></script>
    <script id="script"><![CDATA[

    //==========grid 基本函数================================================================================================

    var contextPath = "${base.contextPath}";
    var currentRole;
    $.ajax({
        url: '${base.contextPath}/get/roleCode/by/roleId',
        data: {roleId: ${Session.roleId}},
        async : false,
        success: function (json) {
            if (json.success) {
                for(var i=0;i<json.total;i++){
                    if(json.rows[i].roleId == ${Session.roleId}){
                        currentRole = json.rows[i].roleCode;
                        break;
                    }
                }
            }
        }
    });

    var viewModel = kendo.observable({
        model: {}
    });
    function accountEditor(container, options) {
        $('<input required name="' + options.field + '"/>')
            .appendTo(container)
            .kendoLov($.extend(${lovProvider.getLov(base.contextPath, base.locale, "WECHAT_APP_ACCOUNT")}, {
                textField: 'accountName',
                model: options.model,
                select: function (e) {
                    debugger
                    options.model.set("accountType",e.item.accountType)
                }
            }));
    }
    function accoutTemp(dataItem) {
        return dataItem.accountName || '';
    }
    function gridLocate(e) {
        var d = this.dataItem(e.element);
    }
    function isEditable(field) {
        if(currentRole=='QUERY_ADMIN'){
            return false;
        }else {
            if (field === "accountType") {
                return false;
            }
            return true;
        }
    }
    function parameterMap(options, type) {
        if (type !== "read" && options.models) {
            var datas = options.models
            if (type == 'create' || type == 'update') {
                datas = options.models.map(function (data) {
                    data['__status'] = (type == 'create' ? 'add' : 'update');
                    return data;
                })
            }
            return kendo.stringify(datas);
        } else if (type === "read") {
            var map = {};
            map.page = options.page;
            map.pagesize = options.pageSize;
            for (var k in map) {
                if (map[k] === '' || map[k] === null || map[k] === undefined)
                    delete map[k]
            }
            return map;
        }
    }

    //==========template or editor 函数======================================================================================

    //渲染操作按钮
    function operationFn(dataItem) {
        var str = '';
        if (!!dataItem.id) {
            str = '<div onclick="menuSet(' + dataItem.id + ',\'' + dataItem.name + '\')" class="btn-style color-white backgroundColor-blue right-10">定义菜单</div>'
                + '<div class="btn-style color-white backgroundColor-blue" onclick="openResponseMsg(' + dataItem.id + ')">定义欢迎语</div>';
        } else {
            str = '<div class="btn-style color-white backgroundColor-grey right-10">定义菜单</div><div class="btn-style color-white backgroundColor-grey">定义欢迎语</div>';
        }
        return str;
    }

    //====ajax 函数==========================================================================================================

    //推送请求
    function push(accountId) {
        $.ajax({
            url: '${base.contextPath}/wechat/menu/push?accountId=' + accountId,
            dataType: 'json',
            success: function (datas) {
            }
        });
    }

    //=========弹出框，页面跳转===============================================================================================

    function menuSet(id, accountName) {
        debugger;
        if(currentRole=='QUERY_ADMIN') return false;
        Hls.openBox({
            url: "${base.contextPath}/weChat/userDefinedMenu.view?wechatId=" + id + "&title=" + accountName,
            winId: "menu-div",
            width: "945px"
        });
    }
    function sendNotice(id, name) {
        if(currentRole=='QUERY_ADMIN') return false;
        Hls.openBox({
            url: "${base.contextPath}/weChat/weChatPushNoticce.view",
            winId: "menu-div",
            width: "960px"
        });
    }
    function openResponseMsg(wechatAppId) {
        if(currentRole=='QUERY_ADMIN') return false;
        Hls.openBox({
            url: "${base.contextPath}/weChat/wechat_response_message.view?wechatAppId=" + wechatAppId,
            winId: "menu-div",
            width: "550px"
        })
    }
    function typeTemp(dataItem) {
        var result;
        wechatType.forEach(function (value, index, array) {
            if (dataItem.accountType == value.value) {
                result = value.meaning;
            }
        })
        return result || '';
    }

    function typeEditor(container, options) {
        $('<input name="' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({
                dataTextField: "meaning",
                dataValueField: "value",
                valuePrimitive: true,
                dataSource: wechatType
            });
    }

    function reqEnd(){
        if(currentRole=='QUERY_ADMIN'){
            gridDataSource.transport.options.destroy="";
            gridDataSource.transport.options.create="";
            $(".hlsgridbox-btn-detail")[0].innerHTML="";
        }
    }

    ]]></script>
    <div id="realseDetail"></div>
    <div id="menu-div"></div>
    <h:dataSource id="gridDataSource" batch="true" pageSize="10" requestEnd="reqEnd" serverPaging="true">
        <h:transport parameterMap="parameterMap">
            <h:read url="${base.contextPath}/wechat/enterprise/app/queryAccount" type="GET" dataType="json"/>
            <h:create url="${base.contextPath}/wechat/enterprise/app/submit" type="POST"
                      contentType="application/json"/>
            <h:update url="${base.contextPath}/wechat/enterprise/app/submit" type="POST"
                      contentType="application/json"/>
            <h:destroy url="${base.contextPath}/wechat/enterprise/app/remove" type="POST"
                       contentType="application/json"/>
        </h:transport>
        <h:schema data="rows" total="total">
            <h:model id="id" editable="isEditable">
                <h:fields>
                    <h:item name="name">
                        <h:validation required="true"/>
                    </h:item>
                    <h:item name="wxCode">
                        <h:validation required="true"/>
                    </h:item>
                </h:fields>
            </h:model>
        </h:schema>
    </h:dataSource>
    <h:hlsGridBox hlsGridId="grid" hlsBtnType="add,save"
                      viewModelName="viewModel.model" dataSourceId="gridDataSource" id="gridBox">
            <h:hlsGrid id="grid" dataSource="gridDataSource" autoResize="true" hlsBtnType="DELETE"
                       editable="inline" resizable="true" navigatable="true"
                       locate="gridLocate">
                <h:pageable pageSizes="5,10,20,50" buttonCount="3" refresh="true">
                </h:pageable>
                <h:columns>
                    <h:column title='操作'  width="75">
                        <h:command>
                            <h:item name="edit"/>
                            <h:item name="destroy"/>
                        </h:command>
                        <h:headerAttributes style="text-align:center"/>
                        <h:attributes style="text-align:center"/>
                    </h:column>
                    <h:column field="name" title='<@spring.message "名称"/>' width="90">
                        <h:headerAttributes style="text-align:center"/>
                        <h:attributes style="text-align:left"/>
                    </h:column>
                    <h:column field="accountId" title='<@spring.message "微信账户"/>' width="130" editor="accountEditor"
                              template="accoutTemp">
                        <h:headerAttributes style="text-align:center"/>
                        <h:attributes style="text-align:left"/>
                    </h:column>
                    <h:column field="wxCode" title='<@spring.message "代码"/>' width="90">
                        <h:headerAttributes style="text-align:center"/>
                        <h:attributes style="text-align:left"/>
                    </h:column>
                    <h:column field="accountType" title="账号种类"
                              width="90" editor="typeEditor" template="typeTemp">
                        <h:headerAttributes style="text-align:center"/>
                        <h:attributes style="text-align:left"/>
                    </h:column>
                    <h:column field="corpId" title='<@spring.message "appId/corpId"/>' width="170">
                        <h:headerAttributes style="text-align:center"/>
                        <h:attributes style="text-align:left"/>
                    </h:column>
                    <h:column field="agentId" title='<@spring.message "AGENTID"/>' width="110">
                        <h:headerAttributes style="text-align:center"/>
                        <h:attributes style="text-align:left"/>
                    </h:column>
                    <h:column field="secret" title="<@spring.message 'secret'/>" width="320">
                        <h:headerAttributes style="text-align:center"/>
                        <h:attributes style="text-align:left"/>
                    </h:column>
                    <h:column field="wechatId" title="<@spring.message '微信ID'/>" width="320">
                        <h:headerAttributes style="text-align:center"/>
                        <h:attributes style="text-align:left"/>
                    </h:column>
                    <h:column field="" title="<@spring.message '操作'/>" width="180" template="fn:operationFn">
                        <h:headerAttributes style="text-align:center"/>
                        <h:attributes style="text-align:center"/>
                    </h:column>
                </h:columns>
            </h:hlsGrid>
        </h:hlsGridBox>
    <img src="${base.contextPath}/resources/images/WECHAT/interactive.png" id="sendMessage"
         style="position: absolute;height: 27px;width: 27px;right: 9%;top: 4.5%;cursor: pointer;"
         onclick="sendNotice()"/>
    <script><![CDATA[
    parent.window.frames["iframe_WECHAT.APPLICATION"].style.height = "480px";
    ]]></script>
</h:screen>