<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<script>
    /**
     *
     * @param pleavetype 出差方式：0：全天出差；1：按时间区间出差；2：半天；3：按时间长度出差
     */
    function resetAttr(pleavetype) {
        switch (pleavetype){
            case '1'://按时间区间出差
                WfForm.changeFieldValue(WfForm.convertFieldNameToId("scdw"),{ value: '1' });//出差时长单位为小时
                //出差开始时间可编辑
                WfForm.changeFieldAttr(WfForm.convertFieldNameToId("kssj"), 3);
                //出差结束时间可编辑
                WfForm.changeFieldAttr(WfForm.convertFieldNameToId("jssj"), 3);
                //出差时间长度不可编辑
                WfForm.changeFieldAttr(WfForm.convertFieldNameToId("ccsc"), 1);
                WfForm.changeFieldAttr(WfForm.convertFieldNameToId("mttsdcc"), 2);
                break;
            case '3'://按时间区间出差
                WfForm.changeFieldValue(WfForm.convertFieldNameToId("scdw"),{ value: '1' });//出差时长单位为小时
                //隐藏出差开始时间所在行（结束时间在同一行，不需要额外设置）
                WfForm.changeFieldAttr(WfForm.convertFieldNameToId("kssj"), 5);
                //出差时间长度可编辑
                WfForm.changeFieldAttr(WfForm.convertFieldNameToId("ccsc"), 3);
                //每天出差时长默认为1小时
                WfForm.changeFieldValue(WfForm.convertFieldNameToId("ccsc"),{ value: '1' });
                WfForm.changeFieldAttr(WfForm.convertFieldNameToId("mttsdcc"), 1);
                WfForm.changeFieldValue(WfForm.convertFieldNameToId("mttsdcc"),{ value: '1' });
                break;
            default:
                WfForm.changeFieldValue(WfForm.convertFieldNameToId("scdw"),{ value: '0' });//出差时长单位为小时
                //隐藏出差开始时间所在行（结束时间在同一行，不需要额外设置）
                WfForm.changeFieldAttr(WfForm.convertFieldNameToId("kssj"), 5);
                //出差时间长度不可编辑
                WfForm.changeFieldAttr(WfForm.convertFieldNameToId("ccsc"), 1);
                WfForm.changeFieldAttr(WfForm.convertFieldNameToId("mttsdcc"), 1);
                WfForm.changeFieldValue(WfForm.convertFieldNameToId("mttsdcc"),{ value: '1' });
                break;
        }
    }
    jQuery().ready(function(){
        WfForm.registerCheckEvent(WfForm.OPER_ADDROW+"1", function(callback){
            callback();    //允许继续添加行调用callback，不调用代表阻断添加
        });
    });
    WfForm.registerAction(WfForm.ACTION_ADDROW+"1", function(index){
        //adddetail()
    });
    /**
     * 自动添加出差明细
     */
    function adddetail() {
        //首先进行必填校验，不通过直接退出
        if(!WfForm.verifyFormRequired(false, true)){ return; }
        //获取出差时段类型：0：全天；1：指定时间区间；2：半天；3：指定时长长度
        let ccsd = WfForm.getFieldValue(WfForm.convertFieldNameToId("ccsd"));
        //每天同时段出差，1:是，0：否
        let mttsdcc = WfForm.getFieldValue(WfForm.convertFieldNameToId("mttsdcc"));
        //开始日期
        var ksrq = new Date(WfForm.getFieldValue(WfForm.convertFieldNameToId("ksrq")));
        //结束日期
        var jsrq = new Date(WfForm.getFieldValue(WfForm.convertFieldNameToId("jsrq")));
        //开始时间
        var kssj = WfForm.getFieldValue(WfForm.convertFieldNameToId("kssj"));
        //结束时间
        let jssj = WfForm.getFieldValue(WfForm.convertFieldNameToId("jssj"));
        //出差时长
        var ccsc = WfForm.getFieldValue(WfForm.convertFieldNameToId("ccsc"));
        //出差类型
        var arr_cclx= mobx.toJS(wfform.getFieldValueObj(WfForm.convertFieldNameToId("cclx")).specialobj);
        var cclx = arr_cclx[0].id;

        var ccr = WfForm.getFieldValue(WfForm.convertFieldNameToId("ccr"));


        //清空明细表1所有记录
        WfForm.delDetailRow("detail_1", "all");

        let mainTableData = {
            ccr: WfForm.getFieldValue(WfForm.convertFieldNameToId("ccr")),
            ccsc: WfForm.getFieldValue(WfForm.convertFieldNameToId("ccsc")),
            ccsd: WfForm.getFieldValue(WfForm.convertFieldNameToId("ccsd")),
            cclx: WfForm.getFieldValue(WfForm.convertFieldNameToId("cclx")),
            mttsdcc: WfForm.getFieldValue(WfForm.convertFieldNameToId("mttsdcc")),
            ksrq: WfForm.getFieldValue(WfForm.convertFieldNameToId("ksrq")),
            jsrq: WfForm.getFieldValue(WfForm.convertFieldNameToId("jsrq")),
            kssj: WfForm.getFieldValue(WfForm.convertFieldNameToId("kssj")),
            jssj: WfForm.getFieldValue(WfForm.convertFieldNameToId("jssj"))
        }

        let param = {
            mainTableData:JSON.stringify(mainTableData),
        }
        console.log(param);
        $.ajax({
            url:'/api/attendance/businesstripsapply/generateBusinessTripsList',
            data:param,
            type:'GET',
            dataType:'json',
            async:false,
            success:function(res){
                if(res.api_status){
                    if(res.data.status){
                        WfForm.delDetailRow("detail_1", "all");
                        let resultData = res.data.data;
                        resultData.filter(item => {
                            let addObj = {};
                            addObj[WfForm.convertFieldNameToId("ccr", "detail_1")] = {value:item.ccr,specialobj:[{id:item.ccr,name:item.ccrName}]};
                            addObj[WfForm.convertFieldNameToId("cclx", "detail_1")] = {value:item.cclx,specialobj:[{id:item.cclx,name:item.cclxName}]};
                            addObj[WfForm.convertFieldNameToId("ksrq", "detail_1")]={value:item.ksrq};
                            addObj[WfForm.convertFieldNameToId("kssj", "detail_1")]={value:item.kssj};
                            addObj[WfForm.convertFieldNameToId("jsrq", "detail_1")]={value:item.jsrq};
                            addObj[WfForm.convertFieldNameToId("jssj", "detail_1")]={value:item.jssj};
                            addObj[WfForm.convertFieldNameToId("ccsc", "detail_1")]={value:item.ccsc};
                            addObj[WfForm.convertFieldNameToId("qtcc", "detail_1")]={value:item.qtcc};
                            addObj[WfForm.convertFieldNameToId("btcc", "detail_1")]={value:item.btcc};

                            WfForm.addDetailRow("detail_1", addObj);
                        })
                    }else{
                        WfForm.showMessage(res.data.errorInfo, 2, 5);
                    }
                }
            }
        })

        //添加完明细后，更新主表的总出差时长栏位信息
        //var totalsc = getTotalsc();
        var totalsc = getMainCcsc();
        WfForm.changeFieldValue(WfForm.convertFieldNameToId("ccsc"), {value:totalsc});
        //控制明细显示属性
        var rowArr = WfForm.getDetailAllRowIndexStr("detail_1").split(",");
        for(var i=0; i<rowArr.length; i++){
            var rowIndex = rowArr[i];
            if(rowIndex !== ""){
                switch(ccsd){
                    case '1'://按时间区间出差
                        WfForm.changeFieldAttr(WfForm.convertFieldNameToId("ccsc","detail_1")+"_"+rowIndex, 1);//出差时长栏位设置只读
                        WfForm.changeFieldAttr(WfForm.convertFieldNameToId("qtcc","detail_1")+"_"+rowIndex, 1);//全天差栏位设置只读
                        WfForm.changeFieldAttr(WfForm.convertFieldNameToId("btcc","detail_1")+"_"+rowIndex, 1);//半天差栏位设置只读
                        break;
                    case '3'://按时间长度出差
                        WfForm.changeFieldAttr(WfForm.convertFieldNameToId("ccsc","detail_1")+"_"+rowIndex, 3);//出差时长栏位设置必填
                        WfForm.changeFieldAttr(WfForm.convertFieldNameToId("qtcc","detail_1")+"_"+rowIndex, 1);//全天差栏位设置只读
                        WfForm.changeFieldAttr(WfForm.convertFieldNameToId("btcc","detail_1")+"_"+rowIndex, 1);//半天差栏位设置只读
                        break;
                    default://其他全天或者半天出差
                        WfForm.changeFieldAttr(WfForm.convertFieldNameToId("ccsc","detail_1")+"_"+rowIndex, 1);//出差时长栏位设置只读
                        WfForm.changeFieldAttr(WfForm.convertFieldNameToId("qtcc","detail_1")+"_"+rowIndex, 2);//全天差栏位设置可编辑
                        WfForm.changeFieldAttr(WfForm.convertFieldNameToId("btcc","detail_1")+"_"+rowIndex, 2);//半天差栏位设置可编辑
                        break;
                }
            }
        }
    }
    /**
     * 计算明细总出差长度
     */
    function getTotalsc(){
        var ccsd = WfForm.getFieldValue(WfForm.convertFieldNameToId("ccsd"));
        var totalsc = 0;
        var rowArr = WfForm.getDetailAllRowIndexStr("detail_1").split(",");
        for(var i=0; i<rowArr.length; i++){
            var rowIndex = rowArr[i];
            if(rowIndex !== ""){
                if (ccsd == '3' || ccsd == '1')//累计小时时长
                    totalsc = Number(totalsc) + Number(WfForm.getFieldValue(WfForm.convertFieldNameToId("ccsc", "detail_1")+"_"+rowIndex));
                else{
                    //累计天数时长
                    totalsc = Number(totalsc) +
                        Number(WfForm.getFieldValue(WfForm.convertFieldNameToId("qtcc", "detail_1")+"_"+rowIndex)) +
                        Number(WfForm.getFieldValue(WfForm.convertFieldNameToId("btcc", "detail_1")+"_"+rowIndex)) * 0.5;
                }
            }
        }
        return totalsc;
    }
    /**
     * 获取主表出差长度,取第一条明细中的出差长度作为流程主表出差长度
     */
    function getMainCcsc(){
        var ccsd = WfForm.getFieldValue(WfForm.convertFieldNameToId("ccsd"));
        var totalsc = 0;
        var rowArr = WfForm.getDetailAllRowIndexStr("detail_1").split(",");
        var rowIndex = rowArr[0];
        if(rowIndex !== ""){
            if (ccsd == '3' || ccsd == '1')//累计小时时长
                totalsc = Number(WfForm.getFieldValue(WfForm.convertFieldNameToId("ccsc", "detail_1")+"_"+rowIndex));
            else{
                //累计天数时长
                totalsc = Number(WfForm.getFieldValue(WfForm.convertFieldNameToId("qtcc", "detail_1")+"_"+rowIndex)) +
                    Number(WfForm.getFieldValue(WfForm.convertFieldNameToId("btcc", "detail_1")+"_"+rowIndex)) * 0.5;
            }
        }
        return totalsc;
    }
    //依据当前出差方式，对页面上的控件显示属性进行调整
    resetAttr(WfForm.getFieldValue(WfForm.convertFieldNameToId("ccsd")));
    //是否自动产生明细记录标记
    //考虑到影响明细记录的栏位如开始结束日期时间等发生变更时，需要重新生成明细，而出差方式变更时又需要对相应栏位的值做修改，所以添加这个开关，防止重复生成明细记录
    var autodetail = true;
    //差期类别值变更时，自动生成明细
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("cclx"),function(obj,id,value){
        //关掉自动生成明细记录功能
        autodetail = false;
        //自动添加出差明细数据
        adddetail();
        //打开自动生成明细记录功能
        autodetail = true;
    });
    //出差人员值变更时，自动生成明细
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("ccr"),function(obj,id,value){
        //关掉自动生成明细记录功能
        autodetail = false;
        //自动添加出差明细数据
        adddetail();
        //打开自动生成明细记录功能
        autodetail = true;
    });
    //出差方式变更时，同样进行页面显示属性的调整
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("ccsd"),function(obj,id,value){
        //关掉自动生成明细记录功能
        autodetail = false;
        //出差方式变更后，修改页面上的控件显示属性
        resetAttr(value);
        //自动添加出差明细数据
        adddetail();
        //打开自动生成明细记录功能
        autodetail = true;
    });
    //是否每日重复时间段值变更时，自动生成明细
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("mttsdcc"),function(obj,id,value){
        //关掉自动生成明细记录功能
        autodetail = false;
        //自动添加出差明细数据
        adddetail();
        //打开自动生成明细记录功能
        autodetail = true;
    });
    //开始日期变更后，自动重新生成明细
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("ksrq"),function(obj,id,value){
        if (autodetail){
            autodetail = false;
            adddetail();
            autodetail = true;
        }
    });
    //结束日期变更后，自动重新生成明细
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("jsrq"),function(obj,id,value){
        if (autodetail){
            autodetail = false;
            adddetail();
            autodetail = true;
        }
    });
    //开始时间变更后，自动重新生成明细
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("kssj"),function(obj,id,value){
        if (autodetail){
            autodetail = false;
            adddetail();
            autodetail = true;
        }
    });
    //结束时间变更后，自动重新生成明细
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("jssj"),function(obj,id,value){
        if (autodetail){
            autodetail = false;
            adddetail();
            autodetail = true;
        }
    });
    //出差时长发生变更后，自动重新生成明细
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("ccsc"),function(obj,id,value){
        if (autodetail){
            autodetail = false;
            adddetail();
            autodetail = true;
        }
    });
    //绑定全天差栏位值变更事件
    jQuery(document).ready(function(){
        WfForm.bindDetailFieldChangeEvent(WfForm.convertFieldNameToId("qtcc", "detail_1"),function(id,rowIndex,value){
            if (autodetail){
                autodetail = false;
                if (value === '0')
                    WfForm.changeFieldValue(WfForm.convertFieldNameToId("btcc", "detail_1")+"_"+rowIndex, {value:"1"});
                else
                    WfForm.changeFieldValue(WfForm.convertFieldNameToId("btcc", "detail_1")+"_"+rowIndex, {value:"0"});
                //var totalsc = getTotalsc();
                var totalsc = getMainCcsc();
                WfForm.changeFieldValue(WfForm.convertFieldNameToId("ccsc"), {value:totalsc});
                autodetail = true;
            }
        });
    });
    //绑定半天差值变更事件
    jQuery(document).ready(function(){
        WfForm.bindDetailFieldChangeEvent(WfForm.convertFieldNameToId("btcc", "detail_1"),function(id,rowIndex,value){
            if (autodetail){
                autodetail = false;
                if (value === '0')
                    WfForm.changeFieldValue(WfForm.convertFieldNameToId("qtcc", "detail_1")+"_"+rowIndex, {value:"1"});
                else
                    WfForm.changeFieldValue(WfForm.convertFieldNameToId("qtcc", "detail_1")+"_"+rowIndex, {value:"0"});
                //var totalsc = getTotalsc();
                var totalsc = getMainCcsc();
                WfForm.changeFieldValue(WfForm.convertFieldNameToId("ccsc"), {value:totalsc});
                autodetail = true;
            }
        });
    });
    //绑定出差时长栏位值变更事件
    jQuery(document).ready(function(){
        WfForm.bindDetailFieldChangeEvent(WfForm.convertFieldNameToId("qjsc", "detail_1"),function(id,rowIndex,value){
            if (autodetail){
                autodetail = false;
                //var totalsc = getTotalsc();
                var totalsc = getMainCcsc();
                WfForm.changeFieldValue(WfForm.convertFieldNameToId("qjsc"), {value:totalsc});
                autodetail = true;
            }
        });
    });
    WfForm.registerCheckEvent(WfForm.OPER_SAVE+","+WfForm.OPER_SUBMIT,function(callback){
        var d_count = WfForm.getDetailRowCount("detail_1");
        if (Number(d_count) === 0){
            alert("没有出差明细，不能继续！")
        }else{
            callback();
        }
    });
</script>






