<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: LR  
    $Date: 2013-8-13 上午10:30:25  
    $Revision: 1.0  
    $Purpose: 
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true">
    <a:init-procedure><![CDATA[
        ]]>
    </a:init-procedure>
    <a:view>
        <a:link id="change_et_date_link" model="cont.CON560.con_contract_finish_for_query" modelaction="batch_update"/>
        <a:link id="${/parameter/@layout_code}con_contract_query_link"
                url="${/request/@context_path}/modules/cont/CON505/con_contract_modify.lview"/>
        <a:link id="con_contract_terminate_link_id" model="cont.CON560.con_contract_terminate"
                modelaction="batch_update"/>
        <a:link id="sys_service_welcome_link" url="${/request/@context_path}/welcome.lview"/>
        <a:link id="con_contract_update_link_id"
                url="${/request/@context_path}/modules/cont/CON500/con_contract_update.lview"/>
        <a:link id="con_contract_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code"
                modelaction="update"/>
        <script type="text/javascript"><![CDATA[
        function save_et_date_btn(){
             var datas=$('con560_contract_result_ds').getJsonData();
             if(datas.length==0){
                 $L.showInfoMessage("提示",'未修改任何数据!');
                 return;
             }else{
                 var arry=[];
                 for(var i=0;i<datas.length;i++){
                    var obj={};
                    obj.contract_id=datas[i].contract_id;
                    obj.et_date=Leaf.formatDate(datas[i].et_date);
                    obj._status=datas[i]._status;
                    arry.push(obj);

                 }
                 console.log(arry);
                 //保存数据
                 var detail_mask=Ext.getBody();
                 Leaf.Masker.mask(detail_mask);

                 Leaf.request({
                     url: $('change_et_date_link').getUrl(),
                     para: arry,
                     success: function() {
                         Leaf.SideBar.show({
                             msg: '操作成功',
                             duration: 2000
                         });
                         Leaf.Masker.unmask(detail_mask);
                         $('con560_contract_result_ds').query();
                     },
                     failure: function() {
                         Leaf.Masker.unmask(detail_mask);
                     },
                     error: function() {
                         Leaf.Masker.unmask(detail_mask);
                     },
                     scope: this
                 });
             }
        }
        function date_editor_func(record,name){
            if(record.get('contract_status')=='INCEPT'||record.get('contract_status')=='ETING'||record.get('contract_status')=='REPURING'){
                return 'date_editor';
            }else{
                return '';
            }
        }
        function con560_con_contract_detail() {
            var records = $('con560_contract_result_ds').getSelected();
            var seal_record = $('seal_info_ds').getCurrentRecord();

            if (!records.length) {
                Leaf.showMessage('${l:PROMPT}', '请至少选择一条记录');
                return;
            }
            if(!records[0].get('et_date')){
                $L.showInfoMessage("提示",'请输入合同结束日!');
                return;
            }
            //判断数据有没有修改
            var arrys=$('con560_contract_result_ds').data;
            for(var i=0;i<arrys.length;i++){
                var dirty_flag=arrys[i].dirty;
                if(dirty_flag){
                    $L.showInfoMessage("提示",'页面数据存在改动,请先保存数据!');
                    return;
                }
            }
            var TerminateData = [];
            for (var i = 0; i < records.length; i++) {
                if(records[i].data.is_terminate=='否'){
                    $L.showInfoMessage("提示",'未收金额如果不为0不可进行合同结束');
                    return;
                }
                jQuery.each(seal_record.data, function(j, v){
                    records[i].set( j ,  v );
                });
                records[i].set('_status', 'update');
                TerminateData.push(records[i].data);
            }
            Leaf.showConfirm('${l:PROMPT}', '确认提交合同结束工作流?', function () {
                Leaf.request({
                    url: $('con_contract_terminate_link_id').getUrl(),
                    para: TerminateData,
                    success: function () {
                        Leaf.SideBar.show({
                            msg: '提交成功',
                            duration: 2000
                        });
                        $('con560_contract_result_ds').query();
                    },
                    scope: this
                });
            }, function () {
            }, null, null);
        }

        function open_contract_win(ds_id, record_id) {
            var record = $(ds_id).findById(record_id);
            var param = record.data;
                param['function_code'] = 'CON301';
                param['function_usage'] = 'QUERY';
            param['winid'] = 'con_contract_modify_win_id';
            param['download'] = 'Y';
            param['url_title'] = '${l:CON301.CONTRACT_DETAIL}';
            hls_doc_get_layout_code('con_contract_get_layout_code_link_id', param, '${/parameter/@layout_code}con_contract_query_link', ds_id);
        }
        function con560_render_contractMaintainDs_grid(value, record, name) {
            if (name == 'contract_number') {
                return '<a href="javascript:open_contract_win(\'' + record.ds.id + '\',\'' + record.id + '\')">' + value + '</a>';
            }
        }

        function con560_param_set_value(param) {
            param['document_category'] = 'CONTRACT';
            param['function_code'] = 'CON301';
            param['function_usage'] = 'QUERY';
            param['maintain_type'] = 'READONLY';
            param['url_title'] = '${l:CON301.CONTRACT_DETAIL}';
            hls_doc_get_layout_code('con_contract_get_layout_code_link_id', param, 'con_contract_update_link_id', 'con560_contract_result_ds');
        }

        function con560_contractMaintainDs_grid_update(id) {
            var current_record = $('con560_contract_result_ds').findById(id);
            var param = current_record.data;
            param['document_id'] = current_record.get('contract_id');
            con560_param_set_value(param);
        }

        function con560_con_contract_query() {
            $('con560_contract_result_ds').query();
        }

        function con560_con_contract_reset() {
            $('con560_contract_query_ds').reset();
        }

        function summaryRenderer(datas, name) {
            var sum = 0;
            var sum2 = 0;
            for (var i = 0; i < datas.length; i++) {
                record = datas[i];
                if (name == "lease_item_amount") {
                    var t_1 = record.get("lease_item_amount");
                    var t_2 = parseFloat(t_1);
                    if (!isNaN(t_2)) {
                        sum += t_2;
                    }
                }
                if (name == "down_payment") {
                    var d_1 = record.get("down_payment");
                    var d_2 = parseFloat(d_1);
                    if (!isNaN(d_2)) {
                        sum2 += d_2;
                    }
                }
            }
            if (name == "bp_vender") {
                return '<div align="right">合计:</div>';
            }
            if (name == "lease_item_amount") {
                return '<font color="red">' + Leaf.formatNumber(sum, 2) + '</font>';
            }
            if (name == "down_payment") {
                return '<font color="red">' + Leaf.formatNumber(sum2, 2) + '</font>';
            }
        }

        function onIndexChange(ds, record, name, value, oldvalue){
            $('receivedAmountDs').setQueryParameter('contract_id', record.get('contract_id'));
            $('unreceivedAmountDs').setQueryParameter('contract_id', record.get('contract_id'));
            $('receivedAmountDs').query();
            $('unreceivedAmountDs').query();

            /*$('seal_info_ds').setQueryParameter('contract_id', record.get('contract_id'));
            $('seal_info_ds').query();*/

            if(Ext.isEmpty($('seal_info_ds').getCurrentRecord())){
                $('seal_info_ds').create();
            }

        }

        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
        <a:screen-include
                screen="modules/cont/CON500/con_contract_authority_list_validate.lview?document_category=CONTRACT&amp;function_code=CON560"/>
        <a:dataSets>
            <a:dataSet id="receivedAmountDs" autoQuery="true" model="cont.CON560.con_contract_received_amount"/>
            <a:dataSet id="unreceivedAmountDs" autoQuery="true" model="cont.CON560.con_contract_unreceived_amount"/>
            <a:dataSet id="con560_contract_status_ds" autoQuery="true" model="cont.CON560.con_contract_status"/>
            <a:dataSet id="con560_lease_organization_name_ds" loadData="true"
                       model="basic.hls_lease_organization_for_lov"/>
            <a:dataSet id="con560_contract_status_grid_ds" lookupCode="CON500_CONTRACT_STATUS"/>
            <a:dataSet id="seal_number_ds" lookupCode="SEAL_NUMBER_FINISH"/>
            <a:dataSet id="con560_contract_query_ds" autoCreate="true">
                <a:fields>
                    <a:field name="bp_name"  lovGridHeight="350" lovHeight="500" lovLabelWidth="100"
                             lovService="cont.CON541.hls_bp_masster_for_lov" lovWidth="520"
                             title="HLS.BP_TITLE">
                        <a:mapping>
                            <a:map from="bp_name" to="bp_name"/>
                            <a:map from="bp_code" to="bp_code"/>
                            <a:map from="bp_id" to="bp_id_tenant"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="bp_id_tenant"/>
                    <!-- <a:field name="contract_number" autoComplete="true" lovGridHeight="300" lovHeight="450" lovService="cont.CON531.con_contract_lov" lovWidth="500" title="HLS.CONTRACT_NUMBER">
                        <a:mapping>
                            <a:map from="contract_number" to="contract_number"/>
                            <a:map from="contract_id" to="contract_id"/>
                        </a:mapping>
                    </a:field> -->
                    <a:field name="contract_number" typeCase="Upper"/>
<!--                    <a:field name="lease_organization_name" displayField="description"-->
<!--                             options="con560_lease_organization_name_ds" returnField="lease_organization"-->
<!--                             valueField="lease_organization"/>-->
                    <a:field name="owner_user_id_n" lovGridHeight="350" lovHeight="475"
                             lovService="cont.CON560.sys_user_for_lov" lovWidth="520" title="出单人选择">
                        <a:mapping>
                            <a:map from="user_id" to="owner_user_id"/>
                            <a:map from="description" to="owner_user_id_n"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="contract_status_n" displayField="code_value_name"
                             options="con560_contract_status_ds" returnField="contract_status"
                             valueField="code_value"/>
                </a:fields>
            </a:dataSet>
            <a:dataSet id="con560_contract_result_ds" autoPageSize="true" autoQuery="true" pageSize="4"
                       model="cont.CON560.con_contract_finish_for_query" queryDataSet="con560_contract_query_ds"
                       queryUrl="${/request/@context_path}/autocrud/cont.CON560.con_contract_finish_for_query/query"
                       selectable="true" selectionModel="single">
                <!--<a:fields>
                    <a:field name="et_date" required="true" readOnly="false"/>
                </a:fields>-->
                <a:events>
                    <a:event name="indexChange" handler="onIndexChange"/>
                    <a:event name="query" handler="aut_authority_list_validate_query"/>
                </a:events>
            </a:dataSet>
            <a:dataSet id="seal_info_ds" autoQuery="true" model="cont.CON560.con_contract_seal_info">
                <a:fields>
                    <a:field name="instruments_types" readOnly="true" defaultValue="所有权转移通知书"/>
                    <a:field name="common_seal" readOnly="true" defaultValue="Y" uncheckedValue="N" checkedValue="Y"/>
                    <a:field name="corporate_visa_seal" readOnly="true" defaultValue="N" uncheckedValue="N" checkedValue="Y"/>
                    <a:field name="legal_seal" readOnly="true" defaultValue="N" uncheckedValue="N" checkedValue="Y"/>
                    <a:field name="con_special_seal" readOnly="true" defaultValue="N" uncheckedValue="N" checkedValue="Y"/>
                    <a:field name="financial_seal" readOnly="true" defaultValue="N" uncheckedValue="N" checkedValue="Y"/>
                    <a:field name="supervisio_seal" readOnly="true" defaultValue="N" uncheckedValue="N" checkedValue="Y"/>
                    <a:field name="seal_number_n" readOnly="true" displayField="code_value_name" options="seal_number_ds" returnField="seal_number" valueField="code_value" defaultValue="一式一份" />
                    <a:field name="seal_number" readOnly="true" defaultValue="SEAL_O_ON_NUMBER" />
                </a:fields>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <a:gridButton click="con560_con_contract_query" text="HLS.QUERY"/>
                <a:gridButton click="con560_con_contract_reset" text="HLS.RESET"/>
                <a:gridButton click="con560_con_contract_detail" text="合同结束"/>
            </a:screenTopToolbar>
            <a:form column="3" labelWidth="100" marginWidth="30" title="CON301.CONTRACT_QUERY">
                <a:textField name="contract_number" bindTarget="con560_contract_query_ds" prompt="HLS.CONTRACT_NUMBER"/>
                <a:lov name="bp_name" bindTarget="con560_contract_query_ds" prompt="HLS.TENANT_CODE"/>
                <!--<a:lov name="owner_user_id_n" bindTarget="con560_contract_query_ds" prompt="出单人"/>-->
<!--                <a:comboBox name="lease_organization_name" bindTarget="con560_contract_query_ds" prompt="事业部"/>-->
                <a:comboBox name="contract_status_n" bindTarget="con560_contract_query_ds" prompt="合同状态"/>
            </a:form>
            <a:grid id="con560_con_contract_grid_ds" bindTarget="con560_contract_result_ds" height="350"
                    marginWidth="30" navBar="true">
                <a:toolBar>

                    <a:button id="hn_atm_complement_atm_grid_save" click='save_et_date_btn' icon="${/request/@context_path}/images/save.png" text="保存"/>

                </a:toolBar>
                <a:columns>
                    <a:column name="contract_number" lock="true" prompt="HLS.CONTRACT_NUMBER"
                              renderer="con560_render_contractMaintainDs_grid" width="100"/>
                    <a:column name="bp_name" prompt="HLS.TENANT_NAME" width="150"/>
                    <a:column name="inception_of_lease" prompt="起租日期" renderer="Leaf.formatDate" width="100"/>
<!--                    <a:column name="lease_end_date" prompt="合同预计结束日期" renderer="Leaf.formatDate" width="140"/>-->
                    <a:column name="is_terminate" prompt="是否已结清" width="100"/>
                    <a:column name="residual_value" align="right" prompt="留购价" renderer="Leaf.formatMoney" width="50"/>
                    <!--<a:column name="signing_user_id_n" prompt="签约人" width="100"/>-->
<!--                    <a:column name="signing_date" prompt="签约时间" renderer="Leaf.formatDate" width="100"/>-->
<!--                    <a:column name="signing_location" prompt="签约地点" width="100"/>-->
                    <!--<a:column name="bp_vender" align="center" footerRenderer="summaryRenderer" prompt="主供应商名称" width="200"/>-->
                    <a:column name="lease_item_amount" align="right" footerRenderer="summaryRenderer" prompt="租赁物总价"
                              renderer="Leaf.formatMoney" width="120"/>
                    <a:column name="down_payment" align="right" footerRenderer="summaryRenderer" prompt="首付款"
                              renderer="Leaf.formatMoney" width="120"/>
                    <!--<a:column name="owner_user_id_n" prompt="出单人" width="60"/>-->
                    <a:column name="contract_status_desc" prompt="合同状态" width="80"/>
                    <a:column name="amount_total" prompt="应收租金总额" width="80" renderer="Leaf.formatMoney"/>
                    <a:column name="amount_principal" prompt="应收本金总额" width="80" renderer="Leaf.formatMoney"/>
                    <a:column name="amount_interest" prompt="应收利息总额" width="80" renderer="Leaf.formatMoney"/>
                    <a:column name="amount_received_interest" prompt="已收利息总额" width="80" renderer="Leaf.formatMoney"/>
                    <a:column name="amount_received_penalty" prompt="已收违约金" width="80" renderer="Leaf.formatMoney"/>
                    <a:column name="et_date" prompt="合同结束日" width="80" editorFunction="date_editor_func" renderer="Leaf.formatDate"/>
                </a:columns>
                <a:editors>
                    <a:datePicker id="date_editor"/>
                </a:editors>
            </a:grid>
            <a:tabPanel marginHeight="420" marginWidth="30">
                <a:tabs>
                    <a:tab prompt="未收金额" width="110">
                        <a:grid bindTarget="unreceivedAmountDs" height="250" marginWidth="55" navBar="true">
                            <a:columns>
                                <a:column name="times" prompt="期数"/>
                                <a:column name="cf_item_dis" prompt="类型"/>
                                <a:column name="due_amount" align="right" prompt="应收金额" renderer="Leaf.formatMoney"/>
                                <a:column name="received_amount" align="right" prompt="已收金额"
                                          renderer="Leaf.formatMoney"/>
                                <a:column name="due_date" prompt="预定支付日期"/>
                                <a:column name="amount" align="right" prompt="未收金额" renderer="Leaf.formatMoney"/>
                                <a:column name="principal" align="right" prompt="未收本金" renderer="Leaf.formatMoney"/>
                                <a:column name="interest" align="right" prompt="未收利息" renderer="Leaf.formatMoney"/>
                                <!--<a:column name="termination_date" prompt="回购日"/>-->
                                <a:column name="cf_status_dis" prompt="现金流状态"/>
                            </a:columns>
                        </a:grid>
                    </a:tab>
                    <a:tab prompt="已收金额" width="110">
                        <a:grid bindTarget="receivedAmountDs" height="250" marginWidth="55" navBar="true">
                            <a:columns>
                                <a:column name="times" prompt="期数"/>
                                <a:column name="cf_item_dis" prompt="类型"/>
                                <a:column name="due_amount" align="right" prompt="应收金额" renderer="Leaf.formatMoney"/>
                                <a:column name="received_amount" align="right" prompt="已收金额"
                                          renderer="Leaf.formatMoney"/>
                                <a:column name="due_date" prompt="预定支付日期"/>
                                <a:column name="received_principal" align="right" prompt="已收本金"
                                          renderer="Leaf.formatMoney"/>
                                <a:column name="received_interest" align="right" prompt="已收利息"
                                          renderer="Leaf.formatMoney"/>
                                <!--<a:column name="termination_date" prompt="回购日"/>-->
                                <a:column name="cf_status_dis" prompt="现金流状态"/>
                            </a:columns>
                        </a:grid>
                    </a:tab>
                    <a:tab prompt="用印信息" width="110">
                        <a:grid id="seal_info_ds_id" bindTarget="seal_info_ds" height="250" marginWidth="55" navBar="true">
                            <a:columns>
                                <a:column name="instruments_types" prompt="文书种类"/>
                                <a:column name="common_seal" prompt="公章" editor="seal_info_check_id"/>
                                <a:column name="corporate_visa_seal" prompt="法人签证章" editor="seal_info_check_id"/>
                                <a:column name="legal_seal" prompt="法人章" editor="seal_info_check_id"/>
                                <a:column name="con_special_seal" prompt="合同专用章" editor="seal_info_check_id"/>
                                <a:column name="financial_seal" prompt="财务章" editor="seal_info_check_id"/>
                                <a:column name="supervisio_seal" prompt="监管专用章" editor="seal_info_check_id"/>
                                <a:column name="seal_number_n" prompt="文书部数" editor="seal_info_combo_id"/>
                            </a:columns>
                            <a:editors>
                                <a:checkBox id="seal_info_check_id"/>
                                <a:comboBox id="seal_info_combo_id"/>
                            </a:editors>
                        </a:grid>
                    </a:tab>
                </a:tabs>
            </a:tabPanel>
        </a:screenBody>
    </a:view>
</a:screen>