<?xml version="1.0" encoding="UTF-8"?>
<h:screen xmlns:h="http://www.hand-china.com/hap">
    <style>
        .k-textbox {
            width: 150px !important;
        }
    </style>
    <script id="script"><![CDATA[
    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 appNames = [];
    //修改后的content
    var changedContent = '';
    $.ajax({
        url: "${base.contextPath}/wechat/enterprise/app/query",
        dataType: "json",
        async: false,
        type: "GET",
        success: function (datas) {
            appNames = datas.rows
        }
    });
    //显示应用名称
    function appTemp(dataItem) {
        var appName = '';
        //使用 try catch 使foreach跳出
        try {
            appNames.forEach(function (value, index, array) {
                if (value.id == dataItem.appId) {
                    appName = value.name;
                    foreach.break = new Error("StopIteration");
                }
            })
        } catch (e) {

        }
        return appName;
    }

    function schemaError() {

    }
    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;
            map.templateName = $("#name").val();
            map.templateCode = $("#code").val();
            for (var k in map) {
                if (map[k] === '' || map[k] === null || map[k] === undefined)
                    delete map[k]
            }
            return map;
        }
    }
    function gridLocate(e) {
        var d = this.dataItem(e.element);
    }
    function requestError() {

    }

    function deseditable(e) {
        if(currentRole=='QUERY_ADMIN'){
            return false;
        }else {
            if (e === "tagIds" && this.accountType !== 1) {
                return false;
            }
            return true;
        }
    }

    function appIdEditor(container, options) {
        $('<input required name="' + options.field + '"/>')
            .appendTo(container)
            .kendoLov($.extend(${lovProvider.getLov(base.contextPath, base.locale, "WECHAT_ACCOUNT")}, {
                textField: 'name',
                data: options.model,
                change: function () {
                    options.model.set("tagIds", null);
                },
                select: function (e) {
                    options.model.set("tagIds", null);
                    options.model.set("accountType", e.item.accountType);
                    options.model.set("accountId", e.item.accountId);
                }
            }));
    }
    function tagIdsEditor(container, options) {
        $('<input name="' + options.field + '"/>')
            .appendTo(container)
            .kendoLov($.extend(${lovProvider.getLov(base.contextPath, base.locale, "WECHAT_TAG")}, {
                textField: 'tagName',
                model: options.model,
                query: function (e) {
                    debugger
                    e.param['accountId'] = options.model.accountId;
                }
            }));
    }

    function tagIdsTemp(dataItem) {
        return dataItem.tagName || '';
    }
    function contentEditor(container, options) {
        changedContent = options.model.content;
        Hls.openBox({
            url: "${base.contextPath}/weChat/contentEditor.view",
            winId: "editWin",
            closeFunction: function () {
                if (container.children().length < 1) {
                    container.innerHTML = changedContent;
                    // $("#grid").data("kendoGrid").closeCell();
                    container.blur();
                }
                // container.val("changedContent");
                options.model.set("content", changedContent);
            }
        })
    }

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

    ]]></script>
    <div id="editWin"></div>
    <h:dataSource id="gridDataSource" batch="true" pageSize="10" requestEnd="reqStart" serverPaging="true" error="requestError">
        <h:transport parameterMap="parameterMap">
            <h:read url="${base.contextPath}/wechat/notification/template/query" type="GET" dataType="json"/>
            <h:create url="${base.contextPath}/wechat/notification/template/submit" type="POST"
                      contentType="application/json"/>
            <h:update url="${base.contextPath}/wechat/notification/template/submit" type="POST"
                      contentType="application/json"/>
            <h:destroy url="${base.contextPath}/wechat/notification/template/remove" type="POST"
                       contentType="application/json"/>
        </h:transport>
        <h:schema data="rows" total="total" errors="schemaError">
            <h:model id="templateId" editable="deseditable">
                <h:fields>
                    <h:item name="templateName">
                        <h:validation required="true"/>
                    </h:item>
                    <h:item name="templateCode">
                        <h:validation required="true"/>
                    </h:item>
                    <h:item name="content">
                        <h:validation required="true"/>
                    </h:item>

                </h:fields>
            </h:model>
        </h:schema>
    </h:dataSource>
    <h:hlsGridBox hlsGridId="grid" hlsBtnType="add,save,query" title=""
                  viewModelName="viewModel.data" dataSourceId="gridDataSource" queryIds="name,code"
                  queryPlaceHolders="模板名称,模板代码">
        <h:hlsGrid id="grid" dataSource="gridDataSource" hlsBtnType="ADD,DELETE"
                   editable="inline" height="400px" resizable="true" navigatable="true"
                   locate="gridLocate">
            <h:pageable pageSizes="all,5,10,20,50" buttonCount="0" 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="templateName" title="<@spring.message '模板名称'/>" width="105">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="templateCode" title="<@spring.message '模板代码'/>" width="140">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="content" title="<@spring.message '模板内容'/>" width="160"
                          editor="contentEditor">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left;text-overflow: ellipsis;white-space: nowrap;"/>
                </h:column>
                <h:column field="appId" title="<@spring.message '发送应用'/>" width="100"
                          editor="appIdEditor" template="appTemp">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="tagIds" title="<@spring.message '选择发送标签'/>" width="105"
                          editor="tagIdsEditor" template="tagIdsTemp">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="templateKey" title="<@spring.message '服务号模板id'/>" width="295">
                    <h:attributes style="text-align:left"/>
                    <h:headerAttributes style="text-align:center"/>
                </h:column>

            </h:columns>
        </h:hlsGrid>
    </h:hlsGridBox>
    <script><![CDATA[

    HlsBindGridQuery("name,code", gridDataSource);
    ]]></script>
</h:screen>