<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: 38823
    $Date: 2024-03-21
    $Revision: 1.0
    $purpose: 佣金政策维护页面
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script"
          customizationEnabled="true" dynamiccreateenabled="true" trace="true">
    <a:init-procedure>
    </a:init-procedure>
    <a:view>
        <a:link id="policy_approval_link_id" model="hls.HLS303.hlcm_com_policy_wfl" modelaction="update"/>
        <a:link id="policy_query_link_id" model="hls.HLS303.hlcm_com_policy_wfl" modelaction="query"/>
        <script src="${/request/@context_path}/javascripts/calculate.js" type="text/javascript"/>
        <script type="text/javascript"><![CDATA[
        $L.onReady(function(){
            debugger;
            if('${/parameter/@maintain_type}'=='CREATE'){
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'hlcm_comsn_policy_hd');
                var record = $(ds_id).getCurrentRecord();
                var bpId = '${/parameter/@bp_id}';
                record.set('bp_id',bpId);
                record.set('agent_name','${/parameter/@bp_name}');
            }
            if('${/parameter/@function_usage}'=='QUERY'){
                if(document.getElementById('${/parameter/@layout_code}_exit')){
                    $jq('#${/parameter/@layout_code}_exit').hide();
                }
                if(document.getElementById('${/parameter/@layout_code}_submit_approval')){
                    $jq('#${/parameter/@layout_code}_submit_approval').hide();
                }
                if(document.getElementById('${/parameter/@layout_code}_save')){
                    $jq('#${/parameter/@layout_code}_save').hide();
                }
                //uat环境设置query只读不能对grid按钮隐藏处理
                if($jq('#${/parameter/@layout_code}_G_RESULT_ADD_layout_dynamic_button_id')){
                    $jq('#${/parameter/@layout_code}_G_RESULT_ADD_layout_dynamic_button_id').hide();
                }
                if($jq('#${/parameter/@layout_code}_G_RESULT_DELETE_layout_dynamic_button_id')){
                    $jq('#${/parameter/@layout_code}_G_RESULT_DELETE_layout_dynamic_button_id').hide();
                }
            }
        });
        //更新时调用
        window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_update'] = function(ds, record, name, value, old_value, bp_seq) {
            debugger;
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'hlcm_comsn_policy_ln');
            var result_record = $(ds_id).getAll();
                for(var i=0;i<result_record.length;i++){
                    if(name=='finance_amount_to'){
                        var amountTo=result_record[i].get('finance_amount_to');
                        if(amountTo != undefined){
                            if(i == result_record.length-1){
                                if(result_record[i].get('finance_amount_from')>amountTo){
                                    Leaf.showMessage('提示','当前佣金政策的融资额范围存在重叠/空缺，请先修改！');
                                    return;
                                }else {
                                    $(ds_id).create();
                                    result_record[i+1].set('finance_amount_from',amountTo);
                                }
                            }
                            result_record[i+1].set('finance_amount_from',amountTo);
                        }
                    }
                    if(name == 'finance_amount_from'){
                        if(result_record.length>1){
                            var amountTo=result_record[i].get('finance_amount_to');
                            if(i+1<result_record.length && result_record[i+1].data.finance_amount_from !=undefined){
                                var amountFrom=result_record[i+1].get('finance_amount_from');
                                if(amountTo != amountFrom){
                                    $L.showMessage('提示','当前佣金政策的融资额范围存在重叠/空缺，请先修改！');
                                    return;
                                }
                            }
                        }
                    }
                    if(name == 'comsn_rate'){
                        if(result_record[i].get('comsn_rate') == 0){
                            $L.showMessage('提示','佣金比例不能为0！');
                            return;
                        }
                    }
                }
        };
        /* 时间戳转换为时间 */
        function RetimestampToTime(timestamp) {
            timestamp = timestamp ? timestamp : null;
            var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : String(date.getMonth() + 1)) + '-';
            var D = (date.getDate() < 10 ? '0' + date.getDate() : String(date.getDate()));
            // var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
            // var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
            // var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
            return Y + M + D;
        };
        //提交审批
        window['${/parameter/@layout_code}_submit_approval_layout_dynamic_click'] = function() {
            debugger;
            var ds_id=get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'],'hlcm_comsn_policy_hd');
            var re_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'],'hlcm_comsn_policy_ln');
            var record=$(ds_id).getCurrentRecord();
            var re_record=$(re_ds_id).getAll();
            var policyHdId=record.get('policy_hd_id');
            var bpId=record.get('bp_id');
            var division=record.get('division');
            var calcStartDate = RetimestampToTime(Date.parse(record.get('calc_start_date')));
            var calcEndDate = RetimestampToTime(Date.parse(record.get('calc_end_date')));
            if(record.dirty==true){
                Leaf.showMessage('${l:PROMPT}','${l:HLS.EXECUTE_AFTER_SAVE}');
                return;
            }
            for(var i=0;i<re_record.length;i++){
                if(re_record[i].dirty==true){
                    Leaf.showMessage('${l:PROMPT}','${l:HLS.EXECUTE_AFTER_SAVE}');
                    return;
                }
            }
            if(re_record.length ==0){
                Leaf.showErrorMessage('${l:PROMPT}','请先维护政策规则！');
                return;
            }
            if($(ds_id).validate()){
                Leaf.request({
                    url: $('policy_query_link_id').getUrl(),
                    para:{
                        bp_id:bpId,
                        division:division
                    },
                    scope:this,
                    success: function(res) {
                        if(res.result.record !=undefined){
                            if(res.result.record.length ==undefined){
                                var resStartDate=RetimestampToTime(Date.parse(res.result.record.calc_start_date));
                                var resEndDate=RetimestampToTime(Date.parse(res.result.record.calc_end_date));
                                if((resStartDate<calcStartDate && calcStartDate<resEndDate) || (resStartDate<calcEndDate && calcEndDate<resEndDate)){
                                    $L.showErrorMessage('提示','该代理店项下存在重叠的政策时间，请先检查！');
                                    return;
                                }
                            }else {
                                for(var i=0;i<res.result.record.length;i++){
                                    var resStartDate=RetimestampToTime(Date.parse(res.result.record[i].calc_start_date));
                                    var resEndDate=RetimestampToTime(Date.parse(res.result.record[i].calc_end_date));
                                    if((resStartDate<calcStartDate && calcStartDate<resEndDate) || (resStartDate<calcEndDate && calcEndDate<resEndDate)){
                                        $L.showErrorMessage('提示','该代理店项下存在重叠的政策时间，请先检查！');
                                        return;
                                    }
                                }
                            }
                        }
                        Leaf.showConfirm('${HLS.PROMPT}','确认要提交工作流吗？',function () {
                            window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                            Leaf.request({
                                url: $('policy_approval_link_id').getUrl(),
                                para:{
                                    policy_hd_id:policyHdId
                                },
                                scope:this,
                                success: function() {
                                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                    $('${/parameter/@winid}').close();
                                },
                                failure: function() {
                                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                },
                                error: function() {
                                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                }
                            });
                        });
                    },
                    failure: function() {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    error: function() {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    }
                });
            }
        }
        /* 时间戳转换为时间 */
        function timestampToTime(timestamp) {
            timestamp = timestamp ? timestamp : null;
            var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            var Y = date.getFullYear();
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : String(date.getMonth() + 1));
            var D = (date.getDate() < 10 ? '0' + date.getDate() : String(date.getDate()));
            // var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
            // var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
            // var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
            return Y + M + D;
        };
        /*保存前调用，生成编号*/
        window['${/parameter/@layout_code}_on_layout_dynamic_before_submit'] = function (ds, record) {
            debugger
            var check_flag = true;
            var hd_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'hlcm_comsn_policy_hd');
            var ln_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'hlcm_comsn_policy_ln');
            var record = $(hd_ds_id).getCurrentRecord();
            var ln_record = $(ln_ds_id).getAll();
            for(var i=0;i<ln_record.length;i++){
                var amountTo = ln_record[ln_record.length-1].get('finance_amount_to');
                if(amountTo != undefined){
                    $L.showErrorMessage('提示','当前佣金政策的融资额范围存在重叠/空缺，请先修改！');
                    return false;
                }
                if(i+1 < ln_record.length){
                    var amountTo2 = ln_record[i].get('finance_amount_to');
                    var amountFrom = ln_record[i+1].get('finance_amount_from');
                    if(amountTo2 != amountFrom){
                        $L.showErrorMessage('提示','当前佣金政策的融资额范围存在重叠/空缺，请先修改！');
                        return false;
                    }
                }
            }
            var division = record.get('division_n');
            if(record.get('wfl_status') !='APPROVING'){
                var calcStartDate = timestampToTime(Date.parse(record.get('calc_start_date')));
                var calcEndDate = timestampToTime(Date.parse(record.get('calc_end_date')));
                record.set('policy_name','${/parameter/@extra_nam}'+'-'+division+calcStartDate+'-'+calcEndDate);
                return check_flag;
            }
        }

        //新增时调用(grid,table,gridBox)
        window['${/parameter/@layout_code}_on_layout_dynamic_grid_add'] = function(ds, record, config_records, bp_seq) {
            debugger;
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'hlcm_comsn_policy_ln');
            var record = $(ds_id).getAt(0);
            if(record.get('finance_amount_from') ==undefined){
                record.set('finance_amount_from',0);
                record.getField('finance_amount_from').setReadOnly(true);
            }
        };
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>
