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

<script>

    //自动更新加班时长
    var autodetail = true;

    /**
     * 计算加班时长
     */
    function calculate() {
        //开始时间
        var kssj = WfForm.getFieldValue(WfForm.convertFieldNameToId("kssj"));
        //结束时间
        var jssj = WfForm.getFieldValue(WfForm.convertFieldNameToId("jssj"));
        var v_hours = (new Date("2000-01-01 "+jssj).getTime() - new Date("2000-01-01 "+kssj).getTime())/(1000 * 60 * 60);
        WfForm.changeFieldValue(WfForm.convertFieldNameToId("jbsc"), {value:v_hours});
    }

    //开始日期发生变更后，自动重新计算加班时长
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("ksrq"),function(obj,id,value){
        if (autodetail){
            autodetail = false;
            //开始日期
            var ksrq = new Date(WfForm.getFieldValue(WfForm.convertFieldNameToId("ksrq")));
            //结束日期
            var jsrq = new Date(WfForm.getFieldValue(WfForm.convertFieldNameToId("jsrq")));
            if (ksrq > jsrq) {
                WfForm.changeFieldValue(WfForm.convertFieldNameToId("jsrq"), {value:ksrq});
            }
            //计算加班时长
            calculate();
            autodetail = true;
        }
    });

    //开始时间发生变更后，自动重新计算加班时长
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("kssj"),function(obj,id,value){
        if (autodetail){
            autodetail = false;
            //计算加班时长
            calculate();
            autodetail = true;
        }
    });

    //结束日期发生变更后，自动重新计算加班时长
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("jsrq"),function(obj,id,value){
        if (autodetail){
            autodetail = false;
            //开始日期
            var ksrq = new Date(WfForm.getFieldValue(WfForm.convertFieldNameToId("ksrq")));
            //结束日期
            var jsrq = new Date(WfForm.getFieldValue(WfForm.convertFieldNameToId("jsrq")));
            if (ksrq > jsrq) {
                WfForm.changeFieldValue(WfForm.convertFieldNameToId("ksrq"), {value:jsrq});
            }
            //计算加班时长
            calculate();
            autodetail = true;
        }
    });

    //结束时间发生变更后，自动重新计算加班时长
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("jssj"),function(obj,id,value){
        if (autodetail){
            autodetail = false;
            //计算加班时长
            calculate();
            autodetail = true;
        }
    });

    //结束时间发生变更后，自动重新计算加班时长
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("dxsc"),function(obj,id,value){
        var jbsc = WfForm.getFieldValue(WfForm.convertFieldNameToId("jbsc"));
        if (Number(jbsc) < Number(value)){
            alert("调休时长不得大于加班时长");
            return;
        }
        WfForm.changeFieldValue(WfForm.convertFieldNameToId("jxsc"), {value:Number(jbsc)-Number(value)});
    });

    //结束时间发生变更后，自动重新计算加班时长
    WfForm.bindFieldChangeEvent(WfForm.convertFieldNameToId("dxsc"),function(obj,id,value){
        var jbsc = WfForm.getFieldValue(WfForm.convertFieldNameToId("jbsc"));
        if (Number(jbsc) < Number(value)){
            alert("调休时长不得大于加班时长");
            return;
        }
        WfForm.changeFieldValue(WfForm.convertFieldNameToId("jxsc"), {value:Number(jbsc)-Number(value)});
    });
</script>



