<?xml version="1.0" encoding="UTF-8"?>
<!--
    合同变更
        租金計劃變更
        承租人變更
        回款賬戶變更
 -->
<a:screen 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:model-query defaultWhereClause="t1.user_id=${/session/@user_id}" fetchAll="true"
                       model="cont.CON733.query_user_info" rootPath="user_info"/>
    </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="con_contract_sign_before_change_link" url="${/request/@context_path}/modules/cont/CON731/hls_before_sign_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 user_name = '${/model/user_info/record/@user_name}';
            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' && (user_name != 'MJ002' && user_name != 'BJK002' && user_name != 'TJ002')) {
                //     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();
                }
                if (value == 'CCR_BEFORE_SIGN') {
                    $(ds_id).setQueryParameter('contract_status', 'NEW');
                    $(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;
            }
            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);
            var record = records[0];
            var ccr_document_type = head_record.get('ccr_document_type');
            if ($(head_ds_id).validate($('${/parameter/@layout_code}_user_button1').enable())) {
                //售后回租 无法进行承租人变更和回款账户变更
                if (record.get('business_type') == 'LEASEBACK' && (ccr_document_type == 'LEASE_CHAG' || ccr_document_type == 'ACC_CHAG')) {
                    Leaf.showErrorMessage('${l:PROMPT}', '售后回租无法操作此项变更!');
                    return;
                }
                if(record.get('division') == '70' && ccr_document_type == 'LEASE_CHAG'){
                //if(record.get('division') == '70' &&(ccr_document_type == 'LEASE_CHAG' ||ccr_document_type == 'CUT_CHAG')){
                //     Leaf.showErrorMessage('${l:PROMPT}', '铁甲业务无法操作此项变更!');
                    Leaf.showErrorMessage('${l:PROMPT}', '二手机业务无法操作此项变更!');
                    return;
                }
                //if((record.get('division') != '70' && record.get('division') != '92') && ccr_document_type == 'CCR_BEFORE_SIGN'){
                   // Leaf.showErrorMessage('${l:PROMPT}', '当前合同暂不能进行此项变更!');
                   // return;
                //}
                if (record.get('business_type') == 'LEASEBACK' && record.get('bp_class') == 'NP' && record.get('division') != 70 && record.get('division') != 92&& ccr_document_type == 'CCR_BEFORE_SIGN') {
                    Leaf.showMessage('${l:PROMPT}', '此类业务尚未开放电子签约功能,不能发起签约前变更');
                      return;
                }
                if (record.get('division') == 91 && record.get('business_type')  == 'LEASEBACK' && ccr_document_type == 'CCR_BEFORE_SIGN') {
                    Leaf.showMessage('${l:PROMPT}', '此类业务尚未开放电子签约功能，不能发起签约前变更');
                     return;
                }

                Leaf.showConfirm('特别注意', '<span style="color: #ff0000; ">该操作将导致合同在变更期间无法进行后续操作，请谨慎操作！！！如果需要取消变更，请一定点击取消变更按钮！！！</span>', function () {

                    var contract = record.get('contract_id');
                    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';
                            }else if (ccr_document_type == 'CCR_BEFORE_SIGN') {
                                param['function_code'] = 'CON733CBSIGN';
                                param['url_title'] = '变更申请-签约前合同变更';
                                param['winid'] = 'con733_before_sign_change_detail_winid';
                            }
                            if(ccr_document_type == 'CCR_BEFORE_SIGN') {
                            //签约前变更换个页面，方便后续新人代码阅读 wangwei
                            hls_doc_get_layout_code('con_contract_get_layout_code_link_id', param, 'con_contract_sign_before_change_link', ds_id);
                            }else{
                            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>
