<template>
  <a-collapse
    :bordered="false"
    default-active-key="a"
    expand-icon-position="right"
    class="header"
  >
    <a-collapse-panel key="a" :header="$t('PASSWORD_SETTING')">
      <a-form-model ref="addForm" :model="form">
        <a-form-model-item label="">
          <a-radio-group v-model="radioValue">
            <a-radio value="1" class="radioStyle">
              {{ $t("LOW_LENGTH")
              }}<a-input-number
                v-model="lObj.passwordLength"
                class="numberInt"
              ></a-input-number
              >{{ $t("NO_REQUIREMENTS") }}
              <span class="password"
                >初始密码：<a-input v-model="lObj.initPassword"></a-input
              ></span>
            </a-radio>
            <a-radio value="2" class="radioStyle"
              >中，密码长度<a-input-number
                v-model="mObj.passwordLength"
                class="numberInt"
              ></a-input-number
              >个字符及以上，必须包含数字，字母。
              <span class="password"
                >初始密码：<a-input v-model="mObj.initPassword"></a-input></span
            ></a-radio>
            <a-radio value="3" class="radioStyle"
              >高，密码长度<a-input-number
                v-model="hObj.passwordLength"
                class="numberInt"
              ></a-input-number
              >个字符及以上，必须包含数字，大小写字母，及特殊字符。
              <span class="password"
                >初始密码：<a-input v-model="hObj.initPassword"></a-input></span
            ></a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="" prop="creditCode">
          <a-checkbox
            v-model="form.configBPO.enableFlag"
            :value="form.configBPO.enableFlag"
            >启用密码更换设定</a-checkbox
          >
        </a-form-model-item>
        <a-form-model-item label="" prop="creditCode">
          用户<a-input-number
            v-model="form.configBPO.configValue"
            class="numberInt"
          ></a-input-number
          >月，定期更换密码，切不可以与前次使用的密码相同。
        </a-form-model-item>
        <a-form-model-item label="" prop="creditCode">
          <a-checkbox v-model="form.isForceModifyPwd"
            >首次登录强制修改密码</a-checkbox
          >
        </a-form-model-item>
      </a-form-model>
      <a-button type="primary" @click="confirm()">
        <Icon type="icon-cur-save_1" :size="18" />{{ $t("SAVE") }}
      </a-button>
    </a-collapse-panel>
  </a-collapse>
</template>

<script>
import { Icon } from "@mega-design/ui-vue";
import ApiConstant from "@/api/ApiConstant";

export default {
  components: {
    Icon,
  },
  data() {
    return {
      radioValue: "1",
      details: "",
      form: {
        isForceModifyPwd: false,
        pwdConfigBPOList: [],
        configBPO: {
          enableFlag: false,
          configValue: 1,
        },
      },
      lObj: {
        enableFlag: false,
        id: "",
        initPassword: "",
        passwordLength: 6,
        passwordLevel: "L",
      },
      mObj: {
        enableFlag: false,
        id: "",
        initPassword: "",
        passwordLength: 6,
        passwordLevel: "M",
      },
      hObj: {
        enableFlag: false,
        id: "",
        initPassword: "",
        passwordLength: 6,
        passwordLevel: "H",
      },
      isSave: true,
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      this.$axios
        .get(`${ApiConstant.PREFIX_CORE}/api/sys/pwd-config/get-config`)
        .then((res) => {
          const arr = res.data.data.pwdConfigBPOList;
          arr.forEach((item) => {
            if (item.passwordLevel === "L") {
              this.lObj = { ...item };
            } else if (item.passwordLevel === "M") {
              this.mObj = { ...item };
            } else if (item.passwordLevel === "H") {
              this.hObj = { ...item };
            }
          });
          this.radioValue =
            // eslint-disable-next-line no-nested-ternary
            arr[0]?.enableFlag === "Y"
              ? "1"
              : // eslint-disable-next-line no-nested-ternary
              arr[1]?.enableFlag === "Y"
              ? "2"
              : arr[2]?.enableFlag === "Y"
              ? "3"
              : "";
          this.form.configBPO = { ...res.data.data.configBPO };
          this.form.configBPO.enableFlag =
            this.form.configBPO.enableFlag === "Y";
          this.form.isForceModifyPwd = this.form.isForceModifyPwd === "Y";
        });
    },
    confirm() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          if (this.radioValue === "1") {
            this.verification(
              this.lObj.initPassword,
              this.lObj.passwordLength,
              "L"
            );
            this.lObj.enableFlag = "Y";
            this.mObj.enableFlag = "N";
            this.hObj.enableFlag = "N";
          } else if (this.radioValue === "2") {
            this.verification(
              this.mObj.initPassword,
              this.mObj.passwordLength,
              "M"
            );
            this.lObj.enableFlag = "N";
            this.mObj.enableFlag = "Y";
            this.hObj.enableFlag = "N";
          } else if (this.radioValue === "3") {
            this.verification(
              this.hObj.initPassword,
              this.hObj.passwordLength,
              "H"
            );
            this.lObj.enableFlag = "N";
            this.mObj.enableFlag = "N";
            this.hObj.enableFlag = "Y";
          }
          if (this.form.configBPO.enableFlag === true) {
            if (
              !this.form.configBPO.configValue &&
              this.form.configBPO.configValue !== 0
            ) {
              this.$message.error("启用密码设定，月份必填");
            } else if (this.isSave) {
              this.form.configBPO.enableFlag =
                this.form.configBPO.enableFlag === true ? "Y" : "N";
              this.form.isForceModifyPwd =
                this.form.isForceModifyPwd === true ? "Y" : "N";
              this.form.pwdConfigBPOList = [this.lObj, this.mObj, this.hObj];
              const params = {
                ...this.form,
              };
              this.$axios
                .post(`${ApiConstant.PREFIX_CORE}/api/sys/pwd-config`, params)
                .then((res) => {
                  this.$message.success(this.$t("SAVE_SUCCESS"));
                  this.getData();
                });
            }
          } else if (this.isSave) {
            this.form.configBPO.enableFlag =
              this.form.configBPO.enableFlag === true ? "Y" : "N";
            this.form.isForceModifyPwd =
              this.form.isForceModifyPwd === true ? "Y" : "N";
            this.form.pwdConfigBPOList = [this.lObj, this.mObj, this.hObj];
            const params = {
              ...this.form,
            };
            this.$axios
              .post(`${ApiConstant.PREFIX_CORE}/api/sys/pwd-config`, params)
              .then((res) => {
                this.$message.success(this.$t("SAVE_SUCCESS"));
                this.getData();
              });
          }
        }
      });
    },
    cancel() {
      this.$store.dispatch("tagsView/jumpBeforeView", {
        to: { path: "/upms/modeling/org/customer" },
        vm: this,
      });
    },
    verification(val, length, level) {
      if (length !== 0) {
        if (val.length !== length) {
          this.isSave = false;
          if (val.length === 0) {
            this.$message.error("请输入初始密码");
          } else {
            this.$message.error("初始密码长度不符合");
          }
        } else if (level === "M") {
          const reg = /^(?![^a-zA-Z]+$)(?!\D+$)/;
          if (!reg.test(val)) {
            this.isSave = false;
            this.$message.error("密码必须包含数字和字母");
          } else {
            this.isSave = true;
          }
        } else if (level === "H") {
          const res =
            /([0-9].*([a-zA-Z].*[!$#%?.&@]|[!$#%?.&].*[a-zA-Z])|[a-zA-Z].*([0-9].*[!$#%?.&]|[!$#%?.&].*[0-9])|[!$#%?.&].*([0-9].*[a-zA-Z]|[a-zA-Z].*[0-9]))/;
          if (!res.test(val)) {
            this.isSave = false;
            this.$message.error("密码必须包含数字和字母和特殊字符");
          } else {
            this.isSave = true;
          }
        } else {
          this.isSave = true;
        }
      } else {
        this.isSave = false;
        this.$message.error("密码长度不能为0");
      }
    },
  },
};
</script>
<style lang="less" scoped>
.radioStyle {
  display: flex;
  width: 100%;
  align-items: center;
  padding: 20px 10px;
  border: 1px dashed #ccc;
  margin-bottom: 20px;
}
.password {
  margin-left: 100px;
  input {
    width: 300px;
  }
}
.numberInt {
  margin: 0 10px;
}
</style>
