<?xml version="1.0" encoding="UTF-8"?>
<h:screen xmlns:h="http://www.hand-china.com/hap">
    <script src="${base.contextPath}/common/code?userStatusData=WEB.DOC_TYPE" type="text/javascript"></script>

    <style>

        .form-horizontal .control-label {
            padding-top: 7px;
            margin-bottom: 0;
            width: 8%;
            padding-left: 0px;
            padding-right: 0px;
            text-align: right;
        }

        .panel * {
            box-sizing: border-box;
            padding-left: 3px;
            padding-right: 3px;
        }

        .col-sm-1 {
            width: 11.333333%;
        }

        .col-sm-2 {
            width: 12.666667%;
        }

        #query {
            position: absolute;
            top: 103px;
            left: 880px;
            background: rgb(0, 170, 170);
            color: white;
            border: none;
            width: 67px;
            height: 25px;
            text-align: center;
            line-height: 25px;
            cursor: pointer;
        }

        #query:hover {
            background: rgb(0, 190, 190);
        }

    </style>
    <script id="scriptA"><![CDATA[

    var wxRecoverId = '';

    function deseditable(col) {
        if (currentRole == 'QUERY_ADMIN' || col === "reContent") {
            return false;
        } else {
            if (col === "usable") {
                return true;
            } else {
                return false;
            }
        }
    }

    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({
        disabled: false,
        enabled: true,
        model: {
            enabledFlag: 'Y',
        },
        data: {},
        closeWin: function (e) {
            $("#roleWin").data("kendoWindow").close();
        },
        queryFunction: function (e) {
            content = $("#content").val();
            $('#grid').data('kendoGrid').dataSource.page(1);
        },
        close: function () {
            closeWin();
        },
    });

    function parameterMap(options, type) {
        if (type !== "read" && options.models) {
            var datas = options.models;
            if (type == 'create' || type == 'update' || type == 'destroy') {
                datas = options.models.map(function (data) {
                    if (type == 'create') {
                        data['__status'] = 'add';
                    }
                    if (type == 'update') {
                        data['__status'] = 'update';
                    }
                    if (type == 'destroy') {
                        data['__status'] = 'delete';
                    }
                    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 Hap.prepareQueryParameter(viewModel.data.toJSON(), options);
        }
    }

    function gridLocate(e) {
        var d = this.dataItem(e.element);
    }


    function template(dataItem) {
        var wxRecoverId = dataItem.wxRecoverId;
        return '<a onclick="openDetail(' + wxRecoverId + ')">编辑回复内容</a>'

    };

    function openDetail(wxRecoverId) {
        Hls.openBox({
            url: "${base.contextPath}/weChat/autoRecoverDetial.view?wxRecoverId=" + wxRecoverId,
            winId: "roleWin",
            width: 1250,
            param: {},
            closeFunction: function (e) {
                $('#grid').data('kendoGrid').dataSource.page(1);
            },
        });
    }

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

    ]]></script>

    <div id="roleWin"></div>

    <h:dataSource id="dataSource" batch="true" pageSize="20" requestEnd="reqEnd" serverPaging="true">
        <h:transport parameterMap="parameterMap">
            <h:read url="${base.contextPath}/wx/auto/recover/query" type="POST" dataType="json"/>
            <h:create url="${base.contextPath}/wx/auto/recover/submit" type="POST" dataType="json"
                      contentType="application/json; charset=utf-8"/>
            <h:update url="${base.contextPath}/wx/auto/recover/submit" type="POST" dataType="json"
                      contentType="application/json; charset=utf-8"/>
            <h:destroy url="${base.contextPath}/wx/auto/recover/remove" type="POST" dataType="json"
                       contentType="application/json; charset=utf-8"/>
        </h:transport>
        <h:schema data="rows" total="total">
            <h:model id="wxRecoverId" editable="deseditable">
                <h:fields>
                    <h:item name="usable" type="boolean" defaultValue="Y" uncheckedValue="N" checkedValue="Y"></h:item>
                </h:fields>
            </h:model>
        </h:schema>
    </h:dataSource>

    <h:hlsGridBox hlsGridId="grid" title="" hlsBtnType="delete,query,save,add" queryIds="content"
                  queryPlaceHolders="名称,编码">
        <h:hlsGrid id="grid" dataSource="dataSource" navigatable="true" locate="gridLocate"
                   height="400px" editable="true" hlsBtnType="DELETE"
                   resizable="true">
            <h:pageable pageSizes="5,10,20,50" buttonCount="2" refresh="true">
            </h:pageable>
            <h:columns>
                <h:column field="seq" title='<@spring.message "序号"/>' width="120">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:center"/>
                </h:column>
                <h:column field="content" title='<@spring.message "关键词"/>' width="120">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:center"/>
                </h:column>
                <h:column field="reContent" title='<@spring.message "回复词"/>' width="120">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:center"/>
                </h:column>
                <h:column field=" " title='<@spring.message "编辑"/>' width="130" template="template">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:center"/>
                </h:column>
                <h:column field="usable" title='<@spring.message "启用标志"/>' width="100">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:center"/>
                </h:column>
            </h:columns>
        </h:hlsGrid>
    </h:hlsGridBox>
    <script id="scriptB"><![CDATA[

    $("#content").keydown(function (e) {
        if (e.keyCode == 13) {
            e.target.blur();
            viewModel.queryFunction(e);
        }
    });

    $("#roleWin").kendoWindow({
        width: "900px",
        height: "500px",
        title: '<@spring.message "微信自动回复内容编辑"/>',
        modal: true,
        resizable: false,
        visible: false,
        iframe: true,
        close: function () {
            hlsWindowClose(this);
            dataSource.read();
        }
    });

    ]]></script>
</h:screen>