<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>
          <div class="pwd_input">
            <input
              class="ipt"
              :type="inptType"
              v-model="userInfo.password"
              @focus="stopKeyborad"
              @blur="stopKeyborad"
              @click="stopInput" />
            <el-icon v-if="inptType == 'password'" class="see_icon" @click="hanlePwd(true)">
              <View />
            </el-icon>
            <el-icon v-else class="see_icon" @click="hanlePwd(false)"><Hide /></el-icon>
          </div>
        </div>
        <!-- <div class="item">
          <div class="title">备注信息</div>
          <input
            class="ipt"
            type="text"
            v-model="userInfo.remark"
            :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"
            v-model="userInfo.userPhone"
            :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="editUserBtn()">保存</div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { notify } from '@/utils/message.js';
import { editUserApi, getRoseList } from '@/api/user.js';
import { Hide,View } from '@element-plus/icons-vue'
// const [messageApi, contextHolder] = message.useMessage();

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

const isFocused = ref(false);
let inptType = ref("password");
const hanlePwd = (bol) => {
  if (bol) {
    inptType.value = "text";
  } else {
    inptType.value = "password";
  }
};
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);
};

console.log(route.query);
const userInfo = reactive({
  role: route.query.role_id,
  name: route.query.name,
  user_id: route.query.user_id,
  userName: route.query.userName,
  sex: route.query.sex,
  password: '',
  role_id: route.query.role_id
});

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

const editUserBtn = async () => {
  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 editUserApi(userInfo.user_id, data);

  if (res.status != 200) {
    notify(res.data.desc);
    return;
  }
  notify(res.data.desc, 'success');
  router.go(-1);
};

const showDialog = ref(false);

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: 12px;
  }

  .from {
    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;
    padding: 16px;
    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: 60px;
        }

        .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-right: 20px;
            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.2);
            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;
      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;
      border: 1px solid #e5e5e5;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .right_btn {
      display: flex;
    }

    .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.26);
      border-radius: 2px 2px 2px 2px;
      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;
}
.pwd_input{
  width: 100%;
  position: relative;
  .see_icon{
    font-size: 20px;
    position: absolute;
    top: 50%;
    right: 10px;
    transform: translateY(-50%);
  }
}
input::-ms-reveal{
  display: none;
}
input::-ms-clear{
  display: none;
}
input::-o-clear{
  display: none;
}
</style>
