<?xml version="1.0" encoding="UTF-8"?>
<!--
    合同变更
        租金計劃變更
        承租人變更
        回款賬戶變更
 -->
<a:screen xmlns:s="leaf.plugin.script" xmlns:a="http://www.leaf-framework.org/application"
          customizationEnabled="true" dynamiccreateenabled="true" trace="true">
    <a:init-procedure>
        <a:model-query  defaultWhereClause="t1.role_id=${/session/@role_id}" fetchAll="true" model="cont.CON733.query_roles_info" rootPath="role_code"/>
    </a:init-procedure>
    <a:view>
        <a:link id="con_contract_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code"
                modelaction="update"/>
        <a:link id="con_contract_modify_link"
                url="${/request/@context_path}/modules/cont/CON505/con_contract_modify.lview"/>
        <a:link id="con_contract_change_link"
                url="${/request/@context_path}/modules/cont/CON731/hls_con_contract_change_detail.lview"/>
        <a:link id="con733_create_change_link" model="cont.CON733.create_change_req" modelaction="execute"/>
        <a:link id="con_contract_change_req_link"
                url="${/request/@context_path}/modules/cont/CON701/con_contract_et_print.lsc"/>
        <script type="text/javascript"><![CDATA[

        //合同变更创建：营业内勤只能选到回款账户变更，
        // 债权担当只能选到承租人变更，
        // 代理店下级只能选到租金计划变更；
        Leaf.onReady(function(){
            var role_code='${/model/role_code/record/@role_code}';
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_change_req');
            var ds_id_n = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
            var head_record = $(ds_id).getAt(0);
            if(head_record){
                if(role_code=='0007'){
                    head_record.set('ccr_document_type','ACC_CHAG');
                    head_record.set('ccr_document_type_n','回款账户变更');
                    head_record.getField('ccr_document_type').setReadOnly(true);
                    head_record.getField('ccr_document_type_n').setReadOnly(true);
                    /*$(ds_id_n).setQueryParameter('contract_status', '');
                    $(ds_id_n).query();*/
                }else if(role_code=='0019'){
                    head_record.set('ccr_document_type','LEASE_CHAG');
                    head_record.set('ccr_document_type_n','承租人变更');
                    head_record.getField('ccr_document_type').setReadOnly(true);
                    head_record.getField('ccr_document_type_n').setReadOnly(true);
                    /*$(ds_id_n).setQueryParameter('contract_status', 'INCEPT');
                    $(ds_id_n).query();*/
                }
                else if(role_code=='0008'){
                    head_record.set('ccr_document_type','CUT_CHAG');
                    head_record.set('ccr_document_type_n','租金计划变更');
                    head_record.getField('ccr_document_type').setReadOnly(true);
                    head_record.getField('ccr_document_type_n').setReadOnly(true);
                    /*$(ds_id_n).setQueryParameter('contract_status', 'INCEPT');
                    $(ds_id_n).query();*/
                }
            }
        });
        // 更新事件
        window['${/parameter/@layout_code}_on_layout_dynamic_update'] = function(ds, record, name, value, old_value, bp_seq) {
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
            if(name == 'ccr_document_type' && value){
                if(value != 'ACC_CHAG'){
                    $(ds_id).setQueryParameter('contract_status', 'INCEPT');
                    $(ds_id).query();
                }
                if(value == 'ACC_CHAG'){
                    $(ds_id).setQueryParameter('contract_status', '');
                    $(ds_id).query();
                }
            }
        };
        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['download'] = 'Y';
            param['maintain_type'] = 'UPDATE';
            param['url_title'] = '${l:CON301.CONTRACT_DETAIL}';
            hls_doc_get_layout_code('con_contract_get_layout_code_link_id', param, 'con_contract_modify_link', ds_id);
        }
        window['${/parameter/@layout_code}_dynamic_link_renderer'] = function (value, record, name, config_record) {
            if (name == 'contract_number' && value) {
                return '<a href="javascript:open_contract_win(\'' + record.ds.id + '\',\'' + record.id + '\')">' + value + '</a>';
            }
            return value;
        };
        window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function () {
           // $('${/parameter/@layout_code}_user_button1').disable(); //按钮不可用
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');
            var prj_project_result_ds = $(ds_id);
            var records = prj_project_result_ds.getSelected();
            if (records.length != 1) {
                Leaf.showMessage('${l:PROMPT}', '${l:HLS.SELECT_RECORD}');
                return;
            }
            Leaf.showConfirm('特别注意', '<span style="color: #ff0000; ">该操作将导致合同在变更期间无法进行后续操作，请谨慎操作！！！如果需要取消变更，请一定点击取消变更按钮！！！</span>', function () {
                var record = records[0];

                var head_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_change_req');
                var head_record = $(head_ds_id).getAt(0);
                if ($(head_ds_id).validate($('${/parameter/@layout_code}_user_button1').enable())) {
                    var contract = record.get('contract_id');
                    var ccr_document_type = head_record.get('ccr_document_type');
                    var req_date = head_record.get('req_date');
                    var description = head_record.get('description');
                    var ref_v01 = head_record.get('ref_v01');
                    Leaf.Masker.mask(Ext.getBody());
                    Leaf.request({
                        url: $('con733_create_change_link').getUrl(),
                        para: {
                            simulation_flag: 'N',
                            contract_id: record.get('contract_id'),
                            req_type: ccr_document_type,
                            req_date: req_date,
                            description: description,
                            ref_v01: ref_v01
                        },
                        success: function (res) {
                            $('${/parameter/@layout_code}_user_button1').enable(); //按钮启用
                            var param = record.data;
                            param['change_req_id'] = res.result.change_req_id;
                            param['source_contract_id'] = record.get('contract_id');//變更前源合同id
                            param['contract_id'] = res.result.change_req_id;
                            param['function_usage'] = 'MODIFY';
                            param['maintain_type'] = 'UPDATE';
                            param['document_type'] = ccr_document_type;
                            if(ccr_document_type == 'CUT_CHAG'){//租金計劃變更
                                param['function_code'] = 'CON733CF';
                                param['url_title'] = '变更申请-合同租金计划变更';
                                param['winid'] = 'con733_cf_change_detail_winid';
                            }else if(ccr_document_type == 'LEASE_CHAG'){//承租人變更
                                param['function_code'] = 'CON733BPT';
                                param['url_title'] = '变更申请-合同承租人变更';
                                param['winid'] = 'con733_bpt_change_detail_winid';
                            }else if(ccr_document_type == 'ACC_CHAG'){//回款賬戶變更
                                param['function_code'] = 'CON733BPB';
                                param['url_title'] = '变更申请-合同回款账户变更';
                                param['winid'] = 'con733_bpb_change_detail_winid';
                            }
                            hls_doc_get_layout_code('con_contract_get_layout_code_link_id', param, 'con_contract_change_link', ds_id);
                        },
                        failure: function () {
                         //   $('${/parameter/@layout_code}_user_button1').enable(); //按钮启用
                            Leaf.Masker.unmask(Ext.getBody());
                        },
                        error: function () {
                         //   $('${/parameter/@layout_code}_user_button1').enable(); //按钮启用
                            Leaf.Masker.unmask(Ext.getBody());
                        },
                        scope: this
                    });
                }
            }, null, 300, 150);
        };
        window['${/parameter/@layout_code}_on_layout_dynamic_grid_query'] = function (ds, qpara, bp_seq) {
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract');

            if (ds == $(ds_id)) {
                aut_authority_list_validate_query(ds, qpara);

            }
        };
        ]]></script>
        <a:screen-include
                screen="modules/cont/CON500/con_contract_authority_list_validate.lview?document_category=CONTRACT&amp;function_code=CON733"/>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>
