<template>
  <div class="container">
    <div class="title">用户信息</div>
    <div class="from">
      <div class="items">
        <div class="item">
          <div class="title">角色</div>
          <select v-model="userInfo.role" class="ipt">
            <option value="35efee88-f4bc-11ee-9341-8cc84ba8982e" class="ipt">管理员</option>
            <option value="34efee88-f4bc-11ee-9341-8cc84ba8982e" class="ipt">普通用户</option>
          </select>
        </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="2" name="sex" v-model="userInfo.sex" />
          <div class="ipt-t">女</div>
        </div>
        <div class="item">
          <div class="title">密码</div>
          <input
            class="ipt"
            type="password"
            v-model="userInfo.password"
            :readonly="false"
            @focus="stopKeyborad"
            @blur="stopKeyborad"
            @click="stopInput" />
        </div>
      </div>
      <div class="items">
        <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">
          <div class="title">账号</div>
          <input
            class="ipt"
            v-model="userInfo.username"
            :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="exit" @click="goBack">返回</div>
      <div class="add-user" @click="addNewUser()">保存</div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { notify } from '@/utils/message.js';
import { addUserApi, getRoseList } from '@/api/user.js';

const router = useRouter();
const route = useRoute();

const userInfo = reactive({
  role: '35efee88-f4bc-11ee-9341-8cc84ba8982e',
  role_id: '35efee88-f4bc-11ee-9341-8cc84ba8982e',
  name: '',
  username: '',
  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) => {
  console.log(userInfo);
  if (userInfo.password != userInfo.proPassword) {
    notify('两次密码输入不一致');
    return;
  }
  if (userInfo.name == '') {
    notify('请输入姓名');
    return;
  }
  if (userInfo.username == '') {
    notify('请输入用户名');
    return;
  }
  if (userInfo.password == '') {
    notify('请输入密码');
    return;
  }

  let data = {
    username: userInfo.username,
    name: userInfo.name,
    role_id: userInfo.role,
    password: userInfo.password,
    sex: userInfo.sex
  };
  const res = await addUserApi(data);
  if (res.data.code != 200) {
    notify(res.data.desc);
    return;
  }
  notify(res.data.desc, 'success');
  router.go(-1);
};

const showDialog = ref(false);
// const selectValue = ref('管理员')
const roleList = reactive([]);
const selectRole = async () => {
  const res = await getRoseList();
  if (res.status != 200) {
    notify(res.data.desc);
    return;
  }

  Object.assign(roleList, res.data.data);

  showDialog.value = !showDialog.value;
};
const selVal = (item) => {
  userInfo.role = item.role_name;
  userInfo.role_id = item.role_id;
};
</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: 16px;
    color: #343434;
    text-align: left;
    font-style: normal;
    text-transform: none;
    margin-bottom: 16px;
  }

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

    .items {
      margin-right: 10px;
      .item {
        display: flex;
        align-items: center;
        margin-bottom: 16px;

        .title {
          font-family: Source Han Sans CN, Source Han Sans CN;
          font-weight: 400;
          font-size: 16px;
          color: #666666;
          text-align: left;
          font-style: normal;
          text-transform: none;
          margin-right: 8px;
          min-width: 64px;
        }

        .ipt {
          position: relative;
          width: 220px;
          height: 44px;
          padding-left: 10px;
          font-size: 16px;
          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;

          .sel-val {
            height: 70px;
            line-height: 70px;
            display: flex;
            font-size: 16px;
            color: #666;
            padding: 0 20px 0 0;
            justify-content: space-between;
          }

          .sel-list {
            position: absolute;
            width: 100%;
            top: 80px;
            left: 0;
            z-index: 999;

            color: #666;
            background-color: #fff;
            box-shadow: inset 0px 3px 6px 1px rgba(0, 0, 0, 0.1);
            border-radius: 8px 8px 8px 8px;

            .sel-item {
              width: 100%;
              height: 70px;
              font-size: 16px;
              line-height: 70px;
              padding-left: 24px;
              border-bottom: 1px solid #ccc;
            }
          }
        }
      }
    }
  }

  .footer {
    display: flex;
    width: 100%;
    font-size: 16px;
    justify-content: space-between;
    position: fixed;
    padding: 12px 10px;
    bottom: 0;
    left: 0;
    background: #ffffff;

    .exit {
      width: 89px;
      height: 44px;
      text-align: center;
      background: linear-gradient(180deg, #ffffff 0%, #e5e5e5 100%);
      box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.2);
      border-radius: 12px 12px 12px 12px;
      border: 1px solid #e5e5e5;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .add-user {
      width: 89px;
      height: 44px;
      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.2);
      border-radius: 8px 8px 8px 8px;
      cursor: pointer;
      color: #fff;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}
.sex-item {
  display: flex;
  .ipt-t {
    padding: 0 20px;
    font-size: 16px;
  }
}
input[type='checkbox'],
input[type='radio'] {
  width: 17px !important;
  height: 17px !important;
  border: 1px solid #333;
}
</style>
