<template>
  <div class="data-info">
    <el-card>
      <el-form :model="dataForm" label-width="120px">
        <el-form-item label="用户名:">
          <el-input v-model="dataForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码:">
          <el-input type="password" v-model="dataForm.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码:">
          <el-input
            type="password"
            v-model="dataForm.confirmPassword"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号码:">
          <el-input v-model="dataForm.mobile"></el-input>
        </el-form-item>

        <el-form-item label="邮箱地址:">
          <el-input v-model="dataForm.email"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名">
          <el-input v-model.trim="dataForm.real_name" />
        </el-form-item>
        <!-- <el-form-item label="工号">
          <el-input v-model.trim="dataForm.code" />
        </el-form-item>
        <el-form-item label="职位">
          <el-input v-model.trim="dataForm.job" />
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model.trim="dataForm.contact" />
        </el-form-item> -->

        <el-form-item label="所属角色:">
          <el-select
            v-model="dataForm.role_id"
            clearable
            placeholder="请选择角色"
            size="large"
            style="width: 100%"
          >
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用户状态:">
          <el-select
            v-model="dataForm.status"
            placeholder="请选择状态"
            size="large"
            style="width: 100%"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>

    <div style="text-align: right; margin-top: 20px">
      <el-button type="warning" @click="btnSave">确认保存</el-button>
    </div>
  </div>
</template>

<script>
import { searchAdminRole } from "@/api/AdminRole";
import Tinymce from "@/components/Tinymce";
import { createAdmin, updateAdmin, getAdminInfo } from "@/api/admin";
export default {
  components: {
    Tinymce,
  },
  emits: ["onClose"],
  props: {
    id: {
      type: Number,
      default: 0,
    },
  },
  watch: {
    id: {
      handler(v) {
        let _this = this;
        if (v == null || v <= 0) return;
        _this.getInfo(v);
      },
      immediate: true,
      deep: true,
    },
  },
  data() {
    return {
      activeName: "base",
      currentCategory: [],
      allCategories: [],
      roleList: [],
      dataForm: {
        id: 0,
        username: "",
        password: "",
        confirmPassword: "",
        mobile: "",
        email: "",
        status: 5,
        real_name: "",
        code: "",
        job: "",
        contact: "",
      },
      statusOptions: [
        {
          value: "",
          label: "请选择状态",
        },
        {
          value: 5,
          label: "启用",
        },
        {
          value: 6,
          label: "停用",
        },
      ],
    };
  },

  mounted() {
    let _this = this;
    _this.initRoleList();
  },
  methods: {
    //加载角色列表
    initRoleList() {
      let _this = this;
      searchAdminRole({ page: 1, pageSize: 999, status: 5 }).then((res) => {
        if (res.status != 200) return;
        _this.roleList = res.data.dataList;
        console.log("res", res);
      });
    },
    //保存更新
    btnSave(e) {
      let _this = this;
      // 添加密码长度校验
      if (_this.dataForm.password && _this.dataForm.password.length < 6) {
        _this.$alert("密码长度不能小于6位");
        return;
      }

      // 添加确认密码长度校验
      if (
        _this.dataForm.confirmPassword &&
        _this.dataForm.confirmPassword.length < 6
      ) {
        _this.$alert("确认密码长度不能小于6位");
        return;
      }

      // 添加密码一致性校验
      if (_this.dataForm.password !== _this.dataForm.confirmPassword) {
        _this.$alert("密码和确认密码不一致");
        return;
      }

      if (_this.dataForm.id > 0) {
        updateAdmin(_this.dataForm).then((res) => {
          if (res.status != 200) return;
          _this.$alert("管埋员信息更新成功");
          _this.$emit("onClose");
        });
      } else {
        createAdmin(_this.dataForm).then((res) => {
          if (res.status != 200) return;
          _this.$alert("管理员账户创建成功");
          _this.$emit("onClose");
        });
      }
    },

    //产品详情
    getInfo(id) {
      let _this = this;
      let par = {
        id: id,
      };
      getAdminInfo(par).then((res) => {
        if (!res) return;
        if (res.status != 200) return;
        _this.dataForm = res.data;
      });
    },

    onChangeStatus(e) {
      this.dataForm.status = e;
    },
  },
};
</script>

<style lang="scss">
@import "./dataInfo.scss";
</style>
