var EnterpriseCenter = {
    id: "table", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};

EnterpriseCenter.changePwd = function () {
    var password = $("#tab-3").find("input[name='password']").val();
    var newPassword = $("#tab-3").find("input[name='newPassword']").val();
    var newRePassword = $("#tab-3").find("input[name='newRePassword']").val();
    if (password == null || password == '') {
        Feng.info("请填写原密码!");
        return;
    }
    if (newPassword == null || newPassword == '') {
        Feng.info("请填写新密码!");
        return;
    }
    if (newRePassword == null || newRePassword == '') {
        Feng.info("请填写重复新密码!");
        return;
    }
    if (newPassword != newRePassword) {
        console.log(newPassword, newRePassword)
        Feng.info("两次新密码填写不一致!");
        return;
    }
    var da = {"password": password, "newPassword": newPassword};
    var ajax = new $ax(Feng.ctxPath + "/common/api/changePwd", function (data) {
        Feng.info(data.msg);
        if (data.code == 200) {
            $("#tab-3").find("input[name='password']").val('');
            $("#tab-3").find("input[name='newPassword']").val('');
            $("#tab-3").find("input[name='newRePassword']").val('');
        }
    }, function (data) {
        Feng.error("操作失败!" + data.responseJSON.message + "!");
    });
    ajax.set(da);
    ajax.start();
};

EnterpriseCenter.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '更新单位名称', field: 'newName', align: 'left', width: 120, valign: 'middle', 'class': 'uitd_showTip'},
        {title: '更新统一社会信用代码', field: 'newIdCard', align: 'center', width: 160, valign: 'middle', 'class': 'uitd_showTip'},
        {title: '更新产业领域', field: 'newIndustryFieldNewName', align: 'center', width: 160, valign: 'middle', 'class': 'uitd_showTip'},
        {title: '更新单位标签', field: 'newEnterpriseType', align: 'left', width: 120, valign: 'middle', 'class': 'uitd_showTip'},
        {title: '更新法人代表', field: 'newLegal', align: 'left', width: 100, valign: 'middle', 'class': 'uitd_showTip'},
        {title: '更新所属街道', field: 'newStreetName', align: 'left', width: 100, valign: 'middle', 'class': 'uitd_showTip'},
        {title: '更新单位地址', field: 'newAddress', align: 'left', width: 200, valign: 'middle', 'class': 'uitd_showTip'},
        {title: '审核状态', field: 'checkState', align: 'center', width: 120, valign: 'middle', 'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                if (value == null || value == '') {
                    return "";
                } else if (value == 1) {
                    return "保存未提交";
                } else if (value == 2) {
                    return "待审核";
                } else if (value == 3) {
                    return "审核驳回";
                } else if (value == 4) {
                    return "审核通过";
                } else if (value == 5) {
                    return "重新提交";
                } else if (value == 6) {
                    return "初审驳回";
                } else if (value == 7) {
                    return "初审通过";
                } else {
                    return "";
                }
            },
            cellStyle: function (value, row, index) {
                if (value == null || value == '') {
                    return {css: {}};
                } else if (value == 1) {
                    return {css: {}};
                } else if (value == 2) {
                    return {css: {"background-color": "LightGrey"}};
                } else if (value == 3) {
                    return {css: {"background-color": "Orange"}};
                } else if (value == 4) {
                    return {css: {'background-color': 'LightGreen'}};
                } else if (value == 5) {
                    return {css: {'background-color': 'LightGrey'}};
                } else if (value == 6) {
                    return {css: {"background-color": "Orange"}};
                } else if (value == 7) {
                    return {css: {'background-color': 'LightBlue'}};
                } else {
                    return {css: {}};
                }
            }
        },
        {title: '申请时间', field: 'createTime', align: 'center', width: 170, valign: 'middle', 'class': 'uitd_showTip'},
        {title: '操作', field: 'id', visible: true, align: 'center', valign: 'middle', width: "80px",
            formatter: function (value, row, index) {
                return "<span class='label label-success' onclick=\"EnterpriseCenter.showLog('" + value + "')\" >" +
                        "<i class=\"fa fa-book\"></i>日志" +
                        "</span>";
            }
        }
    ];
};

EnterpriseCenter.addEnterprisechangeRecord = function () {
    //查询是否存在  未处理完的 变更申请
    var ajax = new $ax(Feng.ctxPath + "/enterprise/api/findUnfinishedChangeRecord", function (data) {
        if (data == null || data.length == 0) {
            var index = layer.open({
                type: 2,
                title: '申请机构信息变更',
                area: ['930px', '600px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/enterprise/api/toAdd',
                btn: ['<i class="fa fa-save"></i>&nbsp;&nbsp;保存未提交', '<i class="fa fa-check"></i>&nbsp;&nbsp;提交审核', '<i class="fa fa-eraser"></i>&nbsp;&nbsp;取消'],
                btnAlign: 'c',
                btn1: function (index, layero) {
                    //按钮【按钮一】的回调
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    iframeWin.EpChangeEdit.addSubmit();
                },
                btn2: function (index, layero) {
                    //按钮二的回调
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    iframeWin.EpChangeEdit.submitToCheck();
                    return false;
                }
            });
            layer.full(index);
            EnterpriseCenter.layerIndex = index;
        } else {
            Feng.info("您还有 " + data.length + " 条企业变更申请没有处理完毕， 不能重新提交变更申请!");
        }
    }, function (data) {
        Feng.error("操作失败!" + data.responseJSON.message + "!");
    });
    ajax.set(null);
    ajax.start();
};

EnterpriseCenter.updateEnterprisechangeRecord = function () {
    if (!EnterpriseCenter.check()) {
        return;
    }
    var item = EnterpriseCenter.seItem;
    if (item.checkState != null && item.checkState == 4) {                              //如果已经审核通过了
        Feng.info("该申请已经审核通过，不能再修改!");
        return;
    }
    var index = layer.open({
        type: 2,
        title: '申请机构信息变更',
        area: ['930px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/enterprise/api/toUpdate?id=' + item.id,
        btn: ['<i class="fa fa-save"></i>&nbsp;&nbsp;保存未提交', '<i class="fa fa-check"></i>&nbsp;&nbsp;提交审核', '<i class="fa fa-eraser"></i>&nbsp;&nbsp;取消'],
        btnAlign: 'c',
        btn1: function (index, layero) {
            //按钮【按钮一】的回调
            var iframeWin = window[layero.find('iframe')[0]['name']];
            iframeWin.EpChangeEdit.addSubmit();
        },
        btn2: function (index, layero) {
            //按钮二的回调
            var iframeWin = window[layero.find('iframe')[0]['name']];
            iframeWin.EpChangeEdit.submitToCheck();
            return false;
        }
    });
    layer.full(index);
    EnterpriseCenter.layerIndex = index;
};

EnterpriseCenter.check = function () {
    var selected = $('#' + EnterpriseCenter.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的一条变更记录！");
        return false;
    } else {
        EnterpriseCenter.seItem = selected[0];
        return true;
    }
};

EnterpriseCenter.showEnterprisechangeRecordDetail = function () {
    if (!EnterpriseCenter.check()) {
        return;
    }
    var index = layer.open({
        type: 2,
        title: '机构信息变更详情',
        area: ['930px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/enterprise/api/toDetail?id=' + EnterpriseCenter.seItem.id,
        btn: ['<i class="fa fa-eraser"></i>&nbsp;&nbsp;关闭'],
        btn1: function (index, layero) {
            layer.close(index);
        },
    });
    layer.full(index);
    EnterpriseCenter.layerIndex = index;
};

/**
 * 显示日志
 * @param id
 */
EnterpriseCenter.showLog = function (id) {
    layer.open({
        type: 1,
        title: "日志",
        fixed: false,
        content: '<table id="' + id + '"></table>',
        area: ['80%', '80%'],
        maxmin: true,
        success: function (layero, index) {
            Feng.getCheckLog(id, {"type": 10, "mainId": id, "typeFileId": "", "active": 1})
        }
    });
}

/**
 * 发送验证码
 */
EnterpriseCenter.getEnterpriseSms = function () {
    var agentPhone = $("#tab-1").find("input[name='agentPhone']").val();
    if (agentPhone == null || agentPhone == "") {
        Feng.info("人才联络员手机号码为空!");
        return;
    }
    agentPhone = $.trim(agentPhone);
    if (!Feng.checkMobilePhoneNum(agentPhone)) {
        Feng.info("手机号格式有误，请填写正确的手机号码!");
        return;
    }
    EnterpriseCenter.enterpriseBtnCountDown();
    var ajax = new $ax(Feng.ctxPath + "/api/common/verificationCode?phone=" + agentPhone + "&type=1", function (data) {
        Feng.info(data.msg);
    }, function (data) {
        Feng.error("操作失败!" + data.responseJSON.message + "!");
    });
    ajax.set(null);
    ajax.start();
}

var enterpriseCountDown = 0;
EnterpriseCenter.enterpriseBtnCountDown = function () {
    if (enterpriseCountDown == 0) {
        enterpriseCountDown = 60;
    } else if (enterpriseCountDown == 1) {
        $("#enterprise_sms_btn").removeAttr("disabled");
        $("#enterprise_sms_btn").html("获取验证码");
        enterpriseCountDown = 0;
        return;
    }
    $("#enterprise_sms_btn").attr("disabled", true);
    $("#enterprise_sms_btn").html(enterpriseCountDown + "秒");
    enterpriseCountDown--;
    setTimeout("EnterpriseCenter.enterpriseBtnCountDown()", 1000);
}

/**
 * 绑定聚才网账号
 */
EnterpriseCenter.bindJcAccount = function () {
    var username = $("#username").val();
    var password = $("#password").val();
    var verificationCode = $("#verificationCode").val();
    if (username == null || username == '') {
        Feng.info("聚才网账号不能为空");
        return;
    }
    if (password == null || password == '') {
        Feng.info("聚才网账号密码不能为空");
        return;
    }
    if (verificationCode == null || verificationCode == '') {
        Feng.info("验证码不能为空");
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/api/jucaiInterface/bindAccount", function (data) {
        Feng.info(data.msg);
    }, function (data) {
        Feng.error("操作失败!" + data.responseJSON.message + "!");
    });
    ajax.setData({"userType": 1, "username": username, "password": password, "verificationCode": verificationCode})
    ajax.start();
}

var lock = false;
EnterpriseCenter.closeAccount = function () {
    Feng.confirm("是否确定提交注销申请？", function () {
        var reason = $("#reason").val().trim();
        if (reason == "") {
            Feng.info("注销原因不能为空");
            return;
        }
        if (typeof $("#fileList ul li") == "undefined" && document.getElementById("close-uploader").files.length == 0) {
            Feng.info("请上传附件");
            return;
        }
        if (!lock) {
            lock = true;
            $("#close-uploader-btn").prop("disabled", true);
        } else {
            Feng.info("不能重复点击");
            return;
        }
        $("#EpCloseForm")[0].submit();
    })
}
EnterpriseCenter.setDisabled = function () {
    $("#reason").prop("readonly", true);
    $("#close-uploader-btn").prop("disabled", true);
    $("#closeAccountBtn").prop("disabled", true).text("审核中");
}

EnterpriseCenter.callback = function (data) {
    lock = false;
    if (data.code == 200) {
        Feng.success(data.msg);
        EnterpriseCenter.setDisabled();
        $("#closeAccountState").val(data.checkState);
    } else {
        Feng.error(data.msg);
        $("#close-uploader-btn").removeAttr("disabled");
    }
}

$("#close-uploader").change(function (e) {
    var that = this;
    var files = that.files;
    var totalSize = 0;
    for (var i = 0; i < files.length; i++) {
        if (!Feng.chkFileInvalid(files[i], 5, 10)) {
            return;
        }
        totalSize += files[i].size;
    }
    if (totalSize > 10 * 1024 * 1024) {
        Feng.error("所有附件大小不能超过10MB");
        return;
    }
    $("#fileList ul").html("");
    for (var i = 0; i < files.length; i++) {
        var file = files[i];
        var imgSrc;
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function () {
            if (Feng.isImg(file.type)) {
                imgSrc = this.result;
                var li = '<li><img src="' + imgSrc + '"/></li>';
                $("#fileList ul").append(li);
            } else {
                var li = '<li><img src="/static/img/Pdf.png"/></li>';
                $("#fileList ul").append(li);
            }
        };
    }

});

$(function () {
    var defaultColunms = EnterpriseCenter.initColumn();
    var table = new BSTable(EnterpriseCenter.id, "/enterprise/api/findEnterpriseChangeByPage", defaultColunms);
    table.setPaginationType("server");
    EnterpriseCenter.table = table.init();
    var type = $("#type").val();
    if (type == 1) {
        $("#talentType,#industryFieldNewName,#industryFieldOldName").parent().parent().attr("style", "display:block");
    }

});