<template>
  <div class="container">
    <div class="title c_title">添加用户</div>
    <div class="from">
      <div class="items">
        <div class="item">
          <div class="title">角色</div>
          <vxe-select disabledComponents v-model="userInfo.role_id" class="role_select">
            <vxe-option label="管理员" value="35efee88-f4bc-11ee-9341-8cc84ba8982e"></vxe-option>
            <vxe-option label="普通用户" value="34efee88-f4bc-11ee-9341-8cc84ba8982e"></vxe-option>
          </vxe-select>
        </div>
        <div class="item">
          <div class="title">姓名</div>
          <input
            class="ipt"
            type="text"
            v-model="userInfo.name"
            :readonly="false"
            @focus="stopKeyborad"
            @blur="stopKeyborad"
            @click="stopInput" />
        </div>
        <div class="item sex-item">
          <div class="title">性别</div>
          <input type="radio" value="1" name="sex" v-model="userInfo.sex" />
          <div class="ipt-t">男</div>
          <input type="radio" value="0" name="sex" v-model="userInfo.sex" />
          <div class="ipt-t">女</div>
        </div>
      </div>
      <div class="items">
        <div class="item">
          <div class="title">用户名</div>
          <input
            class="ipt"
            type="text"
            v-model="userInfo.username"
            :readonly="false"
            @focus="stopKeyborad"
            @blur="stopKeyborad"
            @click="stopInput" />
        </div>
        <div class="item">
          <div class="title">密码</div>
          <input
            type="password"
            class="ipt"
            v-model="userInfo.password"
            :readonly="false"
            @focus="stopKeyborad"
            @blur="stopKeyborad"
            @click="stopInput" />
        </div>
        <div class="item">
          <div class="title">确认密码</div>
          <input
            class="ipt"
            type="password"
            v-model="userInfo.proPassword"
            :readonly="false"
            @focus="stopKeyborad"
            @blur="stopKeyborad"
            @click="stopInput" />
        </div>
      </div>
    </div>
    <div class="footer">
      <div class="btn_box lf_btn">
        <div class="btn btn1" @click="goBack">返回</div>
      </div>
      <div class="btn_box rt_btn">
        <div class="btn btn4" @click="addNewUser()">保存</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import { notify } from '@/utils/message.js';
import { addUserApi } from '@/api/user.js';
const disabledComponents = computed(() => {
  return ['disableKeyboard'];
});
const router = useRouter();

const userInfo = reactive({
  role_id: '34efee88-f4bc-11ee-9341-8cc84ba8982e',
  username: '', // 用户名
  name: '', // 姓名
  sex: '1',
  password: '',
  proPassword: ''
});

const isFocused = ref(false);

const stopInput = (e) => {
  if (isFocused.value) {
    e.target.setAttribute('readonly', true);
    setTimeout(() => {
      e.target.removeAttribute('readonly');
    }, 100);
  }
};

const stopKeyborad = (e) => {
  isFocused.value = true;
  e.target.setAttribute('readonly', true);
  setTimeout(() => {
    e.target.removeAttribute('readonly');
  }, 100);
};

const goBack = () => {
  router.go(-1);
};

const addNewUser = async (type) => {
  if (userInfo.name == '') {
    notify('请输入姓名');
    return;
  }
  if (userInfo.username == '') {
    notify('请输入用户名');
    return;
  }
  if (userInfo.password == '') {
    notify('请输入密码');
    return;
  }
  if (userInfo.password != userInfo.proPassword) {
    notify('两次密码输入不一致');
    return;
  }
  let data = {
    ...userInfo
  };
  const res = await addUserApi(data);
  if (res.data.code != 200) {
    notify(res.data.desc);
    return;
  }
  notify(res.data.desc, 'success');
  router.go(-1);
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  overflow: auto;
  .title {
    font-family: Source Han Sans CN, Source Han Sans CN;
    font-weight: bold;
    font-size: 48px;
    color: #343434;
    text-align: right;
    font-style: normal;
    text-transform: none;
    margin-bottom: 50px;
  }
  .c_title {
    text-align: left;
  }

  .from {
    background: #f6f6f6;
    background: #f6f6f6;
    box-shadow: 0px 3px 9px 1px rgba(0, 0, 0, 0.16);
    border-radius: 12px 12px 12px 12px;
    border: 1px solid #cccccc;
    display: flex;
    justify-content: space-around;
    padding: 32px;
    margin-bottom: 60px;

    .items {
      margin-right: 10px;

      .item {
        display: flex;
        align-items: center;
        margin-bottom: 60px;

        .title {
          font-family: Source Han Sans CN, Source Han Sans CN;
          font-weight: 400;
          font-size: 40px;
          color: #666666;
          margin-right: 20px;
          margin-bottom: 0;
          min-width: 160px;
        }
        .role_select {
          height: 100px;
          :deep(.vxe-input) {
            height: 100%;
            font-size: 40px;
            width: 600px;
          }
          :deep(.vxe-select--panel) {
            width: 600px;
            font-size: 40px;
          }
          :deep(.vxe-select-option) {
            height: 80px;
            line-height: 80px;
          }
        }

        .ipt {
          position: relative;
          width: 600px;
          height: 100px;
          padding-left: 10px;
          font-size: 40px;
          background: #ffffff;
          box-shadow: inset 0px 3px 6px 1px rgba(0, 0, 0, 0.1);
          border-radius: 8px 8px 8px 8px;
          border: 1px solid #dcdcdc;
        }
      }
    }
  }

  .footer {
    display: flex;
    width: 100%;
    font-size: 40px;
    justify-content: space-between;
    position: fixed;
    bottom: 0;
    left: 0;
    padding: 19px 41px 40px 40px;
    background: #ffffff;
    .exit {
      width: 200px;
      height: 100px;
      background: linear-gradient(180deg, #ffffff 0%, #e5e5e5 100%);
      box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.26);
      border-radius: 2px 2px 2px 2px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .add-user {
      width: 292px;
      height: 100px;
      line-height: 80px;
      text-align: center;
      background: linear-gradient(180deg, #e24a4a 0%, #a21313 100%);
      box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.26);
      border-radius: 2px 2px 2px 2px;
      cursor: pointer;
      color: #fff;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .btn_box {
      display: flex;
      justify-content: space-between;
    }
    .btn {
      margin-right: 28px;
      background: linear-gradient(180deg, #ffffff 0%, #ececec 100%);
      box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.26);
      border-radius: 8px 8px 8px 8px;
      font-family: Source Han Sans CN, Source Han Sans CN;
      font-weight: 400;
      font-size: 40px;
      color: #666666;
      letter-spacing: 4px;
      text-align: center;
      font-style: normal;
      text-transform: none;
      padding: 30px 60px;
      &:last-child {
        margin-right: 0;
      }
    }
    .btn1 {
    }
    .btn2 {
      color: #cf4a4a;
    }
    .btn3 {
      color: #fff;
      background: linear-gradient(180deg, #ff7b83 0%, #850006 100%);
    }
    .btn4 {
      color: #fff;
      background: linear-gradient(180deg, #51a8ff 0%, #064d92 100%);
    }
  }
}
.sex-item {
  display: flex;
  height: 100px;
  .ipt-t {
    padding: 0 20px;
    font-size: 40px;
  }
}
input[type='checkbox'],
input[type='radio'] {
  width: 40px !important;
  height: 40px !important;
  border: 1px solid #333;
}
</style>
