<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 ref="authorityFormRef" v-if="activeIndex == 1"></authorityForm>

        <loginPageForm ref="loginPageFormRef" v-if="activeIndex == 2"></loginPageForm>
        <!-- 安全相关表单 -->
        <secureForm ref="secureFormRef" v-if="activeIndex == 3"></secureForm>
        <!-- 登录注册配置表单 -->
        <registerForm ref="registerFormRef" v-if="activeIndex == 4"></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, nextTick } 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 authorityFormRef = ref(null);
const loginPageFormRef = ref(null);
const secureFormRef = ref(null);
const registerFormRef = ref(null);
const state = reactive({
  //选中项索引
  activeIndex: 1,
  restoreDialog: false,
  //首选项配置数据
  sysOptions: {},
});
const { activeIndex, restoreDialog } = toRefs(state);
onBeforeMount(() => {
  getPreferenceConfig();
});
//切换菜单选项
const switchMenu = (index) => {
  state.activeIndex = index;
  nextTick(() => {
    switch (index) {
      case 1:
        authorityFormRef.value.setFormData(state.sysOptions);
        break;
      case 2:
        loginPageFormRef.value.setFormData(state.sysOptions);
        break;
      case 3:
        secureFormRef.value.setFormData(state.sysOptions);
        break;
      case 4:
        registerFormRef.value.setFormData(state.sysOptions);
        break;
      default:
        break;
    }
  });
};
//获取首选项配置
const getPreferenceConfig = () => {
  axios.get("/v1/api/sysOptions").then((res) => {
    if (res.data.code == 200) {
      let data = res.data.data;
      state.sysOptions = data;
      authorityFormRef.value.setFormData(data);
    } else {
      ElMessage.error(res.data.data);
    }
  });
};

//保存表单项
const save = () => {
  switch (state.activeIndex) {
    case 1:
      authorityFormRef.value.save();
      break;
    case 2:
      loginPageFormRef.value.save();
      break;
    case 3:
      secureFormRef.value.save();
      break;
    case 4:
      registerFormRef.value.save();
      break;
    default:
      break;
  }
};
// 请求接口发送消息
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>
