<?xml version="1.0" encoding="UTF-8"?>
<h:screen xmlns:h="http://www.hand-china.com/hap">
    <script src="${base.contextPath}/common/code?wechatType=WECHAT.TYPE" type="text/javascript"></script>
    <style>
        html {
            overflow: hidden;
        }
    </style>

    <script id="script"><![CDATA[
    var currentRole;
    $(document).ready(function() {
        $.ajax({
            url: '${base.contextPath}/get/roleCode/by/roleId',
            data: {roleId: ${Session.roleId}},
            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;
                        }
                    }
                }
            }
        });
    });
    function schemaError() {

    }
    function dsEditable(col) {
        if(currentRole=='QUERY_ADMIN'){
            return false;
        }else {
            if (this.isEnable == '0') {
                if (col != 'isEnable') {
                    return false;
                } else {
                    return true;
                }
            } else {
                return true;
            }
        }
    }
    var departmentId = ${RequestParameters.departmentId!-1};
    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;
        }
    }
    function gridLocate(e) {
        var d = this.dataItem(e.element);
    }
    function requestError() {

    }
    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 addressTemp(dataItem) {

        if (dataItem.isEnable == '1') {
            return '<a href="javascript:;" onclick="getInfo(' + dataItem.accountId + ',0,0,' + dataItem.accountType + ')">查看</a>'
        } else {
            return '';
        }
    }
    function getInfo(accountId, departmentIds, tags, accountType) {
        Hls.openBox({
            url: "${base.contextPath}/weChat/wechatDepartment.view?accountId=" + accountId + "&departmentIds=" + departmentIds + "&tags=" + tags + "&accountType=" + accountType,
            winId: "editWin",
            width: "1035px"
        });
    }

    ]]></script>
    <div id="editWin"></div>
    <h:dataSource id="gridDataSource" batch="true" pageSize="10" serverPaging="true" error="requestError">
        <h:transport parameterMap="parameterMap">
            <h:read url="${base.contextPath}/wechat/account/query" type="GET" dataType="json"/>
            <h:create url="${base.contextPath}/wechat/account/submit" type="POST" contentType="application/json"/>
            <h:update url="${base.contextPath}/wechat/account/submit" type="POST" contentType="application/json"/>
            <h:destroy url="${base.contextPath}/wechat/account/remove" type="POST" contentType="application/json"/>
        </h:transport>
        <h:schema data="rows" total="total" errors="schemaError">
            <h:model id="accountId" editable="dsEditable">
                <h:fields>
                    <h:item name="isEnable" defaultValue="1" type="boolean" checkedValue="1" uncheckedValue="0"/>
                </h:fields>
            </h:model>
        </h:schema>
    </h:dataSource>
    <h:hlsGridBox hlsGridId="grid" hlsBtnType="add,save" title=""
                  viewModelName="viewModel.data" dataSourceId="gridDataSource">
        <h:hlsGrid id="grid" dataSource="gridDataSource"
                   hlsBtnType="ADD" 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="accountCode" title='编码' width="105">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="name" title='账号名' width="105">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="companyName" title='公司名' width="190">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="appId" title="appId/corpId" width="160">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="secret" title="secret" width="320">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="accountType" title="账号种类" width="75" editor="typeEditor" template="typeTemp">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="isEnable" title="是否可用" width="80">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:center"/>
                </h:column>
                <h:column field="" title="通讯录" width="60" template="addressTemp">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:center"/>
                </h:column>
            </h:columns>
        </h:hlsGrid>
    </h:hlsGridBox>
    <script><![CDATA[
    var frame = parent.window.frames["iframe_WECHAT.ACCOUNT"];
    frame.style.height = "480px";
    ]]></script>

</h:screen>