<template>
  <div class="page-box">
    <el-card class="title">
      用户详情-<span v-if="detail && detail.id">{{ detail.id }}</span>
    </el-card>
    <br />
    <el-card class="el-input" v-if="detail && detail.id">
      <el-form ref="detail" :model="detail" label-width="80px">
        <el-form-item label="邮箱">
          <div>{{ detail.email }}</div>
        </el-form-item>
        <el-form-item label="用户昵称">
          <el-input type="text" v-model="detail.name"></el-input>
        </el-form-item>
        <el-form-item label="是否患病">
          <el-switch v-model="detail.isSick"></el-switch>
          <div v-if="detail.isSick">
            <el-tag
              v-for="tag in detail.sick"
              :key="tag"
              closable
              style="margin-right: 5px; margin-bottom: 5px"
              @close="handleTagClose(tag)"
            >
              {{ tag }}
            </el-tag>
            <span v-if="inputVisible">
              <el-input
                class="input-new-tag"
                v-model="inputValue"
                ref="saveTagInput"
                size="small"
                @keyup.enter.native="handleInputConfirm"
                @blur="handleInputConfirm"
              />
              <el-button @click="inputVisible = false">取消</el-button>
            </span>
            <el-button
              v-else
              class="button-new-tag"
              size="small"
              @click="showInput"
              >+ 添加</el-button
            >
          </div>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="detail.sex" placeholder="请选择">
            <el-option
              v-for="item in sexOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="detail.age" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="toHome">取消</el-button>
        </el-form-item>
      </el-form>

      <div class="del-user"><span @click="deleteUser">退出登陆</span></div>
    </el-card>
  </div>
</template>
<script>
import { userInfo, userUpdate } from "@/api";

export default {
  data() {
    return {
      detail: {},
      sexOptions: [
        {
          value: "woman",
          label: "女",
        },
        {
          value: "man",
          label: "男",
        },
        {
          value: "other",
          label: "保密",
        },
      ],
      inputVisible: false,
      inputValue: "",
    };
  },
  mounted() {
    this.pageInit();
  },
  methods: {
    async onSubmit() {
      const params = {
        ...this.detail,
      };
      // if (this.inputValue) {
      //   params.isSick = true;
      // }
      if (!this.detail.isSick) {
        this.detail.sick = [];
      }
      console.log(110, this.detail.isSick);
      const res = await userUpdate(params);
      if (res.data) {
        window.location.reload();
      }
    },
    async pageInit() {
      const token = localStorage.getItem("token");
      const res = await userInfo({ token });
      this.detail = res.data;
      console.log(117, this.detail);
      if (this.detail.isSick && this.detail.isSick === true) {
        // this.inputValue = this.detail.sick;
      }
      this.$set(this.detail, "isSick", this.detail.isSick === true);
    },
    toHome(query) {
      console.log(101);
      this.$router.replace({
        name: "home",
        query,
      });
    },
    deleteUser() {
      this.$alert("退出前用户之后无法使用订单信息！", "提示", {
        confirmButtonText: "确定",
        callback: (action) => {
          this.$message({
            type: "info",
            message: `删除成功！`,
          });
          localStorage.clear();
          this.toHome({
            type: "reload",
          });
        },
      });
    },
    handleTagClose(tag) {
      console.log(tag);
      const sick = this.detail.sick.filter((name) => name !== tag);
      this.detail.sick = this.detail = {
        ...this.detail,
        sick: [...sick],
      };
    },
    showInput() {
      this.inputVisible = true;
      const detail = {
        ...this.detail,
        // sick: [],
      };
      if (detail?.sick) {
        detail.sick = [];
      }
      this.detail = { ...detail };
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      const sick = new Set([...(this.detail?.sick || []), this.inputValue]);
      this.detail.sick = this.detail = {
        ...this.detail,
        sick: [...sick],
      };
    },
  },
};
</script>
<style lang="less" scoped>
.page-box {
  padding: 24px;

  .title {
  }

  .del-user {
    text-align: right;

    span {
      background-color: rgba(255, 0, 0, 0.29);
      color: red;
      padding: 1px 3px;
      font-size: 10px;
      cursor: pointer;
    }
  }

  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }

  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
}
</style>
