<?xml version="1.0" encoding="UTF-8"?>

<!--
    $Author: chenlingfeng7543
$Date: 2018/12/20 21:19
$Revision: 1.0
$Purpose: 商业伙伴授信维护
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true" dynamiccreateenabled="true" trace="true">
    <a:init-procedure>
        <a:model-query  defaultWhereClause="t1.user_id=${/session/@user_id} and position_code in(003,9070)" fetchAll="true" model="csh.CSH509.query_roles_info" rootPath="position_code"/>

    </a:init-procedure>
    <a:view>
        <a:link id="hn1150_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code" modelaction="update"/>
        <a:link id="hls361_hls_bp_master_credit_tenant_detail_link" url="${/request/@context_path}/modules/hls/HLS361N/credit_entrance_detail.lview"/>
        <a:link id="hls361_hls_bp_master_credit_agent_detail_link" url="${/request/@context_path}/modules/hls/HLS362N/credit_entrance_detail_confirm.lview"/>
        <a:link id="hls362N_update_credit_confirmed_link_id" url="${/request/@context_path}/modules/hls/HLS362N/hls_bp_master_batch_update.lsc"/>
        <a:link id="con_contract_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code" modelaction="update"/>
        <a:link id="check_create_record_link_id" model="hls.HLS361N.hls_bp_master_create_record"
                modelaction="update"/>
        <a:link id="credit_detail_link" url="${/request/@context_path}/modules/hls/HLS361N/hls_bp_mster_credit_record.lview"/>

        <script type="text/javascript"><![CDATA[
        var user_code='';
        Leaf.onReady(function(){
            user_code='${/model/position_code/record/@position_code}';
            if(user_code == 9070){
                var ds_id=get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'F_CREDIT_HD', 'hls_bp_master_credit_hd');
                var op_ds_id=get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'F_CREDIT_OP', 'hls_bp_master_credit_hd');
                var record = 'JCREDIT_ENTRANCE_CONFIRM_F_QUERY__ds';
                $(record).getCurrentRecord().set('credit_status','APPROVING_OP');
                $(record).getCurrentRecord().set('credit_status_n','审批中(营业副部长-经租)');
                $(ds_id).query();
                $(op_ds_id).query();
            }
        });


        //超链接渲染
        window['${/parameter/@bp_seq}${/parameter/@layout_code}_dynamic_link_renderer'] = function (value, record, name, config_record, bp_seq) {
            //console.log(record);
            var link_function = '';
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_hls_link_render_record'][record.id + '---' + name] = record;

            if (name == 'bp_code' && value) {
                if (record.get('bp_category')=='AGENT'){
                    link_function = 'on_business_tenant_detail_window';
                }else if ( record.get('bp_category')=='TENANT'){
                    link_function = 'on_business_tenant_detail_window';
                }else if ( record.get('bp_category')=='VENDER'){
                    link_function = 'on_business_tenant_detail_window';
                }else if ( record.get('bp_category')=='RENTER'){
                    link_function = 'on_business_tenant_detail_window';
                }

                return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\');">' + value + '</a>';
            }
        };

        //变更确认
        window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function() {
            //授信额度变更
            var credit_ds=get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'],'F_CREDIT_HD', 'hls_bp_master_credit_hd');
            var op_credit_ds=get_dsid_by_tabcode_basetable(window['${/parameter/@layout_code}_layoutDataSetList'],'F_CREDIT_OP', 'hls_bp_master_credit_hd');
            var credit_datas = $(credit_ds).selected;
            var op_credit_datas = $(op_credit_ds).selected;
            if(credit_datas == ''){
                var  datas=$(op_credit_ds).selected;
            }else {
                datas = $(credit_ds).selected;
            }
            if(credit_datas!='' && op_credit_datas!=''){
                $L.showErrorMessage("提示",'请勾选同一业务类型的单据进行授信确认操作！');
                return;
            }
            var param = {};
            var saveData = [];
            //判断角色是否可以操作
            //002 营业本部长
            for (var i = 0; i < datas.length; i++){
                if(user_code==''||user_code==undefined){
                    $L.showInfoMessage("提示",'只有营业本部长/营业副部长（经租）才能操作数据！');
                    return;
                }
            }
            if(datas.length<1){
                $L.showInfoMessage("提示",'请选择数据',null,null)
                return;
            }
            for (var i = 0; i < datas.length; i++) {
                //审批冻结判断
                var credit_status=datas[i].data.credit_status;
                if(credit_status!='APPROVING' && credit_status!='APPROVING_OP'){
                    $L.showInfoMessage("提示",'请选择状态为审批中的数据！',null,null)
                    return;
                }
                var data_record;
                data_record = datas[i];
                data_record.set('_status', 'update');
                data_record.set('bp_credit_hd_id', datas[i].data.bp_credit_hd_id);
                if(user_code == 9070){
                    data_record.set('wanted_status', 'APPROVING');
                }else {
                    data_record.set('wanted_status', 'APPROVED');
                }
                saveData.push(data_record.data);
            }
            param['details'] = saveData;

            $L.showConfirm("提示",'确认进行授信确认？',function(){
                window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                Leaf.request({
                    url: $('hls362N_update_credit_confirmed_link_id').getUrl(),
                    para: param,
                    success: function ()  {
                        Leaf.SideBar.show({
                            msg: '操作成功',
                            duration: 2000
                        });
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        $(credit_ds).query();
                    },
                    error: function() {
                    },
                    failure: function() {
                    },
                    scope: this
                });

            })
        };

        //驳回单据.
        window['${/parameter/@layout_code}_user_button2_layout_dynamic_click'] = function() {
            var credit_ds=get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'hls_bp_master_credit_hd');
            var  datas=$(credit_ds).selected;
            var param = {};
            var saveData = [];
            //角色判断
            //002 营业本部长
            for (var i = 0; i < datas.length; i++){
                if(user_code==''||user_code==undefined){
                    $L.showInfoMessage("提示",'只有营业本部长才能操作数据！');
                    return;
                }else if (user_code =='9065' && datas[i].get('bp_category')!='RENTER' ){
                    $L.showInfoMessage("提示",'当前用户只能选择<font color="red">'+'租赁商'+'</font>类型进行授信操作');
                    return;
                }else if (user_code =='003' && datas[i].get('bp_category')=='RENTER'){
                    $L.showInfoMessage("提示",'当前用户只能选择<font color="red">'+'非租赁商'+'</font>类型进行授信操作');
                    return;
                }
            }
            if(datas.length<1){
                $L.showInfoMessage("提示",'请选择一条数据',null,null)
                return;
            }

            for (var i = 0; i < datas.length; i++) {
                //审批冻结判断
                var credit_status=datas[i].data.credit_status;
                if(credit_status!='APPROVING'){
                    $L.showInfoMessage("提示",'请选择状态为审批中的数据！',null,null)
                    return;
                }
                var data_record;
                data_record = datas[i];
                data_record.set('_status', 'update');
                data_record.set('bp_credit_hd_id', datas[i].data.bp_credit_hd_id);
                data_record.set('wanted_status', 'REJECT');
                saveData.push(data_record.data);
            }
            param['details'] = saveData;
            $L.showConfirm("提示",'确认进行授信退回？',function(){
           //     window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                Leaf.request({
                    url: $('hls362N_update_credit_confirmed_link_id').getUrl(),
                    para: param,
                    success: function ()  {
                        Leaf.SideBar.show({
                            msg: '操作成功',
                            duration: 2000
                        });
                      //  window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        $(credit_ds).query();
                    },
                    error: function() {
                    },
                    failure: function() {
                    },
                    scope: this
                });

            })

        }
        //--承租人的详细信息
        function on_business_tenant_detail_window(id, name) {
            //获得该条record的所有数据
            var record = window['${/parameter/@layout_code}_hls_link_render_record'][id + '---' + name];
            console.log(record)
            var param = record.data;
            if(param.credit_type == 'CAPITAL_LOAN_CREDIT' ){
                param['function_code'] = 'HLS36101';
                param['function_usage'] = 'MODIFY';
                param['maintain_type'] = 'UPDATE';
                param['url_title'] = '商业伙伴授信详细';
                hls_doc_get_layout_code('hn1150_get_layout_code_link_id', param, 'hls361_hls_bp_master_credit_tenant_detail_link', null);
            }else{
                param['function_code'] = 'HLS361Z';
                param['function_usage'] = 'MODIFY';
                param['maintain_type'] = 'UPDATE';
                param['url_title'] = '商业伙伴授信详细';
                hls_doc_get_layout_code('hn1150_get_layout_code_link_id', param, 'hls361_hls_bp_master_credit_tenant_detail_link', null);
            }

        }
        //--经销商的详细信息
        function on_business_agent_detail_window(id, name) {
            //获得该条record的所有数据
            var record = window['${/parameter/@layout_code}_hls_link_render_record'][id + '---' + name];
            var param = record.data;
            param['function_code'] = 'HLS36103';
            param['function_usage'] = 'MODIFY';
            param['maintain_type'] = 'UPDATE';
            param['url_title'] = '商业伙伴授信详细';
            hls_doc_get_layout_code('hn1150_get_layout_code_link_id', param, 'hls361_hls_bp_master_credit_agent_detail_link', null);
        }

        //变更履历按钮
        window['${/parameter/@layout_code}_user_button3_layout_dynamic_click'] = function() {
            var credit_ds=get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'hls_bp_master_credit_hd');
            var record = $(credit_ds).getSelected();
             if (record.length!=1){
                 $L.showInfoMessage("提示",'请选择一条数据！');
                 return;
             }
            Leaf.Masker.mask(Ext.getBody(), '正在执行...');
            Leaf.request({
                url: $('check_create_record_link_id').getUrl(),
                para: {
                    bp_credit_hd_id: record[0].get('bp_credit_hd_id')
                },
                success: function () {
                    Leaf.Masker.unmask(Ext.getBody());

                    var win = new Leaf.Window({
                        id: 'hls_bp_credit_record_window',
                        params: {
                            bp_credit_hd_id: record[0].get('bp_credit_hd_id'),
                            winid: 'hls_bp_credit_record_window'
                        },
                        url: $('credit_detail_link').getUrl(),
                        title: '授信额度变更履历',
                        fullScreen: true
                    });
                    win.on('close', function () {
                        // ds.query(ds.currentPage);
                    });
                },
                failure: function () {
                    Leaf.Masker.unmask(Ext.getBody());
                },
                error: function () {
                    Leaf.Masker.unmask(Ext.getBody());
                },
                scope: this
            });
        }



        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>
