<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: zhangxing5129  
    $Date: 2014-11-24 下午02:31:19  
    $Revision: 1.0  
    $Purpose: 合同文档借阅新增
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" trace="true">
    <a:init-procedure>
        <a:model-query defaultWhereClause="t1.user_id=${/session/@user_id}" fetchAll="true"
                       model="cont.CON556.get_user_name" rootPath="default_value_record"/>
    </a:init-procedure>
    <a:view>
        <a:link id="yonda_document_lend_update_link"
                url="${/request/@context_path}/modules/cont/CON556/yonda_document_lend_record_update.lsc"/>
        <a:link id="yonda_document_lend_record_save_link"
                url="${/request/@context_path}/modules/cont/CON556/yonda_document_lend_record_save.lsc"/>
        <a:link id="lend_record_number_link_id" model="cont.CON556.lend_record_number" modelaction="update"/>
        <!-- <a:link id="get_user_name_link_id" model="cont.CON556.get_user_name" modelaction="update"/> -->
        <a:link id="con_submit_approval_link" model="cont.CON556.contract_workflow_start" modelaction="update"/>
        <script type="text/javascript"><![CDATA[
        var lend_header_id;

        function con_contract_query_ds_update(ds, record, name, value, oldvalue) {
            var records = ds.getAll();
            if (name == 'contract_number') {
                if (!Ext.isEmpty(value)) {
                    for (var i = 0; i < records.length; i++) {
                        if (record.id != records[i].id) {
                            if (value == records[i].get('contract_number')) {
                                Leaf.showMessage('提示', '该合同已经存在，请选择其他合同！');
                                $('yonda_document_lend_record_ln_query_ds').setQueryParameter('contract_id', '');
                                $('yonda_document_lend_record_ln_query_ds').query();
                                return;
                            }
                        }
                    }
                }
                $('yonda_document_lend_record_ln_query_ds').setQueryParameter('contract_id', record.get('contract_id'));
                $('yonda_document_lend_record_ln_query_ds').query();
            }
        }

        function yonda_document_lend_record_add_close() {
            $('${/parameter/@winid}').close();
        }

        var count = 0;
        function lend_record_save() {
            var saveData = [];
            var header_record = $('yonda_document_lend_record_hd_create_ds').getCurrentRecord();
            var params = header_record.data;
            var lend_header_id = '${/parameter/@lend_header_id}';
            params['lend_header_id'] = lend_header_id;
            var contract_records = $('con_contract_ds').getAll();
            if (contract_records.length > 0) {
                for (var i = 0; i < contract_records.length; i++) {
                    var line_record = contract_records[i].data['record_ln'].data;
                    for (var j = 0; j < line_record.length; j++) {
                        // if (line_record[j].get('lend_flag') == 'Y') {
                        // line_record[j].set('_status', 'insert');
                        // line_record[j].set('contract_id', contract_records[i].get('contract_id'));
                        // saveData.push(line_record[j].data);
                        // }
                        if (count != 0) {
                            //如果头id不为空，则判断头id
                            if (line_record[j].get('lend_flag') == 'Y') {
                                if (line_record[j].get('lend_line_id')) {
                                    //如果行id不为空，则更新，否则插入
                                    line_record[j].set('_status', 'update');
                                    line_record[j].set('lend_header_id', lend_header_id);
                                    line_record[j].set('lend_line_id', line_record[j].get('lend_line_id'));
                                    line_record[j].set('cdd_item_id', line_record[j].get('cdd_item_id'));
                                    line_record[j].set('contract_id', contract_records[i].get('contract_id'));
                                } else {
                                    line_record[j].set('_status', 'insert');
                                    line_record[j].set('contract_id', contract_records[i].get('contract_id'));
                                }
                            } else {
                                if (line_record[j].get('lend_line_id')) {
                                    line_record[j].set('_status', 'delete');
                                    line_record[j].set('lend_header_id', lend_header_id);
                                    line_record[j].set('lend_line_id', line_record[j].get('lend_line_id'));
                                    line_record[j].set('cdd_item_id', line_record[j].get('cdd_item_id'));
                                    line_record[j].set('contract_id', contract_records[i].get('contract_id'));
                                }
                            }
                            var url = $('yonda_document_lend_update_link').getUrl();
                            saveData.push(line_record[j].data);
                        } else {
                            if (line_record[j].get('lend_flag') == 'Y') {
                                line_record[j].set('_status', 'insert');
                                debugger;
                                line_record[j].set('contract_id', contract_records[i].get('contract_id'));
                                url = $('yonda_document_lend_record_save_link').getUrl();
                                saveData.push(line_record[j].data);
                            }
                        }
                    }
                }
            }
            if (saveData.length == 0) {
                Leaf.showMessage('提示', '请至少选择文档一条记录');
                return;
            }
            params['line_records'] = saveData;
            Leaf.request({
                url: url,
                para: params,
                success: function (res) {
                    if (count == 0) {
                        count = 1;
                        Leaf.request({
                            url: $('lend_record_number_link_id').getUrl(),
                            para: {
                                lend_header_id: lend_header_id
                            },
                            success: function () {
                            },
                            error: function () {
                            },
                            failure: function () {
                            }
                        });
                    }
                    $('yonda_document_lend_record_ln_query_ds').setQueryParameter('lend_header_id', lend_header_id);
                    // $('yonda_document_lend_record_ln_query_ds').setQueryParameter('contract_id', contract_records[i].get('contract_id'));
                    $('yonda_document_lend_record_ln_query_ds').query();
                },
                error: function () {
                },
                failure: function () {
                }
            });
        }

        function date_Validator2(record, name, value) {
            var rec = $('yonda_document_lend_record_hd_create_ds').getCurrentRecord();
            var rec2 = $('yonda_document_lend_record_ln_query_ds').getCurrentRecord();
            if (name == 'promise_date' || name == 'send_date') {
                if (!Ext.isEmpty(value) && typeof(value) != 'undefined') {
                    if (rec2.get('promise_date') < rec.get('send_date')) {
                        return '承诺归还日期不能小于寄出日期！';
                    }
                }
                return value;
            }
        }

        function con_contract_lend_ds_update(ds, record, name, value, oldvalue) {
            if (name == 'send_date') {
                var records = $('yonda_document_lend_record_ln_query_ds').getSelected()();
                for (var i = 0; i < records.length; i++) {
                    var promise_date = records[i].get('promise_date');
                    if (!Ext.isEmpty(value) && typeof(value) != 'undefined' && !Ext.isEmpty(promise_date) && typeof(promise_date) != 'undefined') {
                        if (promise_date < value) {
                            Leaf.showMessage('提示', '承诺归还日期不能小于寄出日期！');
                            return;
                        }
                    }
                }
            }
        }

        function lend_record_submit() {
            var record = $('yonda_document_lend_record_hd_create_ds').getCurrentRecord();//
            lend_header_id = record.get('lend_header_id');
            Leaf.showConfirm('提示', '确认提交借阅申请吗？', function () {
                if (!lend_header_id) {
                    Leaf.showMessage('提示', '请保存后再提交！');
                    return;
                }
                if ($('yonda_document_lend_record_hd_create_ds').validate()) {
                    var header_record = $('yonda_document_lend_record_hd_create_ds').getCurrentRecord();
                    if (!$('yonda_document_lend_record_hd_create_ds').validate()) {
                        return;
                    }
                    var contract_records = $('con_contract_ds').getCurrentRecord();
                    if (!$('con_contract_ds').validate()) {
                        return;
                    }
                }
                var contract_id = contract_records.get('contract_id');
                //debugger;
                var url = $('con_submit_approval_link').getUrl();
                Leaf.request({
                    url: url,
                    para: {
                        contract_id: contract_id,
                        lend_header_id: lend_header_id
                    },
                    success: function () {
                        $('${/parameter/@winid}').close();
                        Leaf.SideBar.show({
                            msg: '操作成功',
                            duration: 2000
                        });
                    },
                    failure: function () {
                    },
                    error: function () {
                    },
                    scope: this
                });
            });
        }

        function zjwfl5110_beforeapproveCheck(type) {
            //debugger;
            var saveData = [];
            var header_record = $('yonda_document_lend_record_hd_create_ds').getCurrentRecord();
            var params = header_record.data;
            //debugger;
            var contract_records = $('con_contract_ds').getAll();
            if (contract_records.length > 0) {
                for (var i = 0; i < contract_records.length; i++) {
                    var line_record = contract_records[i].data['record_ln'].data;
                    for (var j = 0; j < line_record.length; j++) {
                        if (header_record.get('lend_header_id')) {
                            //如果头id不为空，则判断头id
                            if (line_record[j].get('lend_flag') == 'Y') {
                                if (line_record[j].get('lend_line_id')) {
                                    //如果行id不为空，则更新，否则插入
                                    line_record[j].set('_status', 'update');
                                    line_record[j].set('lend_header_id', header_record.get('lend_header_id'));
                                    line_record[j].set('lend_line_id', line_record[j].get('lend_line_id'));
                                    line_record[j].set('cdd_item_id', line_record[j].get('cdd_item_id'));
                                    line_record[j].set('contract_id', contract_records[i].get('contract_id'));
                                } else {
                                    line_record[j].set('_status', 'insert');
                                    line_record[j].set('contract_id', contract_records[i].get('contract_id'));
                                }
                            } else {
                                if (line_record[j].get('lend_line_id')) {
                                    line_record[j].set('_status', 'delete');
                                    line_record[j].set('lend_header_id', header_record.get('lend_header_id'));
                                    line_record[j].set('lend_line_id', line_record[j].get('lend_line_id'));
                                    line_record[j].set('cdd_item_id', line_record[j].get('cdd_item_id'));
                                    line_record[j].set('contract_id', contract_records[i].get('contract_id'));
                                }
                            }
                            var url = $('yonda_document_lend_update_link').getUrl();
                            saveData.push(line_record[j].data);
                        } else {
                            if (line_record[j].get('lend_flag') == 'Y') {
                                line_record[j].set('_status', 'insert');
                                debugger;
                                line_record[j].set('contract_id', contract_records[i].get('contract_id'));
                                url = $('yonda_document_lend_record_save_link').getUrl();
                                saveData.push(line_record[j].data);
                            }
                        }
                    }
                }
            }
            params['line_records'] = saveData;
            Leaf.request({
                url: url,
                para: params,
                success: function (res) {
                    return true;
                },
                error: function () {
                    return false;
                },
                failure: function () {
                    return false;
                }
            });
        }
        ]]></script>
        <a:dataSets>
            <a:dataSet id="borrow_to_ds" lookupCode="BORROW_TO"/>
            <a:dataSet id="yes_or_no" lookupCode="YES_NO"/>
            <a:dataSet id="yonda_document_lend_record_hd_create_ds" autoCreate="true">
                <a:fields>
                    <a:field name="borrower_name" defaultValue="${/model/default_value_record/record/@employee_name}"
                             readOnly="true"/>
                    <a:field name="borrower_phone"/>
                    <a:field name="lend_number"/>
                    <a:field name="lend_header_id"/>
                    <a:field name="borrow_to_des" displayField="code_value_name" options="borrow_to_ds" required="true"
                             returnField="borrow_to" valueField="code_value"/>
                    <a:field name="borrow_to"/>
                    <a:field name="unit_id"/>
                    <a:field name="apply_date" defaultValue="${/model/default_value_record/record/@lend_time}"
                             readOnly="true"/>
                    <a:field name="send_date"/>
                    <a:field name="send_number"/>
                    <a:field name="back_date"/>
                    <a:field name="back_number"/>
                    <a:field name="promise_date" required="true"/>
                    <a:field name="reader_name" required="true"/>
                    <!-- <a:field name="link_phone"/> -->
                    <a:field name="lend_number" readOnly="true"/>
                    <!-- <a:field name="borrower_name"/>-->
                    <a:field name="borrow_reason" required="true"/>
                    <a:field name="send_flag" required="true"/>
                    <!--  <a:field name="send_flag_n"  displayField="code_value_name" options="yes_or_no" required="true"
                               returnField="send_flag" valueField="code_value/>-->
                     <a:field name="send_flag_n" displayField="code_value_name" options="yes_or_no"  required="true" returnField="send_flag" valueField="code_value"/>

                    <a:field name="track_number"/>
                    <a:field name="recipient"/>
                </a:fields>
                <a:events><![CDATA[]]></a:events>
            </a:dataSet>
            <a:dataSet id="con_contract_ds" selectable="true">
                <a:fields>
                    <a:field name="contract_id"/>
                    <a:field name="contract_name" readOnly="true"/>
                    <a:field name="bp_name" readOnly="true"/>
                    <a:field name="contract_number" lovHeight="500" lovService="cont.CON556.con_contract_v_for_lov_lend"
                             lovWidth="450" required="true">
                        <a:mapping>
                            <a:map from="contract_id" to="contract_id"/>
                            <a:map from="contract_number" to="contract_number"/>
                            <a:map from="contract_name" to="contract_name"/>
                            <a:map from="bp_name" to="bp_name"/>
                            <a:map from="bp_type_desc" to="bp_type_desc"/>
                            <a:map from="hls_lease_channel_desc" to="hls_lease_channel_desc"/>
                        </a:mapping>
                    </a:field>
                </a:fields>
                <a:events>
                    <a:event name="update" handler="con_contract_query_ds_update"/>
                </a:events>
            </a:dataSet>
            <a:dataSet id="yonda_document_lend_record_ln_query_ds" bindName="record_ln" bindTarget="con_contract_ds"
                       fetchAll="true" model="cont.CON556.yonda_document_lend_record_update" selectable="true">
                <a:fields>
                    <a:field name="lend_flag" checkedValue="Y" uncheckedValue="N"/>
                    <a:field name="document_id"/>
                    <a:field name="cdd_item_id"/>
                    <a:field name="description"/>
                    <a:field name="deposit" datatype="number"/>
                    <!-- <a:field name="promise_date"/> -->
                    <a:field name="comments"/>
                    <a:field name="use_to"/>
                    <a:field name="quantity"/>
                </a:fields>
                <a:events>
                    <a:event name="update" handler="con_contract_query_ds_update"/>
                </a:events>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:gridButton click="yonda_document_lend_record_add_close" text="HLS.BACK"/>
                <a:gridButton click="lend_record_save" text="HLS.SAVE"/>
                <a:gridButton click="lend_record_submit" text="HLS.SUBMIT"/>
            </a:screenTopToolbar>
            <a:form column="3" labelWidth="120" marginWidth="60" title="借阅人信息">
                <a:textField name="lend_number" bindTarget="yonda_document_lend_record_hd_create_ds"
                             prompt="借阅申请编号"/>
                <a:textField name="borrower_name" bindTarget="yonda_document_lend_record_hd_create_ds"
                             prompt="借阅人"/>
                <a:datePicker name="apply_date" bindTarget="yonda_document_lend_record_hd_create_ds" prompt="申请日期"/>

                <a:textField name="borrow_reason" bindTarget="yonda_document_lend_record_hd_create_ds"
                             prompt="借阅原因"/>
                <a:datePicker name="promise_date" bindTarget="yonda_document_lend_record_hd_create_ds"
                              prompt="预计归还日期"/>

                <a:comboBox name="send_flag_n" bindTarget="yonda_document_lend_record_hd_create_ds" prompt="是否寄送"/>
                <a:textField name="track_number" bindTarget="con_contract_ds" prompt="快递单号"/>
                <a:textField name="recipient" bindTarget="con_contract_ds" prompt="收件人"/>
            </a:form>
            <a:grid bindTarget="yonda_document_lend_record_ln_query_ds" marginHeight="270" marginWidth="60"
                    navBar="true">
                <a:toolBar>
                    <a:button type="save" text="保存"/>
                    <a:button type="delete" text="删除"/>
                    <a:button type="add" text="新增"/>
                </a:toolBar>
                <a:columns>
                    <a:column name="contract_number" prompt="合同号" editor="grid_lov" width="200"/>
                    <a:column name="bp_name" prompt="客户名" width="200"/>
                    <a:column name="file_name" prompt="文件名" editor="grid_lov2" width="300"/>

                    <!-- <a:column name="description" prompt="文档名称" width="600"/>
                     <a:column name="lend_flag" editor="yonda_document_lend_record_add_cb" prompt="是否借出" width="80"/>
                     <a:column name="quantity" align="center" editor="grid_nf2" prompt="份数" width="40"/>
                     <a:column name="use_to" editor="grid_tf" prompt="用途" width="200"/>
                     &lt;!&ndash; <a:column name="deposit" editor="grid_nf" prompt="实收押金" renderer="Leaf.formatMoney"/> &ndash;&gt;
                     <a:column name="comments" editor="grid_tf" prompt="备注" width="300"/>-->
                </a:columns>
                <a:editors>
                    <a:checkBox id="yonda_document_lend_record_add_cb"/>
                    <a:datePicker id="grid_dp"/>
                    <a:numberField id="grid_nf" allowFormat="true" allowNegative="false" decimalPrecision="2"/>
                    <a:numberField id="grid_nf2" allowDecimals="false" allowNegative="false"/>
                    <a:textField id="grid_tf"/>
                    <a:lov id="grid_lov"/>
                    <a:lov id="grid_lov2" require="true"/>
                </a:editors>
            </a:grid>
        </a:screenBody>
    </a:view>
</a:screen>
