<template>
  <div class="page_container">
    <bg-breadcrumb></bg-breadcrumb>
    <div class="parent_container">
      <div class="left_container bgc_white">
        <el-menu default-active="1" @select="switchMenu">
          <el-menu-item index="1">
            <span>授权信息</span>
          </el-menu-item>
          <el-menu-item index="2">
            <span>安全相关</span>
          </el-menu-item>
          <el-menu-item index="3">
            <span>登录注册配置</span>
          </el-menu-item>
        </el-menu>
      </div>
      <div class="right_container bgc_white">
        <!-- 授权信息表单 -->
        <el-form
          v-if="activeIndex == 1"
          ref="authorizeFormRef"
          :model="authorizeFormData"
          :rules="authorizeRules"
          label-width="110px"
          class="authorizeForm">
          <el-form-item label="当前版本号" prop="system_version">
            <el-input disabled v-model="authorizeFormData.system_version" />
          </el-form-item>
          <el-form-item label="User" prop="system_user">
            <el-input
              placeholder="统一管理用户认证平台"
              v-model="authorizeFormData.system_user"
              maxlength="30" />
          </el-form-item>
          <el-form-item label="Key" prop="license">
            <el-input
              placeholder="请输入"
              rows="5"
              v-model="authorizeFormData.license"
              type="textarea"
              @blur="getLicenseInfo" />
          </el-form-item>
          <el-form-item label="有效期至" prop="license_dead_date">
            <el-input disabled v-model="authorizeFormData.license_dead_date" />
          </el-form-item>
          <el-form-item label="剩余有效期提醒" prop="license_inform_day">
            <el-input
              placeholder="请输入"
              v-model.trim.number="authorizeFormData.license_inform_day">
              <template #append>天</template>
            </el-input>
            <el-tooltip placement="top-start" effect="light">
              <template #content>
                范围（0~90），单位：天，
                当剩余有效期在配置区间内时，系统会在用户登录系统时发出提醒，0表示不提醒
              </template>
              <div class="tip-image"></div>
            </el-tooltip>
          </el-form-item>
        </el-form>
        <!-- 安全相关表单 -->
        <el-form
          v-if="activeIndex == 2"
          ref="secureFormRef"
          :model="secureFormData"
          :rules="secureRules"
          label-width="110px"
          class="secureForm">
          <div class="passwordItem">
            <el-form-item label="最低密码强度" prop="min_pwd_level">
              <el-select v-model="secureFormData.min_pwd_level" placeholder="请选择">
                <el-option label="弱" :value="1" />
                <el-option label="中" :value="2" />
                <el-option label="强" :value="3" />
              </el-select>
              <el-tooltip placement="top-start" effect="light">
                <template #content>
                  <span>支持大写字母/小写字符/数字/特殊符号</span>
                  <br />
                  <span>密码长度应至少8位字符</span>
                  <br />
                  <span>弱：至少包含一种组合。</span>
                  <br />
                  <span>中：至少包含两种组合。</span>
                  <br />
                  <span>强：至少包含三种及以上组合。</span>
                </template>
                <div class="tip-image"></div>
              </el-tooltip>
            </el-form-item>
            <el-form-item label="是否强制修改" prop="force_update_state">
              <bg-switch
                :labels="['否', '是']"
                :values="[0, 1]"
                v-model="secureFormData.force_update_state"></bg-switch>
            </el-form-item>
          </div>
          <el-form-item label="密码有效期" prop="pwd_validity">
            <el-input
              placeholder="请输入"
              v-model.trim.number="secureFormData.pwd_validity"
              maxlength="3">
              <template #append>天</template>
            </el-input>
            <el-tooltip placement="top-start" effect="light">
              <template #content>
                <span>单位，天</span>
                <br />
                <span
                  >密码有效期是指强制修改密码的周期，例如60天表示用户需要每60天修改一次密码。</span
                >
                <br />
                <span>若配置为0，表示密码有效期为永久</span>
              </template>
              <div class="tip-image"></div>
            </el-tooltip>
          </el-form-item>
          <el-form-item label="会话有效期" prop="session_validity">
            <el-input placeholder="请输入" v-model.trim.number="secureFormData.session_validity">
              <template #append>分钟</template>
            </el-input>
            <el-tooltip placement="top-start" effect="light">
              <template #content>
                <span>{{`范围（10~${session_validity_max}）`}}</span>
                <br />
                <span>单位，分钟</span>
                <br />
                <span>会话有效期是指您登录后，一定时间内无操作，系统将自动登出</span>
              </template>
              <div class="tip-image"></div>
            </el-tooltip>
          </el-form-item>
          <el-form-item label="访问规则模式" prop="access_rule_state">
            <el-select placeholder="请选择" v-model="secureFormData.access_rule_state">
              <el-option label="关闭" :value="0"> </el-option>
              <el-option label="打开" :value="1"> </el-option>
            </el-select>
            <el-tooltip placement="top-start" effect="light">
              <template #content>
                <span>访问规则模式开启后，按照【访问规则管理】配置执行。</span>
                <br />
                <span>该模式针对超管无效</span>
              </template>
              <div class="tip-image"></div>
            </el-tooltip>
          </el-form-item>
        </el-form>
        <!-- 登录注册配置表单 -->
        <el-form
          v-if="activeIndex == 3"
          ref="registerFormRef"
          :model="registerFormData"
          :rules="registerRules"
          label-width="125px"
          class="registerForm">
          <bg-form-gap title="登录受限配置"></bg-form-gap>
          <el-form-item label="登录受限是否启用" prop="login_config_state">
            <bg-switch
              :labels="['否', '是']"
              :values="[0, 1]"
              v-model="registerFormData.login_config_state"></bg-switch>
          </el-form-item>
          <div class="input_password_config" v-if="registerFormData.login_config_state == 1">
            <span>在</span>
            <el-form-item prop="login_limit_time" label-width="0">
              <el-input
                placeholder="请输入小时数"
                v-model.trim.number="registerFormData.login_limit_time"
                maxlength="3"></el-input>
            </el-form-item>
            <span>小时内</span>
            <el-form-item prop="login_pwd_error" label-width="0">
              <el-input
                placeholder="请输入密码输错次数"
                v-model.trim.number="registerFormData.login_pwd_error"
                maxlength="3"></el-input>
            </el-form-item>
            <span>次输错密码，自动锁定</span>
            <el-form-item prop="login_lock_time" label-width="0">
              <el-input
                placeholder="请输入分钟数"
                v-model.trim.number="registerFormData.login_lock_time"
                maxlength="3"></el-input>
            </el-form-item>
            <span>分钟</span>
            <el-button type="primary" @click="restoreDialog = true">一键还原受限</el-button>
            <el-tooltip placement="top-start" effect="light">
              <template #content>
                <span>必须为大于0的正整数且不能为空</span>
                <br />
                <span>一键还原按钮将所有锁定的账号解锁并将输错次数还原</span>
              </template>
              <div class="tip-image"></div>
            </el-tooltip>
          </div>
          <bg-form-gap title="注册配置"></bg-form-gap>
          <el-form-item label="允许注册" prop="register_config_state">
            <el-radio-group v-model="registerFormData.register_config_state">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div class="operate_btns">
          <el-button type="primary" @click="save"> 保存 </el-button>
        </div>
      </div>
    </div>
    <el-dialog class="dialog_box" title="提示" v-model="restoreDialog" width="420px">
      <div>该操作将还原所有锁定账号和输错次数，是否继续？</div>
      <template v-slot:footer>
        <div class="apaas_button">
          <el-button type="default" @click="restoreDialog = false">取 消</el-button>
          <el-button type="primary" @click="confirm">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, toRefs, onBeforeMount, ref } from "vue";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
import { ElMessage } from "element-plus";
import axios from "@/request/http.js";

const session_validity_max = window.defaultConfig.session_validity_max;

const authorizeFormRef = ref(null);
const secureFormRef = ref(null);
const registerFormRef = ref(null);
const state = reactive({
  //授权信息表单
  authorizeFormData: {
    id: "",
    system_version: "",
    system_user: "",
    license: "",
    license_dead_date: "",
    license_inform_day: "",
  },
  //授权表单校验规则
  authorizeRules: {
    license_inform_day: [
      { type: "number", min: 0, max: 90, message: "请输入0~90的整数", trigger: "blur" },
    ],
  },

  //安全相关信息表单
  secureFormData: {
    id: "",
    min_pwd_level: "",
    force_update_state: 0,
    pwd_validity: "",
    session_validity: "",
    access_rule_state: "",
  },

  //安全相关表单校验规则
  secureRules: {
    pwd_validity: [
      {
        type: "number",
        message: "请输入大于或等于0的整数",
        trigger: "blur",
      },
    ],
    session_validity: [
      {
        type: "number",
        min: 10,
        max: session_validity_max,
        message: `请输入10~${session_validity_max}的整数`,
        trigger: "blur",
      },
    ],
  },
  //登录注册配置表单
  registerFormData: {
    id: "",
    login_config_state: 0,
    login_limit_time: "",
    login_pwd_error: "",
    login_lock_time: "",
    register_config_state: 0,
  },
  //登录注册配置表单校验规则
  registerRules: {
    login_limit_time: [
      {
        type: "number",
        min: 1,
        message: "请输入正整数",
        trigger: "blur",
      },
    ],
    login_pwd_error: [
      {
        type: "number",
        min: 1,
        message: "请输入正整数",
        trigger: "blur",
      },
    ],
    login_lock_time: [
      {
        type: "number",
        min: 1,
        message: "请输入正整数",
        trigger: "blur",
      },
    ],
  },
  //选中项索引
  activeIndex: 1,
  restoreDialog: false,
});
const {
  authorizeFormData,
  authorizeRules,
  secureFormData,
  secureRules,
  registerFormData,
  registerRules,
  activeIndex,
  restoreDialog,
} = toRefs(state);
onBeforeMount(() => {
  getPreferenceConfig();
});
//切换菜单选项
const switchMenu = (index) => {
  state.activeIndex = index;
};
//获取首选项配置
const getPreferenceConfig = () => {
  axios.get("/apaas/system/v5/sysOptions").then((res) => {
    if (res.data.code == 200) {
      let data = res.data.data;
      Object.keys(state.authorizeFormData).forEach((item) => {
        state.authorizeFormData[item] = data[item];
      });
      Object.keys(state.secureFormData).forEach((item) => {
        state.secureFormData[item] = data[item];
      });
      Object.keys(state.registerFormData).forEach((item) => {
        state.registerFormData[item] = data[item];
      });
    } else {
      ElMessage.error(res.data.data);
    }
  });
};
//根据license获取授权信息
const getLicenseInfo = () => {
  if (!state.authorizeFormData.license) return;
  axios
    .post("/apaas/system/v5/sysOptions/getLicenseInfo", {
      license: state.authorizeFormData.license,
    })
    .then((res) => {
      if (res.data.code == 200) {
        const data = res.data.data || {};
        state.authorizeFormData.system_version = data.version;
        state.authorizeFormData.license_dead_date = data.deadDate;
      } else {
        ElMessage.error(res.data.data);
      }
    });
};
//保存表单项
const save = () => {
  let params = {};
  if (state.activeIndex == 1) {
    authorizeFormRef.value.validate((valid) => {
      if (valid) {
        params = {
          ...state.authorizeFormData,
        };
        postData("/apaas/system/v5/sysOptions/licenseOpts", params);
      }
    });
  } else if (state.activeIndex == 2) {
    secureFormRef.value.validate((valid) => {
      if (valid) {
        params = {
          ...state.secureFormData,
        };
        postData("/apaas/system/v5/sysOptions/safeOpts", params);
      }
    });
  } else {
    registerFormRef.value.validate((valid) => {
      if (valid) {
        params = {
          ...state.registerFormData,
        };
        if (params.login_config_state == 0) {
          params.login_limit_time = 0;
          params.login_pwd_error = 0;
          params.login_lock_time = 0;
        }
        postData("/apaas/system/v5/sysOptions/loginOpts", params);
      }
    });
  }
};
// 请求接口发送消息
const postData = (url, params) => {
  axios.post(url, params).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success(res.data.msg);
    } else {
      ElMessage.error(res.data.data);
    }
  });
};

const confirm = () => {
  axios.post(`/apaas/system/v5/sysOptions/reStartConfig`).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success(res.data.msg);
      state.restoreDialog = false;
    } else {
      ElMessage.error(res.data.data);
    }
  });
};
</script>

<style lang="scss" scoped>
.parent_container {
  display: flex;
  justify-content: space-between;
  height: calc(100% - 46px - 20px);
  .left_container {
    width: 240px;
    height: 100%;
    padding: 24px 16px 0;
    .el-menu {
      border: none;
      .el-menu-item {
        justify-content: center;
        height: 36px;
        color: #404a62 !important;
        &.is-active,
        &:hover {
          background-color: #f2f3f7;
          border-radius: 4px;
        }
      }
    }
  }
  .right_container {
    position: relative;
    width: calc(100% - 250px);
    min-width: 852px;
    height: 100%;
    padding: 32px 0 68px 24px;
    margin-left: 16px;

    .authorizeForm,
    .secureForm,
    .registerForm {
      :deep() .el-form-item {
        margin-bottom: 24px;
        width: 54%;
        min-width: 580px;
        font-size: 14px;
        .el-form-item__label,
        .el-radio__label {
          color: var(--el-text-color-primary);
        }
        .el-select {
          width: 100%;
        }
        .el-input-group__append {
          width: 60px;
          color: #404a62;
          border-radius: 0 6px 6px 0;
          background: #f7f7f9;
        }
      }
    }
    .secureForm {
      .passwordItem {
        display: flex;
        align-items: center;
        .el-form-item:nth-of-type(1) {
          flex-shrink: 0;
          margin-right: 56px;
        }
        .el-form-item:nth-of-type(2) {
          min-width: auto;
        }
      }
    }

    .registerForm {
      .input_password_config {
        position: relative;
        display: flex;
        align-items: center;
        width: 798px;
        margin-bottom: 24px;
        font-size: 14px;
        color: var(--el-text-color-primary);
        .el-form-item {
          margin: 0px 8px;
          min-width: auto;
          &:nth-of-type(1),
          &:nth-of-type(3) {
            flex: 0;
            min-width: 110px;
            width: 120px;
          }
          &:nth-of-type(2) {
            flex: 0;
            min-width: 140px;
            width: 150px;
          }
        }
        .el-button {
          margin-left: 18px;
        }
      }
      .el-radio {
        margin-right: 54px;
      }
    }

    .operate_btns {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      height: 68px;
      padding-right: 16px;
      line-height: 68px;
      text-align: right;
      border-top: solid 1px #e6e9ef;
      .el-button {
        width: 92px;
      }
    }
  }
}
</style>
