//员工自助--单人请假单
var leaveTypeNumber = "";
shr.defineClass("shr.ats.AtsLeaveBillEditByPersonExt", shr.ats.AtsLeaveBillEdit, {

    initalizeDOM: function () {
        var self = this;
        shr.ats.AtsLeaveBillEditByPersonExt.superClass.initalizeDOM.call(this);
        if(this.operateState.toUpperCase() != 'VIEW'){
            //初始化隐藏字段
            this.customerInitFileHide();
            this.showColumnByPolicy();
        }else{
            this.customerInitFileHide();
            this.showViewColumn();
        }
        var thisTechingDuties = atsMlUtile.getFieldOriginalValue("entries_TeachingDuties");
        if(thisTechingDuties){
            self.getField("entries_TeachingTask").shrTextField('option', 'required', true);
            self.getField("entries_AlternativeTeacher").shrTextField('option', 'required', true);
        }
        $("#entries_TeachingDuties").on("change", function () {
            var teachingDuties = atsMlUtile.getFieldOriginalValue("entries_TeachingDuties");
            if(teachingDuties){
                self.getField("entries_TeachingTask").shrTextField('option', 'required', true);
                self.getField("entries_AlternativeTeacher").shrTextField('option', 'required', true);
            }else{
                self.getField("entries_TeachingTask").shrTextField('option', 'required', false);
                self.getField("entries_AlternativeTeacher").shrTextField('option', 'required', false);
            }
        });
    },
    
    //初始化假期类型框框信息,包括名称,单位,额度
    initTableAndDiv: function (info) {
        var that = this;
        var attendColl = JSON.parse(info.timeAttendanceCollection);
        var size = info.timeAttendanceCollectionSize;
        var remainValue = info.timeAttendRemainValue; //年假的剩余额度
        //年假剩余额度赋给隐藏域 在保存的时候做判断使用
        $("#entries_msgValue").val(remainValue);

        var table = "";
        table =
            table +
            "<table >" +
            " <tr> " +
            " <td id='info_mess' class='td_typeinfo'> " +
            " </td> " +
            " </tr> " +
            "</table> ";
        $("#message_holidayType").html(table);

        var td_div = "";
        var hideNumber = ["JQLX000012Y","JQLX000008Y","JQLX000009Y","JQLX000013Y","JQLX000014Y","JQLX000018Y"];
        for (var j = 0; j < size; j++) {
            holidayPolicyId = attendColl[j].id;
            attendTypeId = attendColl[j].holidayType.id;
            number = attendColl[j].holidayType.number;
            if(hideNumber.includes(number)){
                continue;
            }
            attendTypeName = attendColl[j].name;
            var leaveRemark = "";
            if (attendColl[j].remark) {
                leaveRemark = attendColl[j].remark;
            }
            //attendUnit = attendColl[j].unitType;//is obj
            var unitTypeName = "";
            var unitTypeValue = ""; //保存的时候 做个校验????
            if (attendColl[j].unit != undefined) {
                unitTypeName = attendColl[j].unit.alias;
                unitTypeValue = attendColl[j].unit.value;
                //that._unitType = unitTypeValue;//单位类型的值
            }
            remainValue_mess = "";
            vacationRemain = info.vacationRemain;
            freeRemain = info.freeRemain;
            var proposerId = $("#entries_person_el").val();
            var hrOrgUnitId = $("#hrOrgUnit_el").val();

            //循环map
            for (var prop in vacationRemain) {
                if (vacationRemain.hasOwnProperty(prop)) {
                    //if(yearHolidayTypeId == prop) // 年假
                    //{
                    if (prop == attendTypeId) {
                        var numberOptions = {
                            decimalPrecision: info.saveLengthMap[prop]
                        };
                        if (vacationRemain[prop] != "") {
                            //remainValue_mess = "<font class='remain_info'>剩余["+ vacationRemain[prop]  +"]"+",在途["+freeRemain[prop]+"]</font>";
                            remainValue_mess =
                                "<font class='remain_info' onclick='showHolidayLimit(" +
                                '"' +
                                proposerId +
                                '","' +
                                hrOrgUnitId +
                                '","' +
                                holidayPolicyId +
                                "\")' >" +
                                jsBizMultLan.atsManager_atsLeaveBillEdit_i18n_45 +
                                "[<span class='remain_info_params'>" +
                                numberfieldService.format(
                                    vacationRemain[prop],
                                    numberOptions
                                ) +
                                "</span>]" +
                                "," +
                                jsBizMultLan.atsManager_atsLeaveBillEdit_i18n_60 +
                                "[<span class='remain_info_params'>" +
                                numberfieldService.format(
                                    freeRemain[prop],
                                    numberOptions
                                ) +
                                "</span>]</font>";
                        } else {
                            //remainValue_mess = "<font class='remain_info' style='display:none;'>剩余["+ vacationRemain[prop]  +"]"+",在途"+freeRemain[prop]+"</font>";
                            remainValue_mess =
                                "<font class='remain_info'  onclick='showHolidayLimit(" +
                                '"' +
                                proposerId +
                                '","' +
                                hrOrgUnitId +
                                '","' +
                                holidayPolicyId +
                                "\")' style='display:none;'>" +
                                jsBizMultLan.atsManager_atsLeaveBillEdit_i18n_45 +
                                "[<span class='remain_info_params'>" +
                                numberfieldService.format(
                                    vacationRemain[prop],
                                    numberOptions
                                ) +
                                "</span>]" +
                                "," +
                                jsBizMultLan.atsManager_atsLeaveBillEdit_i18n_60 +
                                "[<span class='remain_info_params'>" +
                                numberfieldService.format(
                                    freeRemain[prop],
                                    numberOptions
                                ) +
                                "</span>]</font>";
                        }
                    }
                    // }
                }
            }

            if (j >= 4) {
                //如果大于4 设置隐藏 显示更多
                td_div =
                    td_div +
                    "<div id='div" +
                    j +
                    "' style='display:none' title = '" +
                    leaveRemark +
                    "' class='div_blockinfo' onclick='changeColor(" +
                    j +
                    "," +
                    size +
                    ',"' +
                    holidayPolicyId +
                    '","' +
                    attendTypeId +
                    '","' +
                    shr.encryptForAes(attendTypeName) +
                    '","' +
                    unitTypeName +
                    '","' +
                    unitTypeValue +
                    "\")'><font class='attendTypeName_info'>" +
                    attendTypeName +
                    "</font>&nbsp;&nbsp;(" +
                    unitTypeName +
                    ")<br/> " +
                    remainValue_mess +
                    "</div>";
            } else {
                td_div =
                    td_div +
                    "<div id='div" +
                    j +
                    "' class='div_blockinfo' title = '" +
                    leaveRemark +
                    "' onclick='changeColor(" +
                    j +
                    "," +
                    size +
                    ',"' +
                    holidayPolicyId +
                    '","' +
                    attendTypeId +
                    '","' +
                    shr.encryptForAes(attendTypeName) +
                    '","' +
                    unitTypeName +
                    '","' +
                    unitTypeValue +
                    "\")'><font class='attendTypeName_info'>" +
                    attendTypeName +
                    "</font>&nbsp;&nbsp;(" +
                    unitTypeName +
                    ")<br/> " +
                    remainValue_mess +
                    "</div>";
            }
        }
        // 更多按钮
        if (size > 4) {
            td_div =
                td_div +
                "<div id='div" +
                size +
                "' class='div_blockinfo' class='attendTypeName_info'><div style='font-size: 16px;'>" +
                jsBizMultLan.atsManager_atsLeaveBillEdit_i18n_23 +
                "&gt;&gt;</div> </div>";
        }
        $("#info_mess").html(td_div);

        for (var i = 0; i < size; i++) {
            $("#div" + i).bind("click", function () {
                atsMlUtile.setTransNumValue("entries_leaveLength", "");
                //重新计算请假时长
                that.getRealLeaveLengthOfDay();
            });
        }

        //审核界面为编辑状态时
        if (
            that.isFromWF() &&
            that.getOperateState() == "EDIT" &&
            $("#billState").val() != 0
        ) {
            $("#info_mess .div_blockinfo")
                .attr("onclick", "")
                .css("cursor", "default");
        }

        // 注册更多按钮事件
        $("#div" + size).bind("click", function () {
            for (var i = 4; i < size; i++) {
                // 显示大于4的假期类型
                $("#div" + i).show();
            }
            //隐藏更多按钮
            $("#div" + size).hide();

            if (
                that.isFromWF() &&
                that.getOperateState() == "EDIT" &&
                $("#billState").val() != 0
            ) {
                $("#info_mess .div_blockinfo")
                    .attr("onclick", "")
                    .css("cursor", "default");
            }
            //设置默认假期类型
            that.setAttendTypeDefault(attendColl, size, unitTypeName);
        });
        //首次进来 设置默认选中年休假 这个假期类型
        that.setAttendTypeDefault(attendColl, size, unitTypeName); //所有假期类型集合,集合大小
    },
    
    
    /**
     *  初始化隐藏字段
     */
    customerInitFileHide : function(){
        $("#dowFile").hide();
        //教师假（教师旅行假）    
        $("#entries_Teachingperiod").parents(".field-area").hide();
        $("#entries_Teachinginnextsemester").parents(".field-area").hide();
        $("#entries_Startdateonextsemester").parents(".field-area").hide();
        $("#entries_DateofTestB").parents(".field-area").hide();
        $("#entries_DateofTestA").parents(".field-area").hide();
        $("#entries_DateofreturningtoGTIIT").parents(".field-area").hide();
        $("#entries_DateofleavingGTIIT").parents(".field-area").hide();
        //出差假（商务旅行）
        $("#entries_EstimatedTotalCost").parents(".field-area").hide();
        $("#entries_CITY").parents(".field-area").hide();
        $("#entries_Purpose").parents(".field-area").hide();
        //行政人员出差假   
        $("#entries_EmergencyContact").parents(".field-area").hide();
        $("#entries_Destination").parents(".field-area").hide();
        $("#entries_TripReason").parents(".field-area").hide();
        //病假、年假、出差假（商务旅行）
        $("#entries_AlternativeTeacher").parents(".field-area").hide();
        $("#entries_TeachingTask").parents(".field-area").hide();
        $("#entries_TeachingDuties").parents(".field-area").hide();
        $("#prompt").hide();
    },
    
    

    /**
     * 根据选择的假期显示字段
     */
    showColumnByPolicy : function(){
        var self = this;
        $("#entries_policy_el").on("change", function () {
            var policyId = $("#entries_policy_el").val();
            var personId = $("#entries_person_el").val();
            var personType = "";
            shr.callHandler({
                handler : "com.kingdee.shr.ats.web.handler.AtsLeaveBillEditHandlerEx",
                action : 'customerCheckParam',
                async : false,
                type : "POST",
                param : { "policyId" : "","personId" : personId },
                success:function(data){
                    if(data.personType){
                        personType = data.personType;
                    }
                }
            });

            shr.callHandler({
                handler : "com.kingdee.shr.ats.web.handler.AtsLeaveBillEditHandlerEx",
                action : 'getLeaveData',
                async : false,
                type : "POST",
                param : {
                        "policyId" : policyId
                        },
                success:function(data){
                    
                    self.getField("entries_Teachingperiod").shrPromptBox('option', 'required', false);
                    self.getField("entries_Teachinginnextsemester").shrPromptBox('option', 'required', false);
                    self.getField("entries_Startdateonextsemester").shrDateTimePicker('option', 'required', false);
                    self.getField("entries_DateofTestB").shrDateTimePicker('option', 'required', false);                        
                    self.getField("entries_DateofTestA").shrDateTimePicker('option', 'required', false);
                    self.getField("entries_DateofreturningtoGTIIT").shrDateTimePicker('option', 'required', false);
                    self.getField("entries_DateofleavingGTIIT").shrDateTimePicker('option', 'required', false);
                    self.getField("entries_EstimatedTotalCost").shrTextField('option', 'required', false);      
                    self.getField("entries_CITY").shrTextField('option', 'required', false);
                    self.getField("entries_Purpose").shrTextField('option', 'required', false);
                    self.getField("entries_EmergencyContact").shrTextField('option', 'required', false);
                    self.getField("entries_Destination").shrTextField('option', 'required', false);
                    self.getField("entries_TripReason").shrTextField('option', 'required', false);  
                    
                    //教师假（教师旅行假）    
                    $("#entries_Teachingperiod").parents(".field-area").hide();
                    $("#entries_Teachinginnextsemester").parents(".field-area").hide();
                    $("#entries_Startdateonextsemester").parents(".field-area").hide();
                    $("#entries_DateofTestB").parents(".field-area").hide();
                    $("#entries_DateofTestA").parents(".field-area").hide();
                    $("#entries_DateofreturningtoGTIIT").parents(".field-area").hide();
                    $("#entries_DateofleavingGTIIT").parents(".field-area").hide();
                    //出差假（商务旅行）
                    $("#entries_EstimatedTotalCost").parents(".field-area").hide();
                    $("#entries_CITY").parents(".field-area").hide();
                    $("#entries_Purpose").parents(".field-area").hide();
                    //行政人员出差假   
                    $("#entries_EmergencyContact").parents(".field-area").hide();
                    $("#entries_Destination").parents(".field-area").hide();
                    $("#entries_TripReason").parents(".field-area").hide();
                    //病假、年假、出差假（商务旅行）
                    $("#entries_AlternativeTeacher").parents(".field-area").hide();
                    $("#entries_TeachingTask").parents(".field-area").hide();
                    $("#entries_TeachingDuties").parents(".field-area").hide();
                    $("#prompt").hide();
                    $("#dowFile").hide();
                    if(data){
                        leaveTypeNumber = data.number;
                        if(data.number == 'JQLX000001Y' || data.number == 'JQLX000003Y'){
                            //病假、年假
                            if("GTIIT_FACULTY" == personType || "GTIIT_OTHER" == personType){
                                $("#prompt").show();
                                $("#entries_TeachingDuties").parents(".field-area").show();
                                $("#entries_TeachingTask").parents(".field-area").show();
                                $("#entries_AlternativeTeacher").parents(".field-area").show();
                            }
                        }else if(data.number == 'JQLX000017Y' ){
                            //行政人员出差假   
                            $("#entries_EmergencyContact").parents(".field-area").show();
                            $("#entries_Destination").parents(".field-area").show();
                            $("#entries_TripReason").parents(".field-area").show();                     
                            self.getField("entries_EmergencyContact").shrTextField('option', 'required', true);
                            self.getField("entries_Destination").shrTextField('option', 'required', true);
                            self.getField("entries_TripReason").shrTextField('option', 'required', true);
                        }else if(data.number == 'JQLX000020Y' ){
                            //出差假（商务旅行） 
                            $("#prompt").show();
                            $("#entries_TeachingDuties").parents(".field-area").show();
                            $("#entries_TeachingTask").parents(".field-area").show();
                            $("#entries_AlternativeTeacher").parents(".field-area").show();
                            $("#entries_EstimatedTotalCost").parents(".field-area").show();
                            $("#entries_CITY").parents(".field-area").show();
                            $("#entries_Purpose").parents(".field-area").show();
                            
                            self.getField("entries_EstimatedTotalCost").shrTextField('option', 'required', true);
                            self.getField("entries_CITY").shrTextField('option', 'required', true);
                            self.getField("entries_Purpose").shrTextField('option', 'required', true);
                        }else if(data.number == 'JQLX000010Y' ){
                            //教师假（教师旅行假）    
                            $("#dowFile").show();
                            $("#entries_Teachingperiod").parents(".field-area").show();
                            $("#entries_Teachinginnextsemester").parents(".field-area").show();
                            $("#entries_Startdateonextsemester").parents(".field-area").show();
                            $("#entries_DateofTestB").parents(".field-area").show();
                            $("#entries_DateofTestA").parents(".field-area").show();
                            $("#entries_DateofreturningtoGTIIT").parents(".field-area").show();
                            $("#entries_DateofleavingGTIIT").parents(".field-area").show();     
                            self.getField("entries_Teachingperiod").shrPromptBox('option', 'required', true);
                            self.getField("entries_Teachinginnextsemester").shrPromptBox('option', 'required', true);
                            self.getField("entries_Startdateonextsemester").shrDateTimePicker('option', 'required', true);
                            self.getField("entries_DateofTestB").shrDateTimePicker('option', 'required', true);                     
                            self.getField("entries_DateofTestA").shrDateTimePicker('option', 'required', true);
                            self.getField("entries_DateofreturningtoGTIIT").shrDateTimePicker('option', 'required', true);
                            self.getField("entries_DateofleavingGTIIT").shrDateTimePicker('option', 'required', true);
                        }
                    }
                }
            });
        });
        
        $("#entries_realLeaveLength").on("change", function () {
            var realLeaveLength = $("#entries_realLeaveLength").shrNumberField("getValue");
            if(leaveTypeNumber == "JQLX000004Y"){
                realLeaveLength = realLeaveLength/7.5;
            }
            if(realLeaveLength >= 7){
                var personId = $("#entries_person_el").val();
                shr.callHandler({
                    handler : "com.kingdee.shr.ats.web.handler.AtsLeaveBillEditHandlerEx",
                    action : 'customerCheckParam',
                    async : false,
                    type : "POST",
                    param : { "personId" : personId },
                    success:function(data){
                        if( "GTIIT_SAS" == data.personType || "GTIIT_PSS" == data.personType || "GTIIT_GAS" == data.personType){
                            $("#entries_toreasons").shrTextarea('option', 'required', true);   
                            $("#entries_toreasons").parent().parent().find(".field-label").text("Request for leave：During my applied leave,the emergency contact will be ：");
                        }else{
                            $("#entries_toreasons").parent().parent().find(".field-label").text("Request for leave：");
                            $("#entries_toreasons").shrTextarea('option', 'required', false);
                        }
                        if(("GTIIT_SAS" == data.personType || "GTIIT_PSS" == data.personType || "GTIIT_GAS" == data.personType) && leaveTypeNumber == "JQLX000017Y"){
                            $("#entries_toreasons").parent().parent().find(".field-label").text("Request for leave：");
                            $("#entries_toreasons").shrTextarea('option', 'required', false);
                        }
                    }
                
                });
            }else{
                $("#entries_toreasons").parent().parent().find(".field-label").text("Request for leave：");
                $("#entries_toreasons").shrTextarea('option', 'required', false);
            }
            
        });
        
        
        
    },


    showViewColumn : function(){
        var policyId = $("#entries_policy").val();
        var personId = $("#entries_person").val();
        var personType = "";
        shr.callHandler({
            handler : "com.kingdee.shr.ats.web.handler.AtsLeaveBillEditHandlerEx",
            action : 'customerCheckParam',
            async : false,
            type : "POST",
            param : { "policyId" : "","personId" : personId },
            success:function(data){
                if(data.personType){
                    personType = data.personType;
                }
            }
        });
        shr.callHandler({
            handler : "com.kingdee.shr.ats.web.handler.AtsLeaveBillEditHandlerEx",
            action : 'getLeaveData',
            async : false,
            type : "POST",
            param : {
                    "policyId" : policyId
                    },
            success:function(data){
                if(data){
                    if(data.number == 'JQLX000001Y' || data.number == 'JQLX000003Y'){
                        //病假、年假
                        if("GTIIT_FACULTY" == personType || "GTIIT_OTHER" == personType){
                            $("#prompt").show();
                            $("#entries_TeachingDuties").parents(".field-area").show();
                            $("#entries_TeachingTask").parents(".field-area").show();
                            $("#entries_AlternativeTeacher").parents(".field-area").show();
                        }
                    }else if(data.number == 'JQLX000017Y' ){
                        //行政人员出差假   
                        $("#entries_EmergencyContact").parents(".field-area").show();
                        $("#entries_Destination").parents(".field-area").show();
                        $("#entries_TripReason").parents(".field-area").show();                       
                    }else if(data.number == 'JQLX000020Y' ){
                        //出差假（商务旅行） 
                        $("#prompt").show();
                        $("#entries_TeachingDuties").parents(".field-area").show();
                        $("#entries_TeachingTask").parents(".field-area").show();
                        $("#entries_AlternativeTeacher").parents(".field-area").show();
                        $("#entries_EstimatedTotalCost").parents(".field-area").show();
                        $("#entries_CITY").parents(".field-area").show();
                        $("#entries_Purpose").parents(".field-area").show();
                    }else if(data.number == 'JQLX000010Y' ){
                        //教师假（教师旅行假）    
                        $("#dowFile").show();
                        $("#entries_Teachingperiod").parents(".field-area").show();
                        $("#entries_Teachinginnextsemester").parents(".field-area").show();
                        $("#entries_Startdateonextsemester").parents(".field-area").show();
                        $("#entries_DateofTestB").parents(".field-area").show();
                        $("#entries_DateofTestA").parents(".field-area").show();
                        $("#entries_DateofreturningtoGTIIT").parents(".field-area").show();
                        $("#entries_DateofleavingGTIIT").parents(".field-area").show();       
                    }
                }
            }
        });
    },

    dowFileAction:function(){
        shr.callHandler({
            handler : "com.kingdee.customer.util.handler.ConfigurationHandler",
            action : 'getCustomerConfig',
            async : false,
            param : { "fnumber" : "1001" },
            success:function(data){
                if(data){
                    // 创建一个虚拟的链接元素
                    var downloadLink = document.createElement('a');
                    downloadLink.download = data.name;
                    downloadLink.href = data.ip+"/leaveTemplate/"+data.name;
                    document.body.appendChild(downloadLink);
                    downloadLink.click();
                    document.body.removeChild(downloadLink);
                }else{
                    shr.showInfo({message: "No teacher fake template is configured !"});
                }
            }
        });

        // var url = "C:/Users/86133/Desktop/证明.pdf"
        // var urlinner = shr.getContextPath() + shr.dynamicURL + "?downloadUrl="+url+"&method=getCustomerConfig&handler=com.kingdee.customer.util.handler.ConfigurationHandler"+"&uipk="+shr.getUrlRequestParam('uipk');
        // var downloadLink = document.createElement('a');
        // //downloadLink.download = data.name;
        // downloadLink.href = urlinner;
        // document.body.appendChild(downloadLink);
        // downloadLink.click();
        // document.body.removeChild(downloadLink);
    }

    
});
