<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: LR  
    $Date: 2013-8-23 下午02:08:12  
    $Revision: 1.0  
    $Purpose: 
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true" trace="true">
    <a:init-procedure>
        <a:model-query defaultWhereClause="t1.document_category=&apos;CONTRACT&apos;" fetchAll="true" model="basic.hls_document_type_for_lov" rootPath="prj505_con_document_type_path"/>
        <a:model-query defaultWhereClause="t1.project_id=${/parameter/@project_id}" fetchAll="true" model="cont.CON500.con_contract_v" rootPath="prj505_con_contract_lov_path"/>
        <a:model-query defaultWhereClause="t1.enabled_flag = &apos;Y&apos;" fetchAll="true" model="prj.PRJ505.con_billing_method" rootPath="billing_method_list"/>
    </a:init-procedure>
    <a:view>
        <a:link id="prj505_project_create_con_lease_item_lov_link" url="${/request/@context_path}/modules/prj/PRJ505/prj_project_create_con_lease_item_lov.lview"/>
        <a:link id="prj505_project_guarantor_lov_link" url="${/request/@context_path}/modules/prj/PRJ505/prj_project_guarantor_lov.lview"/>
        <a:link id="prj505_project_mortgage_lov_link" url="${/request/@context_path}/modules/prj/PRJ505/prj_project_create_con_mortgage_lov.lview"/>
        <!-- <a:link id="prj505_update_project_status_link" model="prj.PRJ501.prj_project_v" modelaction="execute"/> -->
        <a:link id="con_contract_update_print_detail_link_id" url="${/request/@context_path}/modules/cont/CON500/con_contract_update_print_detail.lview"/>
        <script type="text/javascript"><![CDATA[
            function prj505_project_create_con_close() {
                $('prj505_project_create_contract_window').close();
            }
            
            function prj505_project_create_con_create() {
                var records = $('prj505_project_create_con_ds').getSelected();
                $('prj505_project_create_con_ds').submitSelected();
            }
            
            // function prj505_project_update_project_status() {
                // Leaf.showConfirm('${l:HLS.PROMPT}', '${l:PRJ505.CONTRACT_CREATED_CONFIRM}', function() {
                    // Leaf.request({
                        // url: $('prj505_update_project_status_link').getUrl(),
                        // para: {
                            // project_id: '${/parameter/@project_id}'
                        // },
                        // scope: this,
                        // success: function() {
                            // $('projectQueryScreen_mainDs_grid').clear();
                            // prj505_project_create_con_close();
                        // }
                    // });
                // });
            // }
            
            function prj505_project_create_con_ondLoad(ds) {
                $('prj505_project_lease_item_ds').query();
                $('prj505_project_guarantor_ds').query();
                $('prj505_project_mortgage_ds').query();
                var records = ds.getAll();
                var record = $('projectQueryScreen_mainDs').getCurrentRecord();
                for (var i = 0;i < records.length;i++) {
                    records[i].set('contract_name', record.get('project_name'), true);
                }
                set_create_con_create_btn();
            }
            
            function prj505_project_guarantor_ondLoad(ds) {
                var records = $('prj505_project_create_con_ds').getAll();
                var guarantorRecords = ds.getAll();
                for (var i = 0;i < records.length;i++) {
                    var guarantor = '';
                    for (var j = 0;j < guarantorRecords.length;j++) {
                        if (guarantorRecords[j].get('contract_seq') == records[i].get('contract_seq')) {
                            if (guarantor == '') {
                                guarantor = guarantorRecords[j].get('bp_name')== 'undefined' ? '' : guarantorRecords[j].get('bp_name');
                            } else {
                                guarantor = guarantor + ',' + guarantorRecords[j].get('bp_name')== 'undefined' ? '' : guarantorRecords[j].get('bp_name');
                            }
            
                        }
                    }
                    records[i].set('guarantor', guarantor, true);
                }
            }
            
            function prj505_project_lease_item_ondLoad(ds) {
                var records = $('prj505_project_create_con_ds').getAll();
                var leaseRecords = ds.getAll();
                for (var i = 0;i < records.length;i++) {
                    var lease_item = '';
                    for (var j = 0;j < leaseRecords.length;j++) {
                        if (leaseRecords[j].get('contract_seq') == records[i].get('contract_seq')) {
                            if (lease_item == '') {
                                lease_item = leaseRecords[j].get('full_name') == 'undefined' ? '' : leaseRecords[i].get('full_name');
                                //records[i].get('full_name') == 'undefined' ? '' : records[i].get('full_name');
                            } else {
                                lease_item = lease_item + ',' + leaseRecords[j].get('full_name') == 'undefined' ? '' : leaseRecords[i].get('full_name');
                            }
            
                        }
                    }
                    records[i].set('lease_item', lease_item, true);
                }
            
            }
            
            function prj505_project_mortgage_ondLoad(ds) {
                var records = $('prj505_project_create_con_ds').getAll();
                var leaseRecords = ds.getAll();
                for (var i = 0;i < records.length;i++) {
                    var mortgage = '';
                    for (var j = 0;j < leaseRecords.length;j++) {
                        if (leaseRecords[j].get('contract_seq') == records[i].get('contract_seq')) {
                            if (mortgage == '') {
                                mortgage = leaseRecords[j].get('mortgage_name') == 'undefined' ? '' : leaseRecords[i].get('mortgage_name');
                            } else {
                                mortgage = mortgage + ',' + leaseRecords[j].get('mortgage_name') == 'undefined' ? '' : leaseRecords[i].get('mortgage_name');
                            }
            
                        }
                    }
                    records[i].set('mortgage', mortgage, true);
                }
            
            }
            
            function prj505_project_create_con_grid_cellclick(grid, row, name, record) {
                if (!Leaf.isEmpty(record.get('contract_seq'))) {
                    if (name == 'lease_item') {
                        record.getField('lease_item').setLovUrl($('prj505_project_create_con_lease_item_lov_link').getUrl() + '?project_id=' + '${/parameter/@project_id}' + '&contract_seq=' + record.get('contract_seq'));
                    } else if (name == 'guarantor') {
                        record.getField('guarantor').setLovUrl($('prj505_project_guarantor_lov_link').getUrl() + '?project_id=' + '${/parameter/@project_id}' + '&contract_seq=' + record.get('contract_seq'));
                    }else if (name == 'mortgage') {
                        record.getField('mortgage').setLovUrl($('prj505_project_mortgage_lov_link').getUrl() + '?project_id=' + '${/parameter/@project_id}' + '&contract_seq=' + record.get('contract_seq'));
                    }
                } else {
                    record.getField('lease_item').setLovUrl('');
                }
            }
            
            function prj505_project_create_con_submitsuccess(ds, res) {
                $('prj505_project_create_con_grid').clear();
                // $('prj505_con_contract_ds').query();
                if (Leaf.isEmpty(res.result.record[0])) {
                    var record = new Leaf.Record({
                        contract_id: res.result.record.contract_id,
                        contract_number: res.result.record.contract_number,
                        contract_name: res.result.record.contract_name,
                        bp_name: res.result.record.bp_name
                    });
                    $('prj505_con_contract_ds').create(record.data);
                    Leaf.SideBar.show({
                        msg: res.result.record.contract_number,
                        duration: 2000
                    });
                }
            }
            
            function set_btn_style(id, dis_value) {
                Ext.get(id).setStyle({
                    display: dis_value
                });
            }
            
            function set_created_con_ok_btn() {
                var records = $('prj505_con_contract_ds').getAll();
                if (records.length == 0) {
                    set_btn_style('prj505_created_con_ok_id', 'none');
                } else {
                    set_btn_style('prj505_created_con_ok_id', '');
                }
            }
            
            function set_create_con_create_btn() {
                var pre_records = $('prj505_project_create_con_ds').getAll();
                if (pre_records.length == 0) {
                    set_btn_style('prj505_project_create_con_create_id', 'none');
                } else {
                    set_btn_style('prj505_project_create_con_create_id', '');
                }
            }
            
            function on_prj505_tab_select(tab, index) {
                set_create_con_create_btn();
                set_created_con_ok_btn();
            }
            
            function prj505_project_create_con_grid_clear(){
                $('prj505_project_create_con_grid').clear();
            }
            
            function con505_con_contract_content_print() {
                var records = $('prj505_con_contract_ds').getSelected();
                if (records.length != 1) {
                    Leaf.showMessage('${l:PROMPT}','${l:HLS.SELECT_RECORD}');
                    return;
                }
                var record=records[0];
                new Leaf.Window({
                    id: 'con_contract_update_print_detail_winid',
                    params: {
                        contract_id: record.get('contract_id'),
                        document_category:record.get('document_category'),
                        business_type :record.get('business_type'),
                        winid: 'con_contract_update_print_detail_winid',
                        content_type : 'NORMAL'
                    },
                    url: $('con_contract_update_print_detail_link_id').getUrl(),
                    title: '${l:CON505.CON_CONTENT_PRINT}',
                    width:900,
                    height:450
                });
            }
            
        ]]></script>
        <a:dataSets>
            <a:dataSet id="prj505_con_contract_ds" selectable="true" selectionModel="single">
                <a:datas dataSource="/model/prj505_con_contract_lov_path"/>
            </a:dataSet>
            <a:dataSet id="prj505_con_document_type_ds">
                <a:datas dataSource="/model/prj505_con_document_type_path"/>
            </a:dataSet>
            <a:dataSet id="prj505_billing_method_ds">
                <a:datas dataSource="/model/billing_method_list"/>
            </a:dataSet>
            <a:dataSet id="prj505_query_ds">
                <a:fields>
                    <a:field name="project_id" defaultValue="${/parameter/@project_id}"/>
                </a:fields>
            </a:dataSet>
            <a:dataSet id="prj505_project_create_con_ds" autoPageSize="true" autoQuery="true" fetchAll="true" model="prj.PRJ505.prj_project_create_contract" queryDataSet="prj505_query_ds" selectable="true" selectionModel="single">
                <a:fields>
                    <a:field name="contract_seq" required="true"/>
                    <a:field name="bp_name" autoComplete="true" lovGridHeight="350" lovHeight="500" lovService="prj.PRJ501.prj_project_bp_lov?project_id=${/parameter/@project_id}" lovWidth="500" required="true" title="HLS.TENANT_CODE">
                        <a:mapping>
                            <a:map from="bp_id" to="bp_id"/>
                            <a:map from="bp_name" to="bp_name"/>
                            <a:map from="contract_seq" to="bp_contract_seq"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="bp_contract_seq" required="true"/>
                    <a:field name="lease_item" lovHeight="500" lovWidth="500" prompt="HLS.LEASE_ITEM" title="HLS.LEASE_ITEM"/>
                    <a:field name="mortgage" lovHeight="500" lovWidth="500" prompt="HLS.MORTGAGE" title="HLS.MORTGAGE"/>
                    <a:field name="guarantor" lovHeight="500" lovWidth="650" title="GUARANTOR"/>
                    <a:field name="billing_method_desc" displayField="description" options="prj505_billing_method_ds" required="true" returnField="billing_method" valueField="billing_method"/>
                    <a:field name="contract_type_desc" displayField="description" options="prj505_con_document_type_ds" required="true" returnField="contract_type" valueField="document_type"/>
                    <a:field name="contract_name" required="true"/>
                </a:fields>
                <a:events>
                    <a:event name="load" handler="prj505_project_create_con_ondLoad"/>
                    <a:event name="submitsuccess" handler="prj505_project_create_con_submitsuccess"/>
                </a:events>
            </a:dataSet>
            <a:dataSet id="prj505_project_lease_item_ds" autoQuery="true" fetchAll="true" model="prj.PRJ505.prj_project_create_con_lease_item_lov" queryDataSet="prj505_query_ds" selectable="true">
                <a:fields>
                    <a:field name="check_flag" checkedValue="Y" defaultValue="N" uncheckedValue="N"/>
                </a:fields>
                <a:events>
                    <a:event name="load" handler="prj505_project_lease_item_ondLoad"/>
                </a:events>
            </a:dataSet>
            <a:dataSet id="prj505_project_mortgage_ds" autoQuery="true" fetchAll="true" model="prj.PRJ505.prj_project_create_con_mortgage_lov" queryDataSet="prj505_query_ds" selectable="true">
                <a:fields>
                    <a:field name="check_flag" checkedValue="Y" defaultValue="N" uncheckedValue="N"/>
                </a:fields>
                <a:events>
                    <a:event name="load" handler="prj505_project_mortgage_ondLoad"/>
                </a:events>
            </a:dataSet>
            <a:dataSet id="prj505_project_guarantor_ds" autoQuery="true" fetchAll="true" model="prj.PRJ505.prj_project_guarantor_lov" queryDataSet="prj505_query_ds" selectable="true">
                <a:fields>
                    <a:field name="check_flag" checkedValue="Y" defaultValue="N" uncheckedValue="N"/>
                </a:fields>
                <a:events>
                    <a:event name="load" handler="prj505_project_guarantor_ondLoad"/>
                </a:events>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:gridButton click="prj505_project_create_con_close" text="HLS.CLOSE"/>
                <a:gridButton click="prj505_project_create_con_grid_clear" text="HLS.CLEAR" type="clear"/>
                <a:gridButton id="prj505_project_create_con_create_id" click="prj505_project_create_con_create" text="HLS.CREATE"/>
                <!-- <a:gridButton id="prj505_created_con_ok_id" click="prj505_project_update_project_status" text="PRJ505.CONTRACT_CREATED"/> -->
                <!-- <a:gridButton id="prj505_created_con_content_id" click="con505_con_contract_content_print" text="合同文本生成"/> -->
            </a:screenTopToolbar>
            <a:form column="3" labelSeparator=" " labelWidth="100">
                <a:textField name="project_number" bindTarget="projectQueryScreen_mainDs" prompt="HLS.PROJECT_NUMBER" readOnly="true"/>
                <a:textField name="project_name" bindTarget="projectQueryScreen_mainDs" prompt="HLS.PROJECT_NAME" readOnly="true" width="250"/>
                <a:textField name="approved_date" bindTarget="projectQueryScreen_mainDs" prompt="PRJ505.APPROVED_DATE" readOnly="true"/>
            </a:form>
            <a:tabPanel marginHeight="180" marginWidth="50">
                <a:tabs>
                    <a:tab prompt="PRJ505.CON_CREATE" width="100">
                        <a:grid id="prj505_project_create_con_grid" bindTarget="prj505_project_create_con_ds" marginHeight="230" marginWidth="55">
                            <a:columns>
                                <a:column name="contract_seq" prompt="PRJ505.CONTRACT_SEQ" width="100"/>
                                <a:column name="contract_name" editor="prj505_project_create_con_grid_tf" prompt="HLS.CONTRACT_NAME" width="180"/>
                                <a:column name="bp_name" editor="prj505_project_create_con_grid_lov" prompt="HLS.TENANT_CODE" width="150"/>
                                <a:column name="lease_item" editor="prj505_project_create_con_grid_lov" prompt="HLS.LEASE_ITEM" width="220"/>
                                <a:column name="mortgage" editor="prj505_project_create_con_grid_lov" prompt="HLS.MORTGAGE" width="220"/>
                                <a:column name="guarantor" editor="prj505_project_create_con_grid_lov" prompt="PRJ305.GUARANTOR" width="220"/>
                                <a:column name="version" prompt="PRJ505.VERSION" width="90"/>
                                <a:column name="contract_type_desc" editor="prj505_project_create_con_grid_cb" prompt="HLS.CONTRACT_TYPE" width="120"/>
                                <a:column name="billing_method_desc" editor="prj505_project_create_con_grid_cb" prompt="PRJ505.BILLING_METHOD" width="100"/>
                            </a:columns>
                            <a:editors>
                                <a:textField id="prj505_project_create_con_grid_tf"/>
                                <a:lov id="prj505_project_create_con_grid_lov"/>
                                <a:comboBox id="prj505_project_create_con_grid_cb"/>
                            </a:editors>
                            <a:events>
                                <a:event name="cellclick" handler="prj505_project_create_con_grid_cellclick"/>
                            </a:events>
                        </a:grid>
                    </a:tab>
                    <a:tab prompt="PRJ505.CREATED_CON" width="100">
                        <a:grid id="prj505_con_contract_grid" bindTarget="prj505_con_contract_ds" marginHeight="230" marginWidth="55">
                            <a:columns>
                                <a:column name="contract_number" prompt="HLS.CONTRACT_NUMBER" width="150"/>
                                <a:column name="contract_name" prompt="HLS.CONTRACT_NAME" width="300"/>
                                <a:column name="bp_name" prompt="HLS.TENANT_CODE" width="250"/>
                            </a:columns>
                        </a:grid>
                    </a:tab>
                </a:tabs>
                <a:events>
                    <a:event name="select" handler="on_prj505_tab_select"/>
                </a:events>
            </a:tabPanel>
        </a:screenBody>
    </a:view>
</a:screen>