<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>license授权</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-item index="4">
            <span>登录注册配置</span>
          </el-menu-item>
        </el-menu>
      </div>
      <div class="right_container bgc_white">
        <!-- license授权表单 -->
        <authorityForm v-if="activeIndex == 1" :authorizeFormData="authorizeFormData"></authorityForm>

        <loginPageForm v-if="activeIndex == 2" :loginPageFormData="loginPageFormData"></loginPageForm>
        <!-- 安全相关表单 -->
        <secureForm v-if="activeIndex == 3" :secureFormData="secureFormData"></secureForm>
        <!-- 登录注册配置表单 -->
        <registerForm v-if="activeIndex == 4" :registerFormData="registerFormData"></registerForm>
        <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 authorityForm from "./authorityForm.vue";
import loginPageForm from "./loginPageForm.vue";
import secureForm from "./secureForm.vue";
import registerForm from "./registerForm.vue";
import axios from "@/request/http.js";

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: "",
  },
  //登录页表单
  loginPageFormData: {
    logoUrl: [],
    backgroundUrl: [],
    patent: "",
    icpFiling: "",
    icpUrl: "",
    publicSecurityFiling: "",
    publicSecurityUrl: "",
    legalStatementUrl: "",
    privacyPolicyUrl: "",
  },
  //安全相关信息表单
  secureFormData: {
    id: "",
    min_pwd_level: "",
    force_update_state: 0,
    pwd_validity: "",
    session_validity: "",
    access_rule_state: "",
  },

  //登录注册配置表单
  registerFormData: {
    id: "",
    login_config_state: 0,
    login_limit_time: "",
    login_pwd_error: "",
    login_lock_time: "",
    register_config_state: 0,
  },
  //选中项索引
  activeIndex: 1,
  restoreDialog: false,
});
const { authorizeFormData, loginPageFormData, secureFormData, registerFormData, 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);
    }
  });
};

//保存表单项
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;
        }
      }
    }
    :deep() .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;
        }
      }
    }

    :deep() .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>
