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

    <script id="script"><![CDATA[
    var platformList = [{
        "description": "安卓",
        "meaning": "安卓",
        "value": "ANDROID"
    }, {
        "description": "苹果",
        "meaning": "苹果",
        "value": "IOS"
    }];

    var viewModel = kendo.observable({
        model: {},
        query : function () {
            viewModel.model.appCode = $("#appCode").val();
            $("#grid").data("kendoGrid").dataSource.page(1);
        }
    });

    function schemaError() {

    }
    function dsEditable(col) {
        return true;
    }
    function appTemp(dataItem){
        var result;
        appTypeList.forEach(function (value, index, array) {
            if (dataItem.appCode == value.value) {
                result = value.value;
            }
        })
        return result || '';
    }

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

    function typeTemp(dataItem) {
        var result;
        platformList.forEach(function (value, index, array) {
            if (dataItem.platform == 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: platformList
            });
    }
    function gridLocate(e) {
        var d = this.dataItem(e.element);
    }
    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.appCode = viewModel.model.appCode;
            for (var k in map) {
                if (map[k] === '' || map[k] === null || map[k] === undefined)
                    delete map[k]
            }
            return map;
        }
    }
    function requestError() {

    }

    ]]></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}/app/query/appVersionAll" type="GET" dataType="json"/>
            <h:create url="${base.contextPath}/app/add/appVersion" type="POST" contentType="application/json"/>
            <h:update url="${base.contextPath}/app/add/appVersion" type="POST" contentType="application/json"/>
            <h:destroy url="${base.contextPath}/app/delete/appVersion" type="POST" contentType="application/json"/>
        </h:transport>
        <h:schema data="rows" total="total" errors="schemaError">
            <h:model id="versionId" editable="dsEditable">
                <h:fields>
                    <h:item name="appCode">
                        <h:validation required="true"/>
                    </h:item>
                    <h:item name="platform">
                        <h:validation required="true"/>
                    </h:item>
                    <h:item name="versionUrl">
                        <h:validation required="true"/>
                    </h:item>
                    <h:item name="versionNum">
                        <h:validation required="true"/>
                    </h:item>
                    <h:item name="updateContent">
                        <h:validation required="true"/>
                    </h:item>
                    <h:item name="enableFlag" defaultValue="Y" type="boolean" checkedValue="Y" uncheckedValue="N"/>
                    <h:item name="smallVersion" defaultValue="Y" type="boolean" checkedValue="Y" uncheckedValue="N"/>
                    <h:item name="bigVersion" defaultValue="N" type="boolean" checkedValue="Y" uncheckedValue="N"/>
                </h:fields>
            </h:model>
        </h:schema>
    </h:dataSource>
    <h:hlsGridBox hlsGridId="grid" hlsBtnType="query,add,save" title=""
                  viewModelName="viewModel.data" dataSourceId="gridDataSource" queryId="appCode">
        <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="appCode" title='APP代码' width="100" editor="appEditor" template="appTemp">
                    <h:headerAttributes style="text-align:center" />
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="platform" title='平台' width="90" editor="typeEditor" template="typeTemp">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="versionUrl" title='下载地址' width="190">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="versionNum" title="版本号" width="120">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:center"/>
                </h:column>
                <h:column field="updateContent" title="更新内容" width="320">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:left"/>
                </h:column>
                <h:column field="smallVersion" title="小版本更新" width="110">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:center"/>
                </h:column>
                <h:column field="bigVersion" title="大版本更新" width="110">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:center"/>
                </h:column>
                <h:column field="enableFlag" title="启用标志" width="100">
                    <h:headerAttributes style="text-align:center"/>
                    <h:attributes style="text-align:center"/>
                </h:column>
            </h:columns>
        </h:hlsGrid>
    </h:hlsGridBox>

    <script>
        $('#appCode').keydown(function (e) {
            if (e.keyCode == 13) {
                e.target.blur();
                viewModel.query(e);
            }
        });
    </script>

</h:screen>