<?xml version="1.0" encoding="utf-8"?>

<!--
    $author: zhuxianfei
    $date: 2018年6月26日 下午4:33:30
    $revision: 1.0
    $purpose:
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationenabled="true" dynamiccreateenabled="true"
          trace="true">
    <a:init-procedure/>
    <a:view>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
        <a:link id="${/parameter/@layout_code}_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code"
                modelaction="update"/>
        <a:link id="get_lend_number_link_id" model="cont.CON556.lend_record_number" modelaction="execute"/>
        <a:link id="lend_doc_submit_link_id" model="cont.CON556.lend_record_number" modelaction="update"/>
        <!-- <a:link id="tre_asset_contract_link"
                 url="${/request/@context_path}/modules/tre/TRE1050/tre_assets_contract_select.lview"/>
         <a:link id="get_assets_number_link_id" model="tre.TRE1050.get_new_assets_number" modelaction="update"/>

         <a:link id="${/parameter/@layout_code}_update_mark_link" model="tre.TRE1050.get_new_assets_number" modelaction="execute"/>-->

        <script type="text/javascript"><![CDATA[

        //提交审批
        window['${/parameter/@layout_code}_submit_approval_layout_dynamic_click'] = function () {
            //
            window['${/parameter/@layout_code}_SAVE_LAYOUT_DYNAMIC_CLICK']();
            var lend_hd_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_doc_lend_record_hd');
            var record = $(lend_hd_ds_id).getAll();
            window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
            Leaf.request({
                url: $('lend_doc_submit_link_id').getUrl(),
                para: {
                    lend_header_id: record[0].get('lend_header_id')
                },
                success: function () {
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    $('${/parameter/@winid}').close();
                    Leaf.SideBar.show({
                        msg: '操作成功',
                        duration: 2000
                    });
                },
                error: function () {
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                },
                failure: function () {
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                },
                sync: true,
                scope: this
            });
        };

        /*保存前调用，生成借阅编号*/
        window['${/parameter/@layout_code}_on_layout_dynamic_before_submit'] = function (ds, record) {
            var lend_hd_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_doc_lend_record_hd');
            var lend_ln_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_doc_lend_record_ln');
            var lend_record = $(lend_hd_ds_id).getAt(0);
            var check_flag = false;
            if (lend_record.get('lend_number')) {
                return true;
            }
            window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
            Leaf.request({
                url: $('get_lend_number_link_id').getUrl(),
                para: {
                    document_category: 'LEND',
                    document_type: 'LEND',
                    function_code: '${/parameter/@function_code}'
                },
                success: function (res) {
                    var document_number = res.result.document_number;
                    lend_record.set('lend_number', document_number);
                    check_flag = true;
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                },
                error: function () {
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                },
                failure: function () {
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                },
                sync: true,
                scope: this
            });
            return check_flag;

        };


        //更新时调用
        window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_update'] = function (ds, record, name, value, old_value, bp_seq) {
            var doc_head_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_doc_lend_record_hd');
            if (doc_head_ds_id == ds.id) {
                var doc_head_ds_record = $(doc_head_ds_id).getAll();
                if (name == 'send_flag') {
                    if (value == 'Y') {
                        record.getField('track_number').setRequired(true);
                        record.getField('recipient').setRequired(true);
                        record.getField('track_number').setReadOnly(false);
                        record.getField('recipient').setReadOnly(false);
                    } else {
                        record.getField('track_number').setRequired(false);
                        record.getField('recipient').setRequired(false);
                        record.getField('track_number').setReadOnly(true);
                        record.getField('recipient').setReadOnly(true);
                        record.set('track_number', '');
                        record.set('recipient', '');
                    }
                }

            }
        };

        ]]></script>

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