<template>
  <div style="display: inline-block">
    <el-dialog
      :visible.sync="dialog"
      :close-on-click-modal="false"
      :title="title"
      append-to-body
      width="500px"
      @close="cancel"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        size="small"
        label-position="left"
        label-width="100px"
        style="width:410px;margin-left:25px;"
        hide-required-asterisk
      >
        <el-form-item label="员工编号" prop="staffNo">
          <el-input v-model="form.staffNo" style="width: 80%" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" style="width: 80%" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          size="mini"
          :loading="loading"
          type="primary"
          @click="doSubmit"
          >提交</el-button
        >
        <el-button size="mini" @click="dialog = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import store from '@/store'
// import { getToken } from '@/utils/auth'
import { saveTgInfo, getTgInfo } from "@/api/adviser.js";

export default {
  data() {
    return {
      loading: false,
      dialog: false,
      title: "新增投顾",
      id: null,
      form: {
        id: null,
        name: "",
        staffNo: ""
      },
      rules: {
        // name: [
        //   { required: true, message: '请输入展业姓名', trigger: 'blur' },
        //   { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        // ],
        // profile: [
        //   { required: true, message: '请输入个人简介', trigger: 'blur' },
        //   {
        //     min: 3,
        //     max: 500,
        //     message: '长度在 3 到 500 个字符',
        //     trigger: 'blur'
        //   }
        // ]
      }
    };
  },
  mounted() {
    // this.getLists()
  },
  watch: {
    dialog(newName) {
      if (newName) {
        // this.getInfo()
      }
    }
  },
  methods: {
    // 获取详情
    async getInfo() {
      const data = await getTgInfo({
        id: this.form.id
      });
      if (data.code === 0) {
        this.form = data.data;
        this.tags = [];
        this.form.tagList.forEach(v => {
          this.tags.push(v.id);
        });
      } else {
        this.$message.error(data.message);
      }
      console.log(data.data);
    },
    cancel() {
      this.resetForm();
    },
    // 提交
    async doSubmit() {
      if (!this.form.staffNo || !this.form.staffNo.trim()) {
        return this.$message.error("请输入员工编号");
      }
      if (!this.form.name || !this.form.name.trim()) {
        return this.$message.error("请输入姓名");
      }
      this.loading = true;
      const data = await saveTgInfo({
        avatar: this.form.avatar,
        id: this.form.id,
        sex: this.form.sex,
        profile: this.form.profile,
        showName: this.form.showName,
        license: this.form.license,
        phone: this.form.phone,
        tags: this.tags
      });
      if (data.code === 0) {
        this.dialog = false;
        this.resetForm();
        this.loading = false;
        this.$message.success("修改成功");
        this.$parent.getTgInfoList();
      } else {
        this.loading = false;
        this.$message.error(data.message);
      }
    },
    resetForm() {
      this.$refs["form"].resetFields();
      this.form = {
        id: null,
        name: "",
        staffNo: ""
      };
      this.id = null;
      this.loading = false;
      this.dialog = false;
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__title {
  font-weight: 700;
}
</style>
