
var deletedList = ""; //用于存储在数据库中还存在，但是在页面上已删除的分录ID
shr.defineClass("shr.custom.FillSignCardEditEx", shr.ats.FillSignCardEdit, {
    strdate: "",
    initalizeDOM: function () {
        shr.custom.FillSignCardEditEx.superClass.initalizeDOM.call(this);
        var that = this;
        // that.processPersonF7ChangeEvent();
        // //that.createPersonExistAttenFileF7();
        // //增加业务组织处理
        // that.processF7ChangeEventHrOrgUnit();
        // that.setButtonVisible();
        // that.strdate = $.getUrlParam("strdate");
        // that.myExtendValidate(); //使用自定义的校验扩展校验
        // that.myExtendValidate1(); //用自定义的校验扩展校验考勤日期
        // //新增的补签卡自动填充申请人和申请日期
        // that.fillProposerAndApplyDate();
        // //隐藏提交生效按钮
        // if (that.getOperateState() == "VIEW") {
        //     $("#submitEffect").hide();
        // }
        // /*
        // 龙光没有此按钮
        // //隐藏提交生效按钮
        // $("#submitEffect").hide();
        // */
        // // that.initEditGrid();
        // that.dealWindownLoad();
        // that.setNumberFieldEnable();
        // if (that.getOperateState() != "ADDNEW") {
        //     that.handleFSCEntry();
        // }

        // $(".ui-state-default").unbind("mouseenter");
        // $(".ui-state-default").unbind("mouseleave");
        // $(".ui-state-default").die("mouseenter");
        // $(".ui-state-default").die("mouseleave");

        // $(".ui-state-default")
        //     .live("mouseenter", function () {
        //         $(this).addClass("ui-state-hover");
        //     })
        //     .live("mouseleave", function () {
        //         $(this).removeClass("ui-state-hover");
        //         $(".ui-state-hover").removeClass("ui-state-hover");
        //     });

        // if (that.isFromWF()) {
        //     $("#addInstanceToDeskItem").css("display", "none");
        // }
        // that.initCcPersonPrompt();
        // 延迟3秒后执行一个匿名函数
        setTimeout(function () {
            that.exeValChangeEvent()

            var reasonInputs = $('input[name^="reason"]');
            reasonInputs.each(function () {
                var title = $(this).val()
                const numbers = $(this).attr("id").match(/\d+/g);
                var num = numbers[0];
                if (title == "其它") {
                    // $("#remark" + num).attr("disabled", true).addClass("disabled");
                    $("#remark" + num).attr('style', 'background-color:#daeef8');
                    $("#remark" + num).attr('validate', "{required:true,maxlength:128}");
                } else {
                    $("#remark" + num).attr('style', 'padding-right: 0px;');
                    $("#remark" + num).attr('validate', "{maxlength:128}");
                    $('[for="remark' + num + '"]').remove();
                    // $("#remark" + num).removeAttr('background-color');
                }
            });
        }, 1000);

    },
    exeValChangeEvent: function () {
        $('.ui-promptBox-inputframe input[type="text"][ctrlrole="promptBox"]').live('change', function (even, val) {
            var thisId = even.currentTarget.id
            if (thisId.indexOf("reason") !== -1) {

                var title = $('#' + thisId).attr("title")
                const numbers = thisId.match(/\d+/g);
                var num = numbers[0];
                if (title == "其它") {
                    // $("#remark" + num).attr("disabled", true).addClass("disabled");
                    $("#remark" + num).attr('style', 'background-color:#daeef8');
                    $("#remark" + num).attr('validate', "{required:true,maxlength:128}");
                } else {
                    $("#remark" + num).attr('style', 'padding-right: 0px;');
                    $("#remark" + num).attr('validate', "{maxlength:128}");
                    $('[for="remark' + num + '"]').remove();
                    // $("#remark" + num).removeAttr('background-color');
                }
            }

        });
    },
    handleFSCEntry: function (hrOrgUnitId) {
        var that = this;
        if (that.getOperateState() == "ADDNEW") {
            var data = {
                uipk: "com.kingdee.eas.hr.ats.app.FillSignCardAllForm",
                hrOrgUnitId: hrOrgUnitId
            };
            shr.doAjax({
                url: shr.getContextPath() + "/dynamic.do?method=addItemJson",
                dataType: "json",
                type: "POST",
                data: data,
                success: function (response) {
                    if ($("input[name^=reason]").length == 0) {
                        that.setFSCEntry(response);
                    }
                    that.handleFSCEntryReason(response);
                }
            });
        } else if (
            that.getOperateState() == "VIEW" ||
            that.getOperateState() == "EDIT"
        ) {
            var ids = $("form[id^=form]").find("input[id^=id]").val();
            var data = {
                id: ids || "",
                uipk: "com.kingdee.eas.hr.ats.app.FillSignCardAllForm",
                hrOrgUnitId: hrOrgUnitId
            };
            shr.doAjax({
                url: shr.getContextPath() + "/dynamic.do?method=getItemsJson",
                dataType: "json",
                type: "POST",
                data: data,
                success: function (response) {
                    if ($("input[name^=reason]").length == 0) {
                        that.setFSCEntry(response);
                    }
                    //that.handleFSCEntryReason(response);
                }
            });
        } else {
            //nothing
        }
    },
    setFSCEntry: function (rst) {
        var _self = this;
        if (
            rst.records != null &&
            rst.records > 0 &&
            rst.rows != null &&
            rst.rows.length > 0
        ) {
            var id,
                attendDate,
                type,
                fillCardTimeStr,
                remark,
                reason_id,
                reason_name;
            for (var i = 0; i < rst.rows.length; i++) {
                var row = rst.rows[i];
                (id = shr.xssFilter(row["id"])),
                    (attendDate = shr.xssFilter(row["attendDate"])),
                    (type_key = shr.xssFilter(row["type.key"])),
                    (type_value = shr.xssFilter(row["type.value"])),
                    (fillCardTimeStr = shr.xssFilter(row["fillCardTimeStr"])),
                    (reason_id = shr.xssFilter(row["reason.id"])),
                    (reason_name = shr.xssFilter(row["reason.name"])),
                    (remark = shr.xssFilter(row["remark"]));
                var row_fields_work = "";
                if (
                    _self.getOperateState() == "ADDNEW" ||
                    _self.getOperateState() == "EDIT"
                ) {
                    row_fields_work =
                        '<div  class="row-fluid row-block row_field">' +
                        '<div class="spanSelf">' +
                        '<input type="hidden" name="id' +
                        i +
                        '" value="' +
                        correctValue(id) +
                        '" />' +
                        '<input type="text" id="attendDate' +
                        i +
                        '" name="attendDate' +
                        i +
                        '" value="' +
                        correctValue(attendDate) +
                        '"class="input-height cell-input" validate="{required:true}" />' +
                        "</div>" +
                        '<div class="spanSelf"><input type="text"  name="type' +
                        i +
                        '" value="" class="input-height cell-input"    validate="{required:true}"/></div>' +
                        '<div class="spanSelf"><input  type="text" name="reason' +
                        i +
                        '" value="" class="input-height cell-input"   validate="{required:true}"/></div>' +
                        '<div class="spanSelf"><input style="background-color:#daeef8"  length="5" type="text" id="fillCardTimeStr' +
                        i +
                        '" name="fillCardTimeStr' +
                        i +
                        '" value="' +
                        correctValue(fillCardTimeStr) +
                        '" class="input-height cell-input fillCardTime"  placeholder="' +
                        jsBizMultLan.atsManager_fillSignCardEdit_i18n_17 +
                        '"  /></div>' +
                        //						+ '<script type="text/javascript">$(function() {var text_json = {id:"fillCardTimeStr0",name: "fillCardTimeStr0",readonly: "",value: "0",validate: "{required:true,myTmVldt:true}",onChange: null};$("#fillCardTimeStr0").shrTextField(text_json);});</script>'
                        '<div class="spanSelf"><input  maxlength="255" type="text" name="remark' +
                        i +
                        '" value="' +
                        correctValue(remark) +
                        '" class="input-height cell-input"/></div>';
                    if (i == 0) {
                        row_fields_work +=
                            '<div><a class="rowAdd cursor-pointer" style="font-size: 20px;">+</a></div>';
                    } else {
                        row_fields_work +=
                            '<div><a class="rowAdd cursor-pointer" style="font-size: 20px;">+</a><a class="rowDel cursor-pointer" style="font-size: 17px;">x</a></div>';
                    }
                    row_fields_work += "</div>";
                    $("#fillSignCardEntryInfo").append(row_fields_work);
                    $(".fillCardTime").on("input", function (e) {
                        var target = e.target;
                        var val = $(target).val() || "";
                        var hasSignal =
                            val.indexOf(":") !== -1 || val.indexOf("：") !== -1;
                        if (
                            val.length === 4 &&
                            /^\d+$/.test(val) &&
                            !hasSignal
                        ) {
                            var hour = val.slice(0, 2);
                            val = hour + ":" + val.slice(2);
                            if (hour < 24) {
                                $(target).val(val);
                            }
                            return;
                        }
                        if (
                            val.length >= 5 &&
                            /^(\d{2}[:|：]\d{2,})+$/.test(val)
                        ) {
                            var hour = val.slice(0, 2);
                            val = val.slice(0, 5);
                            if (hour < 24) {
                                $(target).val(val);
                            }
                            return;
                        }
                    });
                    var remarkObj = { l1: "", l2: "", l3: "" };
                    remarkObj.l1 = correctValue(shr.xssFilter(row["remark_l1"]));
                    remarkObj.l2 = correctValue(shr.xssFilter(row["remark_l2"]));
                    remarkObj.l3 = correctValue(shr.xssFilter(row["remark_l3"]));
                    _self.addRowFieldString(
                        false,
                        i,
                        reason_id,
                        reason_name,
                        type_key,
                        type_value,
                        remarkObj
                    );
                } else if (_self.getOperateState() == "VIEW") {
                    row_fields_work =
                        '<div class="row-fluid row-block row_field">' +
                        '<div class="spanSelf">' +
                        '<input type="hidden" name="id' +
                        i +
                        '" value="' +
                        correctValue(id) +
                        '" />' +
                        '<input type="hidden" id="type' +
                        i +
                        '_el" value="' +
                        correctValue(type_value) +
                        '" />' +
                        '<input type="hidden" id="reason' +
                        i +
                        '_el" value="' +
                        correctValue(reason_id) +
                        '" /> ' +
                        '<span id="attendDate' +
                        i +
                        '" name="attendDate' +
                        i +
                        '" class="cell-input">' +
                        correctValue(attendDate) +
                        "</span>" +
                        "</div>" +
                        '<div class="spanSelf"><span name="type' +
                        i +
                        '" class="cell-input">' +
                        correctValue(type_key) +
                        "</span></div>" +
                        '<div class="spanSelf"><span name="reason' +
                        i +
                        '" class="cell-input">' +
                        correctValue(reason_name) +
                        "</span></div>" +
                        '<div class="spanSelf"><span name="fillCardTimeStr' +
                        i +
                        '"  class="cell-input">' +
                        correctValue(fillCardTimeStr) +
                        "</span></div>" +
                        '<div class="spanSelf"><span name="remark' +
                        i +
                        '" style = "word-break: break-word;" class="cell-input">' +
                        correctValue(remark) +
                        "</span></div>" +
                        "</div>";
                    $("#fillSignCardEntryInfo").append(row_fields_work);
                }
                atsMlUtile.setTransDateValue("attendDate" + i, attendDate);
                _self.initAtsBillExplain();
            }
        }
        //添加事件处理
        //新增
        $("#fillSignCardEntryInfo a.rowAdd").die("click");
        $("#fillSignCardEntryInfo a.rowAdd").live("click", function () {
            var vali = $(
                "#fillSignCardEntryInfo .row_field:last input[name^=attendDate]"
            ).attr("name");
            if (vali != null && vali != "") {
                var idx = new String(vali).substr(10);
                var idxA = new Number(idx) + 1;
                _self.addRowFieldString(
                    true,
                    idxA,
                    null,
                    null,
                    null,
                    null,
                    null
                );
            }
        });

        //删除
        $("#fillSignCardEntryInfo a.rowDel").die("click");
        $("#fillSignCardEntryInfo a.rowDel").live("click", function () {
            var entryId = $(
                $(this).closest("div.row_field").children()[0]
            ).children()[0].value;
            if (entryId != "" && entryId != null && entryId != undefined) {
                deletedList += entryId + ",";
            }
            $(this).closest("div.row_field").remove();
        });
    },
    addRowFieldString: function (
        flag,
        i,
        reason_id,
        reason_name,
        type_key,
        type_value,
        remark
    ) {
        if (flag) {
            var row_fields_work =
                '<div  class="row-fluid row-block row_field">' +
                '<div class="spanSelf"><input type="hidden" name="id' +
                i +
                '" value="" /><input type="text" id="attendDate' +
                i +
                '"  name="attendDate' +
                i +
                '" value=""class="input-height cell-input" validate="{required:true}" /></div>' +
                '<div class="spanSelf"><input name="type' +
                i +
                '_el" type="hidden"/><input type="text"  name="type' +
                i +
                '" value="" class="input-height cell-input"    validate="{required:true}"/></div>' +
                '<div class="spanSelf"><input type="text" name="reason' +
                i +
                '" value="" class="input-height cell-input"   validate="{required:true}"/></div>' +
                '<div class="spanSelf"><input style="background-color:#daeef8"  length="5" type="text" name="fillCardTimeStr' +
                i +
                '" value="" class="input-height cell-input fillCardTime"  placeholder="' +
                jsBizMultLan.atsManager_fillSignCardEdit_i18n_17 +
                '" /></div>' +
                '<div class="spanSelf"><input maxlength="255" type="text" name="remark' +
                i +
                '" value="" class="input-height cell-input"/></div>' +
                '<div><a class="rowAdd cursor-pointer" style="font-size: 20px;">+</a><a class="rowDel cursor-pointer" style="font-size: 17px;">x</a></div>' +
                "</div>";

            $("#fillSignCardEntryInfo").append(row_fields_work);
            $(".fillCardTime").on("input", function (e) {
                var v = $(e.target).val() || "";
                if (/[0-5][0-9][0-5][0-9]/.test(v) && v.length == 4) {
                    v =
                        $(e.target).val().substring(0, 2) +
                        ":" +
                        $(e.target).val().substring(2, 4);
                }
                if (
                    (/[0-2][0-9]:[0-5][0-9]/.test(v) && v.length == 5) ||
                    (/[0-2][0-9]：[0-5][0-9]/.test(v) && v.length == 5)
                ) {
                    var h = new Number(v.substr(0, 2));
                    if (h < 24) {
                        v =
                            $(e.target).val().substring(0, 2) +
                            ":" +
                            $(e.target).val().substring(3, 5);
                        $(e.target).val(v);
                    }
                }
            });
        }
        $('input[name="fillCardTimeStr' + i + '"]').attr(
            "validate",
            "{required:true,myTmVldt:true}"
        );
        $('input[name="attendDate' + i + '"]').attr(
            "validate",
            "{required:true,myTmVldt1:true}"
        );

        //将【考勤日期】 【补签卡类型】【补签卡原因】封装成F7
        //备注改成多语言
        $('input[name="remark' + i + '"]')
            .attr("id", "remark" + i)
            .shrMultiLangBox({
                readonly: "",
                value: remark,
                validate: "{maxlength:128}",
                trimAll: false,
                onChange: null
            });
        //var mulHtml = '<div data-ctrlrole="labelContainer" class="field-area flex-c field-basis2" style=""><div class="field-ctrl flex-c" style="width: unset; max-width: 100%;"><input style="width:248px;height:22px;" id="remark'+i+'" name="remark'+i+'" value="{&quot;l1&quot;:&quot;&quot;,&quot;l2&quot;:&quot;&quot;,&quot;l3&quot;:&quot;&quot;}"></div><script language="javascript">$(function() {var options = {id: "remark'+i+'",readonly: "",validate: "{maxlength:255}",style: "",trimAll:false,isMultiLan:true};$("#remark'+i+'").shrTextarea(options);});</script></div></div>';
        //$("input[name='remark" + i + "']").parent().html(mulHtml);
        //$("#remark"+i).parents(".supportMultiLang").prop("style","width:248px;height:22px;");

        //针对从【考勤看板--我要补卡】中进入
        if (
            this.strdate != null &&
            this.strdate != undefined &&
            this.strdate != ""
        ) {
            $('input[name="attendDate' + i + '"]').shrDateTimePicker(
                "setValue",
                this.strdate
            );
        }
        //【补签卡类型】
        var that = this;
        var select_json = {
            id: "type" + i,
            readonly: "",
            value: "0",
            onChange: null,
            validate: "{required:true}",
            filter: ""
        };
        select_json.data = [
            {
                value: 1,
                alias: jsBizMultLan.atsManager_fillSignCardEdit_i18n_1
            }
        ];
        $('input[name="type' + i + '"]').shrSelect(select_json);
        if (!flag) {
            $('input[name="type' + i + '"]').val(type_key);
            $('input[name="type' + i + '_el"]').val(type_value);
        } else {
            //如果是新增，默认的就是补卡
            $('input[name="type' + i + '"]').val(
                jsBizMultLan.atsManager_fillSignCardEdit_i18n_1
            );
            $('input[name="type' + i + '_el"]').val(1);
        }

        //【考勤日期】
        var attendDate = atsMlUtile.getFieldOriginalValue("attendDate" + i);
        $('input[id="attendDate' + i + '"]').shrDateTimePicker({
            id: "attendDate" + i,
            tagClass: "block-father input-height",
            readonly: "",
            yearRange: "",
            ctrlType: "Date",
            value: attendDate,
            isAutoTimeZoneTrans: false,
            validate: "{dateISO:true,required:true}",
            onChange: function () {
                if (0 == i) {
                    that.initAtsBillExplain();
                }
            },
            isNewDateCtrl: true
        });

        //【补签卡原因】
        var grid_f7_json = { id: "reason" + i, name: "reason" + i };
        grid_f7_json.subWidgetName = "shrPromptGrid";
        var object = $('input[name="reason' + i + '"]');
        grid_f7_json.subWidgetOptions = {
            title: jsBizMultLan.atsManager_fillSignCardEdit_i18n_4,
            uipk: "com.kingdee.eas.hr.ats.app.FillSignReason.AvailableList.F7",
            query: ""
        };
        grid_f7_json.subWidgetOptions.isHRBaseItem = true;
        grid_f7_json.subWidgetOptions.filterConfig = [
            {
                name: "isComUse",
                value: true,
                alias: jsBizMultLan.atsManager_fillSignCardEdit_i18n_20,
                widgetType: "checkbox"
            }
        ];
        grid_f7_json.subWidgetOptions.bizFilterFields = "hrOrgUnit";
        grid_f7_json.subWidgetOptions.f7ReKeyValue =
            "BaseInfo.id:BaseInfo.name";
        grid_f7_json.subWidgetName = "specialPromptGrid";
        grid_f7_json.validate = "{required:true}";
        object.shrPromptBox(grid_f7_json);

        if (!flag) {
            object.val(correctValue(reason_name));
            //利用F7de特性来设置值
            $("#reason" + i + "_el").val(correctValue(reason_id));
        } else {
            //新增行时，补签卡原因取自上一行
            var preRow = i - 1;
            $("#reason" + i + "_el").val(
                correctValue($("#reason" + preRow + "_el").val())
            );
            $("#reason" + i).val(correctValue($("#reason" + preRow).val()));
            if ($("#reason" + preRow).val() == "其它") {
                $("#remark" + i).attr('style', 'background-color:#daeef8');
                $("#remark" + i).attr('validate', "{required:true,maxlength:128}");
            }

        }
    },
})
