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


<script>

    /**
     * 自动添加出差明细
     */
    function adddetail() {
        //首先进行必填校验，不通过直接退出
        //if(!WfForm.verifyFormRequired(false, true)){ return; }

        let mainTableData = {
            szjg: WfForm.getFieldValue(WfForm.convertFieldNameToId("szjg")),
            jbry: WfForm.getFieldValue(WfForm.convertFieldNameToId("jbry")),
            jblx: WfForm.getFieldValue(WfForm.convertFieldNameToId("jblx")),
            mttsdjb: WfForm.getFieldValue(WfForm.convertFieldNameToId("mttsdjb")),
            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/overtimeplan/generateOvertimePlanDetails',
            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("jbry", "detail_1")] = {value:item.jbry,specialobj:[{id:item.jbry,name:item.jbryName}]};
                            addObj[WfForm.convertFieldNameToId("jblx", "detail_1")] = {value:item.jblx,specialobj:[{id:item.jblx,name:item.jblxName}]};
                            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("gsrq", "detail_1")]={value:item.gsrq};
                            addObj[WfForm.convertFieldNameToId("jbsc", "detail_1")]={value:Number(item.jbsc)};

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

        //添加完明细后，更新主表的总请假时长栏位信息
        var totalsc = getTotalsc();
        WfForm.changeFieldValue(WfForm.convertFieldNameToId("jbsc"), {value:totalsc});
    }
    // function adddetail() {
    //   //首先进行必填校验，不通过直接退出
    //   //if(!WfForm.verifyFormRequired(false, true)){ return; }
    //   //每天同时段出差，1:是，0：否
    //   let mttsdcc = WfForm.getFieldValue(WfForm.convertFieldNameToId("mttsdjb"));
    //   //开始日期
    //   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("jbsc"));
    //   //加班类型
    //   var arr_jblx = mobx.toJS(wfform.getFieldValueObj(WfForm.convertFieldNameToId("jblx")).specialobj);
    //   //清空第一明细所有记录
    //   WfForm.delDetailRow("detail_1", "all");
    //   //判断出差是否跨天
    //   let v_overzero = jssj < kssj ? true : false;
    //   //出差人ID数组
    //   var arr_ids = (WfForm.getFieldValue(WfForm.convertFieldNameToId("jbry"))).split(',');
    //   //出差人名字数组
    //   var arr_names = (WfForm.getBrowserShowName(WfForm.convertFieldNameToId("jbry"))).split(',');
    //   for (let index = 0; index < arr_ids.length; index++) {
    //     //预置明细记录开始日期
    //     var v_tmpbdate = new Date(ksrq);
    //     //预置明细记录结束日期
    //     var v_tmpedate = new Date(v_tmpbdate);
    //     if (mttsdcc == '1') {
    //       //首次循环时，明细开始与结束日期均等于主表开始日期，而主表结束日期一定大于等于开始日期，所以至少添加一笔明细
    //       //当出差为跨天差时，每笔明细的结束日期会加一天
    //       while(v_tmpedate <= jsrq && v_tmpedate <= jsrq){
    //         //初始化明细记录
    //         var addObj = {};
    //         //添加出差人信息
    //         addObj[WfForm.convertFieldNameToId("jbry", "detail_1")] = {value:arr_ids[index],specialobj:[{id:arr_ids[index],name:arr_names[index]}]};
    //         //添加加班类型信息
    //         addObj[WfForm.convertFieldNameToId("jblx", "detail_1")] = {value:arr_jblx[0].id,specialobj:[{id:arr_jblx[0].id,name:arr_jblx[0].mc}]};
    //         //添加开始日期字段信息
    //         addObj[WfForm.convertFieldNameToId("ksrq", "detail_1")] = {value:v_tmpbdate};

    //         addObj[WfForm.convertFieldNameToId("gsrq", "detail_1")] = {value:v_tmpbdate};
    //         //按时间区间出差逻辑
    //         if (v_overzero){
    //           //跨天时，明细记录的结束日期等于开始日期加一天
    //           v_tmpedate = v_tmpedate.setDate(v_tmpbdate.getDate() + 1);
    //           v_tmpedate = new Date(v_tmpedate);
    //         }
    //         //添加结束日期字段信息
    //         addObj[WfForm.convertFieldNameToId("jsrq", "detail_1")] = {value:v_tmpedate};
    //         //添加开始时间字段信息
    //         addObj[WfForm.convertFieldNameToId("kssj", "detail_1")] = {value:kssj};
    //         //添加结束时间字段信息
    //         addObj[WfForm.convertFieldNameToId("jssj", "detail_1")] = {value:jssj};
    //         //todo 调用接口计算开始结束时间区间内的工作时长；
    //         //暂时先不调用接口，直接使用开始结束时间区间间隔小时数
    //         var v_hours = (new Date(v_tmpedate+" "+jssj).getTime() - new Date(v_tmpbdate+" "+kssj).getTime())/(1000 * 60 * 60);
    //         //超过8小时，以8小时计
    //         v_hours = Number(v_hours) > 8 ? 8 : Number(v_hours)
    //         addObj[WfForm.convertFieldNameToId("jbsc", "detail_1")] = {value:v_hours};
    //         //追加明细记录
    //         WfForm.addDetailRow("detail_1", addObj);
    //         //明细开始日期加一天
    //         v_tmpbdate = v_tmpbdate.setDate(v_tmpbdate.getDate() + 1);
    //         v_tmpbdate = new Date(v_tmpbdate);
    //         //明细结束日期加一天
    //         v_tmpedate = v_tmpedate.setDate(v_tmpedate.getDate() + 1);
    //         v_tmpedate = new Date(v_tmpedate);
    //       }
    //     }else{
    //       //非每天同时段出差方式出差
    //       //设置第一笔明细结束日期为出差开始日期加一天
    //       v_tmpedate = v_tmpedate.setDate(v_tmpedate.getDate() + 1);
    //       v_tmpedate = new Date(v_tmpedate);
    //       while (v_tmpedate <= jsrq) {
    //         var addObj = {};
    //         addObj[WfForm.convertFieldNameToId("jbry", "detail_1")] = {value:arr_ids[index],specialobj:[{id:arr_ids[index],name:arr_names[index]}]};
    //         addObj[WfForm.convertFieldNameToId("ksrq", "detail_1")] = {value:v_tmpbdate};
    //         addObj[WfForm.convertFieldNameToId("gsrq", "detail_1")] = {value:v_tmpbdate};
    //         addObj[WfForm.convertFieldNameToId("jsrq", "detail_1")] = {value:v_tmpedate};
    //         addObj[WfForm.convertFieldNameToId("kssj", "detail_1")] = {value:kssj};
    //         addObj[WfForm.convertFieldNameToId("jssj", "detail_1")] = {value:"00:00"};
    //         WfForm.addDetailRow("detail_1", addObj);
    //         v_tmpbdate = new Date(v_tmpedate);
    //         kssj = '00:00';
    //         //明细结束日期加一天
    //         v_tmpedate = v_tmpedate.setDate(v_tmpedate.getDate() + 1);
    //         v_tmpedate = new Date(v_tmpedate);
    //       }
    //       var addObj = {};
    //       addObj[WfForm.convertFieldNameToId("ksrq", "detail_1")] = {value:v_tmpbdate};
    //       addObj[WfForm.convertFieldNameToId("gsrq", "detail_1")] = {value:v_tmpbdate};
    //       addObj[WfForm.convertFieldNameToId("jsrq", "detail_1")] = {value:jsrq};
    //       addObj[WfForm.convertFieldNameToId("kssj", "detail_1")] = {value:kssj};
    //       addObj[WfForm.convertFieldNameToId("jssj", "detail_1")] = {value:jssj};
    //       //追加明细记录
    //       WfForm.addDetailRow("detail_1", addObj);
    //     }
    //   };
    //   //添加完明细后，更新主表的总请假时长栏位信息
    //   var totalsc = getTotalsc();
    //   WfForm.changeFieldValue(WfForm.convertFieldNameToId("jbsc"), {value:totalsc});
    // }

    /**
     * 计算明细总请假长度
     */
    function getTotalsc(){
        var totalsc = 0;
        var rowArr = WfForm.getDetailAllRowIndexStr("detail_1").split(",");
        for(var i=0; i<rowArr.length; i++){
            var rowIndex = rowArr[i];
            if(rowIndex !== ""){
                totalsc = Number(totalsc) + Number(WfForm.getFieldValue(WfForm.convertFieldNameToId("jbsc", "detail_1")+"_"+rowIndex));
            }
        }
        return totalsc;
    }
    //加班类型值变更时，自动生成明细jblx
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("jblx"),function(obj,id,value){
        //自动添加出差明细数据
        adddetail();
    });

    //加班人员值变更时，自动生成明细jblx
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("jbry"),function(obj,id,value){
        //自动添加出差明细数据
        adddetail();
    });

    //加班开始日期值变更时，自动生成明细
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("ksrq"),function(obj,id,value){
        //自动添加出差明细数据
        adddetail();
    });

    //加班结束日期值变更时，自动生成明细
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("jsrq"),function(obj,id,value){
        //自动添加出差明细数据
        adddetail();
    });

    //加班开始时间值变更时，自动生成明细
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("kssj"),function(obj,id,value){
        //自动添加出差明细数据
        adddetail();
    });

    //加班结束时间值变更时，自动生成明细
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("jssj"),function(obj,id,value){
        //自动添加出差明细数据
        adddetail();
    });
</script>


