<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: syj
    $Date: 2018年7月10日10:13:26
    $Revision: 1.0
    $Purpose: 商品拆分入口
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application">
    <a:init-procedure/>
    <a:view>
        <a:link id="save_button_link_id" model="cont.CON810.batch_goods_tmp" modelaction="batch_update"/>
        <a:link id="goods_batch_import_upload_link_id"
                url="${/request/@context_path}/modules/cont/CON810/goods_batch_import_upload.lview"/>
        <a:link id="con810_atm_attachment_multi_query" model="cont.CON810.fnd_atm_attachment_multi_query"
                modelaction="query"/>
        <script type="text/javascript"><![CDATA[
        function import_button() {
            var record = $('goods_batch_ds').getCurrentRecord();

            if ($('goods_batch_ds').isModified()) {
                Leaf.showErrorMessage('错误', '页面有未保存数据，请先保存后再提交！');
                return;
            }

            if (record.length == 0) {
                Leaf.showMessage('${l:PROMPT}', '${l:FND.PLEASE_ADD_LINE}');
                return;
            } else {
                var batch_id = record.get('batch_id');
                var win = new Leaf.Window({
                    id: 'goods_batch_import_upload_winid',
                    url: $('goods_batch_import_upload_link_id').getUrl(),
                    params: {
                        batch_id: batch_id,
                        win_id: 'goods_batch_import_upload_winid',
                        parent_ds_id: 'goods_batch_ds'
                    },
                    title: '导入',
                    width: 420,
                    height: 300
                });
                win.on('close', function () {
                    $('goods_batch_ds').query();
                });
            }
        }


        function return_btn() {
            history.go(-1);
        }


        function add_btn() {
            $('goods_batch_grid_id').showEditorByRecord($('goods_batch_ds').create());
        }

        function query_btn() {
            $('goods_batch_ds').query();
        }

        function save_btn() {
            Leaf.Masker.mask(Ext.getBody());
            var ds = $('goods_batch_ds');
            var records = ds.getAll();
            var datas = [];
            for (var i = 0; i < records.length; i++) {
                records[i].set('_status', 'execute');
                datas.push(records[i].data);
            }
            if (ds.validate()) {
                Leaf.request({
                    url: $('save_button_link_id').getUrl(),
                    para: datas,
                    success: function () {
                        Leaf.SideBar.show({
                            msg: '保存成功！',
                            duration: 2000
                        });
                        Leaf.Masker.unmask(Ext.getBody());
                        query_btn();
                    },
                    failure: function() {
                        Leaf.Masker.unmask(Ext.getBody());
                        query_btn();
                    },
                    error: function() {
                        Leaf.Masker.unmask(Ext.getBody());
                        query_btn();
                    },
                    scope: this
                });
            }else{
                Leaf.Masker.unmask(Ext.getBody());
            }
        }

        function edit_renderer(value, record, name) {

            if (name == 'batch_date' || name == 'date_to' || name == 'date_from') {
                return Leaf.formatDate(value);
            }
        }

        function edit_function(record, name) {
            if (name == 'batch_date'  || name == 'date_from') {
                if (record.isNew) {
                    return 'good_batch_dp';
                } else {
                    return '';
                }
            }
            if (name == 'division_n') {
                if (record.isNew) {
                    return 'good_batch_cb';
                } else {
                    return '';
                }
            }
            if (name == 'batch_name') {
                if (record.isNew) {
                    return 'good_batch_tf';
                } else {
                    return '';
                }
            }

        }


        //预览超链接渲染
        function con810_preview_render(value, record, name) {
            return '<a href="javascript:showMeSomeRespect(' + record.id + ')">' + '预览' + '</a>'
        }

        //预览方法
        function showMeSomeRespect(recordId) {
            Leaf.Masker.mask(Ext.getBody());
            var record = $('goods_batch_ds').findById(recordId);
            var table_pk_value = record.get('batch_id');
            var table_name = 'T_GOODS_BATCH';
            Leaf.request({
                url: $('con810_atm_attachment_multi_query').getUrl(),
                para: {
                    table_name: table_name,
                    table_pk_value: table_pk_value
                },
                success: function (res) {
                    if (res.result.record && res.result.record.record_id) {
                        window.open('${/request/@context_path}/sys/office/view?_csrf=${/session/@_csrf.token}&sourceTypeCode=fnd_atm_attachment_multi&sourcePkValue=' + res.result.record.record_id);
                    } else {
                        Leaf.showErrorMessage('提示','尚未导入！');
                    }
                    Leaf.Masker.unmask(Ext.getBody());

                }

            });
        }

        ]]></script>
        <a:dataSets>
            <a:dataSet id="goods_policy_division_ds" lookupCode="GOODS_POLICY_DIVISION"/>
            <a:dataSet id="goods_batch_ds" autoQuery="true" model="cont.CON810.t_goods_batch" selectionModel="single"
                       pageSize="15" selectable="true">
                <a:fields>
                    <a:field name="division_n" requiredMessage="请输入必输字段:产品线" displayField="code_value_name" options="goods_policy_division_ds"
                             required="true" returnField="division" valueField="code_value"/>
                    <a:field name="division"/>
                    <a:field name="enable_flag" checkedValue="Y" defaultValue="N" uncheckedValue="N" readOnly="true"/>

                    <a:field name="batch_name" required="true"/>
                    <a:field name="batch_date" required="true"/>
                    <a:field name="date_from" required="true"/>
                    <a:field name="date_to" required="true"/>

                </a:fields>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <!--<a:gridButton click="return_btn" text="退出"/>-->
                <a:gridButton click="add_btn" text="新增"/>
                <a:gridButton click="query_btn" text="刷新"/>
                <a:gridButton click="save_btn" text="保存"/>
                <a:toolbarButton click="import_button" text="导入"/>
            </a:screenTopToolbar>
            <a:grid id="goods_batch_grid_id" bindTarget="goods_batch_ds" marginHeight="100" marginWidth="30"
                    navBar="true">
                <a:columns>
                    <a:column name="batch_number" align="center" prompt="政策批次号" width="200"/>
                    <a:column name="batch_name" prompt="批次名称" editorFunction="edit_function" width="350"/>
                    <a:column name="batch_date" prompt="创建时间" renderer="edit_renderer" editorFunction="edit_function"
                              width="180"/>
                    <a:column name="date_from" prompt="有效期从" renderer="edit_renderer" editorFunction="edit_function"
                              width="180"/>
                    <a:column name="date_to" prompt="有效期到" renderer="edit_renderer" editor="good_batch_dp"
                              width="180"/>
                    <a:column name="division_n" prompt="产品线" editorFunction="edit_function" width="200" />
                    <a:column name="enable_flag" prompt="启用" editor="good_batch_ck" width="80"/>
                    <a:column name="preview" prompt="预览" align="center" renderer="con810_preview_render" width="100"/>
                </a:columns>
                <a:editors>
                    <a:numberField id="good_batch_nf"/>
                    <a:textField id="good_batch_tf"/>
                    <a:datePicker id="good_batch_dp"/>
                    <a:comboBox id="good_batch_cb"/>
                    <a:checkBox id="good_batch_ck"/>
                </a:editors>
            </a:grid>
        </a:screenBody>

    </a:view>
</a:screen>
