<template>
  <div>
    <el-button
      v-preReClick
      class="drawer-container"
      icon="setting"
      type="primary"
      @click="showSettingDrawer"
    />
    <el-drawer
      v-model="drawer"
      :before-close="handleClose"
      :direction="direction"
      title="系统配置"
    >
      <div class="setting_body">
        <div class="setting_card">
          <h4>主题</h4>
          <div class="setting_content">
            <div class="theme-box">
              <div class="item" @click="changeMode('light')">
                <div class="item-top">
                  <el-icon v-if="userStore.mode === 'light'" class="check">
                    <check />
                  </el-icon>
                  <img
                    src="https://gw.alipayobjects.com/zos/antfincdn/NQ%24zoisaD2/jpRkZQMyYRryryPNtyIC.svg"
                  />
                </div>
                <p>简约白</p>
              </div>
              <div class="item" @click="changeMode('dark')">
                <div class="item-top">
                  <el-icon v-if="userStore.mode === 'dark'" class="check">
                    <check />
                  </el-icon>
                  <img
                    src="https://gw.alipayobjects.com/zos/antfincdn/XwFOFbLkSM/LCkqqYNmvBEbokSDscrm.svg"
                  />
                </div>
                <p>商务黑</p>
              </div>
            </div>
          </div>
        </div>
        <div class="setting_card">
          <h4>登录方式</h4>
          <el-alert
            title="切换之前请确保相关的登录方式已经配置完成"
            type="info"
          />
          <div class="setting_content">
            <el-checkbox-group v-model="loginTypes" @change="checkLogin">
              <el-checkbox :value="1" border label="账号密码登录" />
              <el-checkbox :value="2" border label="ukey登录" />
              <el-checkbox :value="3" border label="动态令牌登录" />
            </el-checkbox-group>
          </div>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useUserStore } from '@/pinia/modules/user'
import { getLoginType, checkLoginType } from '@/api/config'
import { ElMessage } from 'element-plus'

defineOptions({
  name: 'Setting'
})

onMounted(async () => {
  const res = await getLoginType()
  if (res.code === 100000) {
    loginTypes.value = JSON.parse(res.data)
  }
})

const checkLogin = async v => {
  const param = JSON.stringify(v)
  const res = await checkLoginType({ loginType: param })
  if (res.code === 100000) {
    ElMessage({ type: 'success', message: res.message })
  }
}

const drawer = ref(false)
const direction = ref('rtl')

const userStore = useUserStore()
const loginTypes = ref([1])
const handleClose = () => {
  drawer.value = false
}
const showSettingDrawer = () => {
  drawer.value = true
}
const changeMode = e => {
  if (e === null) {
    userStore.changeSideMode('dark')
    return
  }
  userStore.changeSideMode(e)
}
</script>

<style lang="scss" scoped>
.drawer-container {
  transition: all 0.2s;

  &:hover {
    right: 0;
  }

  position: fixed;
  right: -20px;
  bottom: 15%;
  height: 40px;
  width: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  color: #fff;
  border-radius: 4px 0 0 4px;
  cursor: pointer;
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 10%);
}

.setting_body {
  padding: 20px;

  .setting_card {
    margin-bottom: 50px;
  }

  .setting_content {
    margin-top: 20px;
    display: flex;
    flex-direction: column;

    > .theme-box {
      display: flex;
    }

    > .color-box {
      div {
        display: flex;
        flex-direction: column;
      }
    }

    .item {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      margin-right: 20px;

      .item-top {
        position: relative;
      }

      .check {
        position: absolute;
        font-size: 20px;
        color: #00afff;
        right: 10px;
        bottom: 10px;
      }

      p {
        text-align: center;
        font-size: 12px;
      }
    }
  }
}
</style>
