<?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="con572_cdd_uploadFile_id" url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="con572_cdd_query_link" url="${/request/@context_path}/autocrud/cont.CON572.con_add_atm_query/query"/>
        <a:link id="con572_reject_link" model="cont.CON572.con_contract_add_reject" modelaction="execute"/>
        <a:link id="con572_generate_link" model="cont.CON572.con_contract_add_generate" modelaction="update"/>
        <a:link id="con572_regenerate_link" model="cont.CON572.con_contract_add_regenerate" modelaction="update"/>
        <a:link id="con572_approved_link" model="cont.CON572.con_contract_add_approved" modelaction="update"/>
        <a:link id="con572_send_back_link" model="cont.CON572.con_contract_add_send_back" modelaction="update"/>
        <a:link id="con572_close_link" model="cont.CON572.con_contract_add_close" modelaction="update"/>
        <a:link id="con572_additional_confirm_link" url="${/request/@context_path}/modules/cont/CON572/con_contract_additianal_confirm.lview"/>
        <a:link id="con572_get_cdd_list_by_apply_id" model="cont.CON571.con_contract_add_apply" modelaction="query"/>
        <script type="text/javascript"><![CDATA[
    		
    		function con572_save() {
    		    $('add_apply_info_ds').submit();
    		}
    		
    		function con572_select_func(record) {
    		    if('${/model/apply_init/record/@apply_status}'=='CLOSE') {
                    return false;
                }
    		    if($('add_apply_info_ds').getCurrentRecord().get('apply_status')!='SUBMITED') {
    		        return false;
    		    }
                if (record.get('sys_flag') == 'Y') {
                    return false;
                }
                return true;
            }
            
            function description_edit_func(record,name) {
                if(record.isNew) {
                    return 'con572_cdd_tf_id1';
                }else {
                    return '';
                }
            }
            
            function con572_add_apply_reject() {
                Leaf.showConfirm('确认','您确定拒绝吗？',function(){
	    		    Leaf.request({
	    		        url:$('con572_reject_link').getUrl(),
	    		        para:{
	    		            apply_id:'${/parameter/@apply_id}'
	    		        },
			         	success: function() {
	                        Leaf.SideBar.show({
	                            msg: '已拒绝',
	                            duration: 2000
	                        });
	                        Leaf.go($('con572_additional_confirm_link').getUrl());
	                    },
	                    scope: this   		        
	    		    });
    		    });
            }
            
            function con572_add_apply_generate() {
                Leaf.showConfirm('确认','您确定生成吗？',function(){
	    		    Leaf.request({
	    		        url:$('con572_generate_link').getUrl(),
	    		        para:{
	    		            apply_id:'${/parameter/@apply_id}'
	    		        },
			         	success: function() {
	                        Leaf.SideBar.show({
	                            msg: '生成成功',
	                            duration: 2000
	                        });
	                        Leaf.go($('con572_additional_confirm_link').getUrl());
	                    },
	                    scope: this   		        
	    		    });
    		    });
            }
            
            function con572_add_apply_regenerate() {
                Leaf.showConfirm('确认','您确定重新生成吗？',function(){
	    		    Leaf.request({
	    		        url:$('con572_regenerate_link').getUrl(),
	    		        para:{
	    		            apply_id:'${/parameter/@apply_id}'
	    		        },
			         	success: function() {
	                        Leaf.SideBar.show({
	                            msg: '重新生成成功',
	                            duration: 2000
	                        });
	                        Leaf.go($('con572_additional_confirm_link').getUrl());
	                    },
	                    scope: this   		        
	    		    });
    		    });
            }
            
            function con572_add_apply_approved() {
                Leaf.showConfirm('确认','您确定通过吗？',function(){
	    		    Leaf.request({
	    		        url:$('con572_approved_link').getUrl(),
	    		        para:{
	    		            apply_id:'${/parameter/@apply_id}'
	    		        },
			         	success: function() {
	                        Leaf.SideBar.show({
	                            msg: '已通过',
	                            duration: 2000
	                        });
	                        Leaf.go($('con572_additional_confirm_link').getUrl());
	                    },
	                    scope: this   		        
	    		    });
    		    });
            }
            
            function con572_add_apply_send_back() {
                Leaf.showConfirm('确认','您确定退回吗？',function(){
	    		    Leaf.request({
	    		        url:$('con572_send_back_link').getUrl(),
	    		        para:{
	    		            apply_id:'${/parameter/@apply_id}'
	    		        },
			         	success: function() {
	                        Leaf.SideBar.show({
	                            msg: '已退回',
	                            duration: 2000
	                        });
	                        Leaf.go($('con572_additional_confirm_link').getUrl());
	                    },
	                    scope: this   		        
	    		    });
    		    });
            }
            
            function con572_add_apply_close() {
                Leaf.showConfirm('确认','您确定关闭吗？',function(){
	    		    Leaf.request({
	    		        url:$('con572_close_link').getUrl(),
	    		        para:{
	    		            apply_id:'${/parameter/@apply_id}'
	    		        },
			         	success: function() {
	                        Leaf.SideBar.show({
	                            msg: '已关闭',
	                            duration: 2000
	                        });
	                        Leaf.go($('con572_additional_confirm_link').getUrl());
	                    },
	                    scope: this   		        
	    		    });
    		    });
            }
            
            window['con572_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[\'con572_cdd_attachtment_upload\'](\'' + record.get('check_id') + '\',\'' + file_name + '\',\'' + record_id + '\')">${l:HLS.ATTACHMENT}</a>';
                }
            };
            
            
            window['con572_cdd_attachtment_upload'] = function(check_id, file_name, record_id) {
                //
                var url = $('con572_cdd_uploadFile_id').getUrl() + '?table_name=PRJ_CDD_ITEM_CHECK&header_id=' + check_id;
                var record = $('con572_add_atm_result_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: 'con572_cdd_uploadFile_screen_id',
                    width: 850,
                    height: 400
                });
                win.on('close', function() {
                    //
                    Leaf.request({
                        url: $('con572_cdd_query_link').getUrl(),
                        para: paras,
                        success: function(data) {
                            //
                            $('con572_add_atm_result_ds').setQueryParameter('apply_id',$('add_apply_info_ds').getCurrentRecord().get('apply_id'));
                            $('con572_add_atm_result_ds').query();
            
                            //var newvalue = $('con555_detail_result_ds').findById(record_id).get('file_name');
                        },
                        sync: true
                    });           
                });
            
            };
            
            window['con572_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('提示','该申请已关闭，不能添加附件！');
                    $('con572_add_atm_result_ds').remove(record);
                    return;
                }
                if(Leaf.isEmpty($('add_apply_info_ds').getCurrentRecord().get('apply_id'))) {
                    Leaf.showMessage('提示','请先保存再添加附件！');
                    $('con572_add_atm_result_ds').remove(record);
                    return;
                }               
                record.set('document_id',$('add_apply_info_ds').getCurrentRecord().get('apply_id'));
                Leaf.request({
                    url:$('con572_get_cdd_list_by_apply_id').getUrl(),
                    para:{
                        apply_id:$('add_apply_info_ds').getCurrentRecord().get('apply_id')
                    },
                    success:function(res) {
                        record.set('cdd_list_id',res.result.record['cdd_list_id']);
                    }
                });
            }
    	]]></script>
        <a:dataSets>
            <a:dataSet id="con572_add_atm_result_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_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" 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"/>
                </a:fields>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <a:gridButton click="con572_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" prompt="HLS.TENANT_CODE"/>
                    <a:textField name="bp_name" bindTarget="add_apply_info_ds" prompt="HLS.TENANT_NAME"/>
                    <a:lov name="contract_number" bindTarget="add_apply_info_ds" prompt="HLS.CONTRACT_NUMBER"/>
                    <a:textField name="contract_name" bindTarget="add_apply_info_ds" prompt="HLS.CONTRACT_NAME"/>
                </a:hBox>
                <a:hBox labelWidth="100">
                    <a:textArea name="legal_opinion" bindTarget="add_apply_info_ds" 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" height="240" prompt="申请说明" width="665"/>
                        </a:form>
                    </a:tab>
                    <a:tab marginWidth="50" prompt="附件">
                        <a:grid bindTarget="con572_add_atm_result_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="con572_cdd_attachment_render" width="60"/>
                                <a:column name="file_name" align="center" lock="true" prompt="附件名" renderer="con572_detail_link_render" width="400"/>
                                <a:column name="note" editor="con572_cdd_tf_id1" prompt="备注" width="200"/>
                            </a:columns>
                            <a:editors>
                                <a:textField id="con572_cdd_tf_id1"/>
                            </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="SUBMITED">
                        <c:process-config>
                            <a:gridButton click="con572_add_apply_reject" text="拒绝"/>
                            <a:gridButton click="con572_add_apply_generate" text="生成"/>
                            <a:gridButton click="con572_add_apply_regenerate" text="重新生成"/>
                            <a:gridButton click="con572_add_apply_close" text="关闭"/>
                        </c:process-config>
                    </p:case>
                    <p:case value="GENERATED">
                        <c:process-config>
                            <a:gridButton click="con572_add_apply_reject" text="拒绝"/>
                            <a:gridButton click="con572_add_apply_regenerate" text="重新生成"/>
                            <a:gridButton click="con572_add_apply_approved" text="通过"/>
                            <a:gridButton click="con572_add_apply_close" text="关闭"/>
                        </c:process-config>
                    </p:case>
                    <p:case value="AUDITING">
                        <c:process-config>
                            <a:gridButton click="con572_add_apply_approved" text="通过"/>
                            <a:gridButton click="con572_add_apply_send_back" text="审核退回"/>
                            <a:gridButton click="con572_add_apply_close" text="关闭"/>
                        </c:process-config>
                    </p:case>
                    <p:case value="CLOSE">
                        <c:process-config>
                            <script type="text/javascript"><![CDATA[
                    			function setReadOnly() {
                    			    $('add_apply_info_ds').getCurrentRecord().getField('legal_opinion').setReadOnly(true);
                    			    $('add_apply_info_ds').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>
