<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: qm  
    $Date: 2014-6-7
    $Revision: 1.0  
    $Purpose: 
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" trace="true">
    <a:init-procedure>
        <a:model-query model="cont.CON541.get_sub_condition" rootPath="get_sub_condition"/>
        <a:model-query model="cont.CON555.con555_get_info" rootPath="con555_get_info"/>
        <a:model-execute model="db.prj_cdd_item_pkg.prj_cdd_item_extra_save"/>
    </a:init-procedure>
    <a:view>
        <a:link id="con555_cdd_uploadFile_id" url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="con555_cdd_download_id" url="${/request/@context_path}/atm_download.lsc"/>
        <a:link id="contract_history_window_link" url="${/request/@context_path}/modules/cont/CON541/con_document_history_query.lview"/>
        <a:link id="con555_cdd_query_link" url="${/request/@context_path}/autocrud/cont.CON541.con_content_query_detail/query"/>
        <a:link id="con_contract_sign_link_id" model="cont.CON541.con_contract_save" modelaction="batch_update"/>
        <a:link id="contract_credit_submit_link" model="cont.CON541.contract_credit_submit_execute" modelaction="execute"/>
        <a:script><![CDATA[
            window['con555_cdd_editorFunction'] = function(record, name) {
                //var credit_status = $('con555_detail_ds').getCurrentRecord().get('credit_status');
                if (record.get('sys_flag') == 'N' ) {
                    return 'con555_cdd_tf_id';
                }
                return '';
            };
            
            function con555_cdd_editorFunction1(record, name){
                //var credit_status = $('con555_detail_ds').getCurrentRecord().get('credit_status');
                if(record.get('sys_flag') == 'Y'){
                    return '';
                }
                else return 'con555_cdd_tf_id1'
            }
            
            function con555_cdd_selectFunc(record) {
                if (record.get('sys_flag') == 'Y') {
                    return false;
                }
                return true;
            }
            window['con555_cdd_attachment_render'] = function(value, record, name) {
                //
                //var credit_status = $('con555_detail_ds').getCurrentRecord().get('credit_status');
                var check_id = record.get('check_id');
                var file_name = record.get('file_name');
                var record_id = record.id;
                //&& credit_status != 'BACK1'
                if (!record.isNew && check_id ) {
                    return '<a href="javascript:window[\'con555_cdd_attachtment_upload\'](\'' + record.get('check_id') + '\',\'' + file_name + '\',\'' + record_id + '\')">${l:HLS.ATTACHMENT}</a>';
                }
            };
              window['con555_cdd_attachment_render_fw'] = function(value, record, name) {
                //
                //var credit_status = $('con555_detail_ds').getCurrentRecord().get('credit_status');
                var check_id = record.get('check_id');
                var file_name = record.get('file_name');
                var record_id = record.id;
                //&& credit_status != 'BACK1'
                if (!record.isNew && check_id ) {
                    return '<a href="javascript:window[\'con555_cdd_attachtment_upload\'](\'' + record.get('check_id') + '\',\'' + file_name + '\',\'' + record_id + '\')">${l:HLS.ATTACHMENT}</a>';
                }
            };
            window['con555_link_render'] = function(value, record, name) {
                if (value != null) {
                    var link = '${/request/@context_path}/atm_download.lsc?attachment_id=';
                    var str = value.split(';;');
                    var url = '';
                    for (var i = 0;i < str.length;i++) {
                        var temp = str[i].split('--');
                        if (!Leaf.isEmpty(temp[0])) {
                            url = url + '<a href=' + link + temp[1] + '>' + temp[0] + '</a>' + ',';
                        }
                    }
                    return url; 
                }
            };           
            
            window['con555_cdd_attachtment_upload'] = function(check_id, file_name, record_id) {
                //
                var url = $('con555_cdd_uploadFile_id').getUrl() + '?table_name=PRJ_CDD_ITEM_CHECK&header_id=' + check_id;
                var record = $('con555_detail_result_ds').getCurrentRecord();
                var oldvalue = file_name ? file_name : null;
                var contract_id = ${/parameter/@contract_id};
                paras = {
                    'contract_id': contract_id
                };
                var win = new Leaf.Window({
                    url: url,
                    title: '${l:HLS.SUPPORTING_DOCUMENT}',
                    id: 'con555_cdd_uploadFile_screen_id',
                    width: 850,
                    height: 400
                });
                win.on('close', function() {
                    //
                    Leaf.request({
                        url: $('con555_cdd_query_link').getUrl(),
                        para: paras,
                        success: function(data) {
                            //
                            $('con555_detail_result_ds').query();
                            $('con555_detail_result1_ds').query();
            
                            //var newvalue = $('con555_detail_result_ds').findById(record_id).get('file_name');
                        },
                        sync: true
                    });           
                });
            
            };
            window['con555_cdd_required_render'] = function(value, record, name) {
                // var credit_status = $('con555_detail_ds').getCurrentRecord().get('credit_status');
                var project_required_flag = record.get('project_required_flag');
                if (project_required_flag == 'Y') {
                    return '<pan style="color:red">' + value + '</pan>';
                } else {
                    return value;
                }
                // if(credit_status = 'BACK1'){
                    
                // }
            };
            // window['con555_attach_add'] = function(ds) {
                // var record = $('con555_detail_result_ds').getAt(0);
                // var current_record = ds.getCurrentRecord();
                // current_record.data['document_table'] = 'PRJ_CDD_ITEM_CHECK';
                // current_record.data['cdd_list_id'] = record.get('cdd_list_id');
                // current_record.data['document_id'] = record.get('document_id');
                // current_record.data['note'] = record.get('note');
                // document_category = record.get('document_category');
                // if (document_category == 'CONTRACT') {
                    // current_record.data['contract_required_flag'] = 'N';
                    // current_record.data['contract_display_flag'] = 'Y';
                // } else if (document_category == 'PROJECT') {
                    // current_record.data['project_required_flag'] = 'N';
                    // current_record.data['project_display_flag'] = 'Y';
                // }
            // };
            window['con555_attach_load'] = function(ds) {
                var records = ds.getAll();
                if (records.length) {
                    for (var i = 0;i < records.length;i++) {
                        var record = records[i];
                        if (Ext.isEmpty(record.get('check_id'))) {
                            record.dirty = true;
                        }
                    }
                }
            };
            
            // function con555_attach_update(ds, record, value, oldvalue, newvalue) {
                // // alert('con555_attach_update');  
            // }
            
            // function con555_attach_query(ds) {       
                // alert('con555_attach_refresh');
            // }
            
            function con555_con_contract_query() {
                //console.log($('con555_contract_query_ds').data[0].getMeta().getField('contract_status_desc'));
                $('con555_detail_result_ds').query();
            }
            
            function con555_con_contract_reset() {
                $('con555_detail_ds').reset();
            }
                       
            function con555_con_contract_back()
            {
                $('con_contract_content_window').close();
            }
            
            function con555_con_contract_sign() {
                if ($('con555_detail_ds').validate()) {
                    var record = $('con555_detail_ds').getCurrentRecord();
                    
            		record.set('_status', 'update');
            		
            		var saveData = [];

            		saveData.push(record.data);
                        Leaf.request({
                            url: $('con_contract_sign_link_id').getUrl(),
                            para: saveData,
                            success: function() {
                                Leaf.SideBar.show({
                                    msg: '基础信息保存成功',
                                    duration: 2000
                                });
                                $('con555_detail_result1_ds').submit();
                                $('con555_detail_result_ds').submit();
                                $('con555_detail_ds').query();
                                // $('con555_detail_result_ds').query();
                                // $('con555_detail_result1_ds').query();
                            },
                            scope: this
                        });
                }
            }
            
            function document_history_query() {
                var contract_id=$('con555_detail_ds').getCurrentRecord().get('contract_id');
                new Leaf.Window({
				    id:'contract_history_window',
				    url:$('contract_history_window_link').getUrl(),
				    params:{
				        contract_id:contract_id
				    },
				    fullScreen:true
				});		
            }
            
            function submit_contract(){
                var flag;
                flag = '${/model/get_sub_condition/record/@flag}';
                if(!$('con555_detail_ds').validate()){
					return ;
                }
               
                // if($('con555_detail_result_ds').getAll().length<1 && flag=='Y')
                // {
                    // Leaf.showMessage('提示','该合同为附条件通过合同，请根据附条件审批意见在信审附件中上传相应附件！');
                    // return;
                // }
                var record = $('con555_detail_ds').getCurrentRecord();
                if(record.dirty){
                    Leaf.showMessage('提示','页面有未保存数据，请先保存后再提交！');
                    return ;
                }                
                 new Leaf.showConfirm('提示','确认提交吗？',(function(){
                     var contract_id = record.get('contract_id');
					 Leaf.request({
		                url: $('contract_credit_submit_link').getUrl(),
		                para: {contract_id:contract_id},
		                success: function(res) {
		                    Leaf.SideBar.show({
                                    msg: '提交成功',
                                    duration: 2000
                                });
                           $('con_contract_content_window').close();
		                },
		                scope: this
		            });
                 }));  
            }
            
            function con541_update(ds,record,name,value,oldvalue){
                if(name=='dd_agreement_status'){
                    if(value=='Y'){
                        record.getField('ebank_id_n').setRequired(true);
                        record.getField('ebank_type_n').setRequired(true);
                        record.getField('ebank_account_name').setRequired(true);
                        record.getField('ebank_account_num').setRequired(true);
                    }
                    else
                    {
                        record.getField('ebank_id_n').setRequired(false);
                        record.getField('ebank_type_n').setRequired(false);
                        record.getField('ebank_account_name').setRequired(false);
                        record.getField('ebank_account_num').setRequired(false);
                    }
                }
            }
        ]]></a:script>
        <a:dataSets>
            <a:dataSet id="ebank_cmb_ds" lookupCode="EBANK_BBKNBR"/>
            <a:dataSet id="yes_no_ds" lookupCode="YES_NO"/>
            <a:dataSet id="con555_detail_ds" autoQuery="true" fetchAll="true" model="cont.CON541.con_contract_for_query" queryUrl="${/request/@context_path}/autocrud/cont.CON541.con_contract_for_query/query?contract_id=${/parameter/@contract_id}">
                <a:fields>
                    <a:field name="dd_agreement_status_desc" displayField="code_value_name" options="yes_no_ds" returnField="dd_agreement_status" valueField="code_value"/>
                    <a:field name="contract_id"/>
                    <a:field name="contract_number" readOnly="true"/>
                    <a:field name="comment_text" readOnly="true"/>
                    <a:field name="bp_name" readOnly="true"/>
                    <!--<a:field name="zero_times_date" required="true"/>-->
                    <a:field name="signing_date" required="true"/>
                    <a:field name="signing_location" required="true"/>
                    <a:field name="contract_status_desc" readOnly="true"/>
                    <a:field name="ebank_type_n" displayField="code_value_name" options="ebank_cmb_ds" returnField="ebank_type" valueField="code_value"/>
                    <a:field name="ebank_id_n" autoComplete="true" lovGridHeight="300" lovHeight="450" lovService="cont.CON541.csh_bank_lov" lovWidth="500" title="银行">
                        <a:mapping>
                            <a:map from="bank_id" to="ebank_id"/>
                            <a:map from="bank_short_name" to="ebank_id_n"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="credit_status"/>
                </a:fields>
                <a:events>
                    <a:event name="update" handler="con541_update"/>
                </a:events>
            </a:dataSet>
            <a:dataSet id="con555_detail_result_ds" autoQuery="true" model="cont.CON541.con_content_query_detail" pageSize="10" queryUrl="${/request/@context_path}/autocrud/cont.CON541.con_content_query_detail/query?contract_id=${/parameter/@contract_id}" selectFunction="con555_cdd_selectFunc" selectable="true">
                <a:fields>
                    <a:field name="document_table" defaultValue="CON_CONTRACT"/>
                    <a:field name="paper_required" checkedValue="Y" defaultValue="N" uncheckedValue="N"/>
                    <a:field name="sign_required_flag" checkedValue="Y" readOnly="true" uncheckedValue="N"/>
                    <a:field name="paper_required" checkedValue="Y" defaultValue="N" uncheckedValue="N"/>
                    <a:field name="attachment_required" checkedValue="Y" defaultValue="N" uncheckedValue="N"/>
                    <a:field name="sys_flag" defaultValue="N"/>
                    <a:field name="document_id" defaultValue="${/parameter/@contract_id}"/>
                    <a:field name="cdd_list_id" defaultValue="${/parameter/@cdd_list_id}"/>
                    <a:field name="note"/>
                    <a:field name="note2"/>
                    <a:field name="note3"/>
                    <a:field name="note4"/>
                    <a:field name="note5"/>
                </a:fields>
            </a:dataSet>
            <a:dataSet id="con555_detail_result1_ds" autoQuery="true" model="cont.CON541.con_content_query_detail_1" pageSize="10" queryUrl="${/request/@context_path}/autocrud/cont.CON543.con_content_query_detail/query?contract_id=${/parameter/@contract_id}" selectFunction="con555_cdd_selectFunc" selectable="true">
                <a:fields>
                    <a:field name="document_table" defaultValue="CON_CONTRACT_CREDIT"/>
                    <a:field name="paper_required" checkedValue="Y" defaultValue="N" uncheckedValue="N"/>
                    <a:field name="sign_required_flag" checkedValue="Y" readOnly="true" uncheckedValue="N"/>
                    <a:field name="paper_required" checkedValue="Y" defaultValue="N" uncheckedValue="N"/>
                    <a:field name="attachment_required" checkedValue="Y" defaultValue="N" uncheckedValue="N"/>
                    <a:field name="sys_flag" defaultValue="N"/>
                    <a:field name="document_id" defaultValue="${/parameter/@contract_id}"/>
                    <a:field name="cdd_list_id" defaultValue="${/parameter/@cdd_list_id}"/>
                    <a:field name="note"/>
                    <a:field name="note2"/>
                    <a:field name="note3"/>
                    <a:field name="note4"/>
                    <a:field name="note5"/>
                </a:fields>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <a:gridButton click="con555_con_contract_back" text="返回"/>
                <a:gridButton click="con555_con_contract_sign" text="保存"/>
                <a:gridButton click="submit_contract" text="提交"/>
                <a:gridButton click="document_history_query" text="交涉记录"/>
            </a:screenTopToolbar>
            <a:form column="5" labelWidth="100" title="基础信息" width="1300">
                <a:textField name="contract_number" bindTarget="con555_detail_ds" prompt="合同编号"/>
                <a:lov name="bp_name" bindTarget="con555_detail_ds" prompt="承租人名称"/>
                <a:datePicker name="signing_date" bindTarget="con555_detail_ds" prompt="签约时间"/>
                <a:textField name="signing_location" bindTarget="con555_detail_ds" prompt="签约地点"/>
                <a:comboBox name="dd_agreement_status_desc" bindTarget="con555_detail_ds" prompt="签订扣款协议"/>
                <a:lov name="ebank_id_n" bindTarget="con555_detail_ds" prompt="代扣银行"/>
                <a:comboBox name="ebank_type_n" bindTarget="con555_detail_ds" prompt="代扣账户类型"/>
                <a:textField name="ebank_account_name" bindTarget="con555_detail_ds" prompt="代扣账户名"/>
                <a:textField name="ebank_account_num" bindTarget="con555_detail_ds" prompt="代扣账号"/>
            </a:form>
            <a:switch test="/model/get_sub_condition/record/@flag">
                <a:case value="Y">
                    <a:form column="4" labelWidth="100" title="附条件信息" width="1300">
                        <a:textField name="comment_text" bindTarget="con555_detail_ds" prompt="审批信息" width="500"/>
                    </a:form>
                </a:case>
            </a:switch>
            <a:tabPanel width="1300">
                <a:tabs>
                    <a:tab prompt="法务附件">
                        <a:grid bindTarget="con555_detail_result1_ds" height="350" navBar="true" width="1295">
                            <a:toolBar>
                                <a:button id="add_id" type="add"/>
                                <a:button id="save_id" type="save"/>
                                <a:button type="delete"/>
                            </a:toolBar>
                            <a:columns>
                                <a:column name="description" editorFunction="con555_cdd_editorFunction1" lock="true" prompt="HLS.DOCUMENT_NAME" renderer="con555_cdd_required_render" width="200"/>
                                <a:column name="attachment" align="center" lock="true" prompt="附件" renderer="con555_cdd_attachment_render" width="60"/>
                                <a:column name="file_name" align="center" lock="true" prompt="附件名" renderer="con555_link_render" width="300"/>
                                <a:column name="note" prompt="审核意见1" width="180"/>
                                <a:column name="note2" prompt="审核意见2" width="180"/>
                                <a:column name="note3" prompt="审核意见3" width="180"/>
                                <a:column name="note4" prompt="审核意见4" width="180"/>
                                <a:column name="note5" prompt="审核意见5" width="180"/>
                            </a:columns>
                            <a:editors>
                                <a:textField id="con555_cdd_tf_id1"/>
                            </a:editors>
                        </a:grid>
                    </a:tab>
                    <a:tab prompt="信审附件">
                        <a:grid bindTarget="con555_detail_result_ds" height="350" navBar="true" width="1295">
                            <a:toolBar>
                                <a:button type="add"/>
                                <a:button type="save"/>
                                <a:button type="delete"/>
                                <!-- <a:button click="download" icon="${/request/@context_path}/images/download.png" text="打包下载"/> -->
                            </a:toolBar>
                            <a:columns>
                                <a:column name="description" editorFunction="con555_cdd_editorFunction" lock="true" prompt="HLS.DOCUMENT_NAME" renderer="con555_cdd_required_render" width="200"/>
                                <a:column name="attachment" align="center" lock="true" prompt="附件" renderer="con555_cdd_attachment_render_fw" width="60"/>
                                <a:column name="file_name" align="center" lock="true" prompt="附件名" renderer="con555_link_render" width="300"/>
                                <a:column name="note" prompt="审核意见1" width="180"/>
                                <a:column name="note2" prompt="审核意见2" width="180"/>
                                <a:column name="note3" prompt="审核意见3" width="180"/>
                                <a:column name="note4" prompt="审核意见4" width="180"/>
                                <a:column name="note5" prompt="审核意见5" width="180"/>
                            </a:columns>
                            <a:editors>
                                <!-- <a:checkBox id="con555_cdd_ck_id"/> -->
                                <a:textField id="con555_cdd_tf_id"/>
                            </a:editors>
                        </a:grid>
                    </a:tab>
                </a:tabs>
            </a:tabPanel>
        </a:screenBody>
    </a:view>
</a:screen>
