<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: ZLF  
    $Date: 2014-12-11 下午3:41:07  
    $Revision: 1.0  
    $Purpose: 
-->
<a:screen xmlns:c="leaf.application.action" xmlns:a="http://www.leaf-framework.org/application" xmlns:p="uncertain.proc">
    <a:init-procedure>
        <a:model-query defaultWhereClause="apply_id=${/parameter/@apply_id}" model="cont.CON571.con_contract_add_apply_for_init" rootPath="apply_init"/>
    </a:init-procedure>
    <a:view>
        <a:link id="con571_cdd_uploadFile_id_2" url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="con571_cdd_query_link_2" url="${/request/@context_path}/autocrud/cont.CON572.con_add_atm_query/query"/>
        <a:link id="con571_additional_apply_link_2" url="${/request/@context_path}/modules/cont/CON571/con_contract_additional_apply.lview"/>
        <a:link id="con571_submit_link_2" model="cont.CON571.con_contract_add_submit" modelaction="update"/>
        <a:link id="con571_auditing_link" model="cont.CON571.con_contract_add_auditing" modelaction="update"/>
        <a:link id="con571_get_cdd_list_by_apply_id_2" model="cont.CON571.con_contract_add_apply" modelaction="query"/>
        <script type="text/javascript"><![CDATA[
    		function con571_save() {
    		    $('add_apply_info_ds_2').submit();
    		}
    		
    		function con571_add_apply_submit_2() {
    		    var record=$('add_apply_info_ds_2').getCurrentRecord();
    		    var apply_id=record.get('apply_id');
    		    Leaf.showConfirm('确认','您确定提交吗？',function(){
	    		    Leaf.request({
	    		        url:$('con571_submit_link_2').getUrl(),
	    		        para:{
	    		            apply_id:apply_id
	    		        },
			         	success: function() {
	                        Leaf.SideBar.show({
	                            msg: '提交成功',
	                            duration: 2000
	                        });
	                        Leaf.go($('con571_additional_apply_link_2').getUrl());
	                    },
	                    scope: this   		        
	    		    });
    		    });
    		}
    		
    		function con571_add_apply_download_submit() {
    		    var record=$('add_apply_info_ds_2').getCurrentRecord();
    		    Leaf.showConfirm('确认','您确定提交审核吗？',function(){
    		        Leaf.request({
	    		        url:$('con571_auditing_link').getUrl(),
	    		        para:{
	    		            apply_id:record.get('apply_id')
	    		        },
			         	success: function() {
	                        Leaf.SideBar.show({
	                            msg: '提交审核成功',
	                            duration: 2000
	                        });
	                        Leaf.go($('con571_additional_apply_link_2').getUrl());
	                    },
	                    scope: this   		        
	    		    });
    		    });
    		}
    		
    		function con572_detail_select_func(record) {
    		    if('${/model/apply_init/record/@apply_status}'=='CLOSE') {
                    return false;
                }
    		    if($('add_apply_info_ds_2').getCurrentRecord().get('apply_status')!='NEW') {
    		        return false;
    		    }
                if (record.get('sys_flag') == 'Y') {
                    return false;
                }
                return true;
            }
            
            function description_edit_func(record,name) {
                if(record.isNew) {
                    return 'con571_cdd_tf_id2';
                }else {
                    return '';
                }
            }
    		
    		window['con571_detail_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[\'con571_detail_cdd_attachtment_upload\'](\'' + record.get('check_id') + '\',\'' + file_name + '\',\'' + record_id + '\')">${l:HLS.ATTACHMENT}</a>';
                }
            };
            
            window['con571_detail_cdd_attachtment_upload'] = function(check_id, file_name, record_id) {
                //
                var url = $('con571_cdd_uploadFile_id_2').getUrl() + '?table_name=PRJ_CDD_ITEM_CHECK&header_id=' + check_id;
                var record = $('con571_add_atm_result_detail_ds').getCurrentRecord();
                var oldvalue = file_name ? file_name : null;
                var apply_id = ${/parameter/@apply_id};
                paras = {
                    apply_id: apply_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: $('con571_cdd_query_link_2').getUrl(),
                        para: paras,
                        success: function(data) {
                            //
                            $('con571_add_atm_result_detail_ds').setQueryParameter('apply_id',$('add_apply_info_ds_2').getCurrentRecord().get('apply_id'));
                            $('con571_add_atm_result_detail_ds').query();
            
                            //var newvalue = $('con555_detail_result_ds').findById(record_id).get('file_name');
                        },
                        sync: true
                    });           
                });
            
            };
            
            window['con571_detail_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; 
                }
            };
            
            function atm_detail_add_handler(ds,record,index) {
                if('${/model/apply_init/record/@apply_status}'=='CLOSE') {
                    Leaf.showMessage('提示','该申请已关闭，不能添加附件！');
                    $('con571_add_atm_result_detail_ds').remove(record);
                    return;
                }
                if(Leaf.isEmpty($('add_apply_info_ds_2').getCurrentRecord().get('apply_id'))) {
                    Leaf.showMessage('提示','请先保存再添加附件！');
                    $('con571_add_atm_result_detail_ds').remove();
                    return;
                }
                record.set('document_id',$('add_apply_info_ds_2').getCurrentRecord().get('apply_id'));
                Leaf.request({
                    url:$('con571_get_cdd_list_by_apply_id_2').getUrl(),
                    para:{
                        apply_id:$('add_apply_info_ds_2').getCurrentRecord().get('apply_id')
                    },
                    success:function(res) {
                        record.set('cdd_list_id',res.result.record['cdd_list_id']);
                    }
                });
            }
    	]]></script>
        <a:dataSets>
            <a:dataSet id="con571_add_atm_result_detail_ds" autoQuery="true" model="cont.CON572.con_add_atm_query" pageSize="10" queryUrl="${/request/@context_path}/autocrud/cont.CON572.con_add_atm_query/query?apply_id=${/parameter/@apply_id}" selectFunction="con572_detail_select_func" 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/@apply_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:events>
                    <a:event name="add" handler="atm_detail_add_handler"/>
                </a:events>
            </a:dataSet>
            <a:dataSet id="add_apply_info_ds_2" autoCreate="true" loadData="true" model="cont.CON571.con_contract_add_apply" queryUrl="${/request/@context_path}/autocrud/cont.CON571.con_contract_add_apply/query?apply_id=${/parameter/@apply_id}">
                <a:fields>
                    <a:field name="bp_code" autoComplete="true" lovGridHeight="350" lovHeight="500" lovLabelWidth="100" lovService="basic.hls_bp_master_v_for_lov?bp_category=TENANT" lovWidth="550" readOnly="true" title="HLS.BP_TITLE">
                        <a:mapping>
                            <a:map from="bp_code" to="bp_code"/>
                            <a:map from="bp_id" to="bp_id_tenant"/>
                            <a:map from="bp_name" to="bp_name"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="bp_name" readOnly="true"/>
                    <a:field name="contract_number" autoComplete="true" autoCompleteField="contract_number" lovGridHeight="300" lovHeight="450" lovService="cont.CON500.con_contract_number_lov" lovWidth="500" readOnly="true" title="HLS.CONTRACT_NUMBER">
                        <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:mapping>
                    </a:field>
                    <a:field name="contract_id"/>
                    <a:field name="contract_name" readOnly="true"/>
                    <a:field name="legal_opinion" readOnly="true"/>
                </a:fields>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <a:gridButton click="con571_save" text="保存"/>
                <a:placeHolder id="button_ph"/>
            </a:screenTopToolbar>
            <a:form column="1" marginWidth="60" title="申请信息">
                <a:hBox labelWidth="100">
                    <a:lov name="bp_code" bindTarget="add_apply_info_ds_2" prompt="HLS.TENANT_CODE"/>
                    <a:textField name="bp_name" bindTarget="add_apply_info_ds_2" prompt="HLS.TENANT_NAME"/>
                    <a:lov name="contract_number" bindTarget="add_apply_info_ds_2" prompt="HLS.CONTRACT_NUMBER"/>
                    <a:textField name="contract_name" bindTarget="add_apply_info_ds_2" prompt="HLS.CONTRACT_NAME"/>
                </a:hBox>
                <a:hBox labelWidth="100">
                    <a:textArea name="legal_opinion" bindTarget="add_apply_info_ds_2" prompt="法务备注" width="405"/>
                </a:hBox>
            </a:form>
            <a:tabPanel marginWidth="30">
                <a:tabs>
                    <a:tab prompt="申请说明">
                        <a:form height="250" labelWidth="100" marginWidth="60" title="申请说明">
                            <a:textArea name="apply_desc" bindTarget="add_apply_info_ds_2" height="240" prompt="申请说明" width="665"/>
                        </a:form>
                    </a:tab>
                    <a:tab marginWidth="50" prompt="附件">
                        <a:grid bindTarget="con571_add_atm_result_detail_ds" height="250" marginWidth="60" navBar="true">
                            <a:toolBar>
                                <a:button type="add"/>
                                <a:button type="save"/>
                                <a:button type="delete"/>
                            </a:toolBar>
                            <a:columns>
                                <a:column name="description" editorFunction="description_edit_func" lock="true" prompt="HLS.DOCUMENT_NAME" width="200"/>
                                <a:column name="attachment" align="center" lock="true" prompt="附件" renderer="con571_detail_cdd_attachment_render" width="60"/>
                                <a:column name="file_name" align="center" lock="true" prompt="附件名" renderer="con571_detail_link_render" width="400"/>
                                <a:column name="note" editor="con571_cdd_tf_id2" prompt="备注" width="200"/>
                            </a:columns>
                            <a:editors>
                                <a:textField id="con571_cdd_tf_id2"/>
                            </a:editors>
                        </a:grid>
                    </a:tab>
                </a:tabs>
            </a:tabPanel>
        </a:screenBody>
    </a:view>
    <a:view-config>
        <c:create-config targetId="button_ph">
            <p:loop source="/model/apply_init">
                <p:switch test="@apply_status">
                    <p:case value="NEW">
                        <c:process-config>
                            <a:gridButton click="con571_add_apply_submit_2" text="提交"/>
                        </c:process-config>
                    </p:case>
                    <p:case value="REJECTED">
                        <c:process-config>
                            <a:gridButton click="con571_add_apply_submit_2" text="提交"/>
                        </c:process-config>
                    </p:case>
                    <p:case value="GENERATED">
                        <c:process-config>
                            <a:gridButton click="con571_add_apply_download_submit" text="提交审核"/>
                        </c:process-config>
                    </p:case>
                    <p:case value="CLOSE">
                        <c:process-config>
                            <script type="text/javascript"><![CDATA[
                    			function setReadOnly() {
                    			    $('add_apply_info_ds_2').getCurrentRecord().getField('legal_opinion').setReadOnly(true);
                    			    $('add_apply_info_ds_2').getCurrentRecord().getField('apply_desc').setReadOnly(true);
                    			}
                    			
                    			setReadOnly();
                    		]]></script>
                        </c:process-config>
                    </p:case>
                </p:switch>
            </p:loop>
        </c:create-config>
    </a:view-config>
</a:screen>
