<template>
  <div style="display: inline-block">
    <el-dialog
      :visible.sync="dialog"
      :close-on-click-modal="false"
      :title="title"
      append-to-body
      width="700px"
    >
      <el-form
        ref="form1"
        :model="form"
        :rules="rules"
        size="small"
        label-position="left"
        label-width="120px"
        hide-required-asterisk
      >
        <el-form-item label="登陆账号" prop="staffNo">
          <div>{{ form.staffNo }}</div>
        </el-form-item>
        <el-form-item label="员工号" prop="staffNo">
          <div>{{ form.upId }}</div>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <div>{{ form.name }}</div>
        </el-form-item>
        <el-form-item label="投顾执业证号" prop="license">
          <div>{{ form.license }}</div>
        </el-form-item>
        <el-form-item label="投顾手机号" prop="license">
          <div>{{ form.phone }}</div>
        </el-form-item>
        <el-form-item label="投顾所在营业部" prop="name">
          <div>{{ form.deptName }}</div>
        </el-form-item>
        <el-form-item label="展业姓名" prop="showName">
          <div>{{ form.showName }}</div>
        </el-form-item>
        <el-form-item v-if="true" label="性别" prop="sex">
          <div>{{ form.sex === 1 ? "男" : "女" }}</div>
        </el-form-item>
        <el-form-item label="展示头像">
          <img :src="form.avatar" width="70" height="70" />
          <img
            :src="form.avatar"
            style="width: 70px; height: 70px; border-radius: 50%"
          />
        </el-form-item>
        <el-form-item label="个人简介" prop="profile">
          <div>{{ form.profile }}</div>
        </el-form-item>
        <el-form-item label="展示标签" prop="tagList">
          <el-tag
            v-for="tag in form.tagList"
            :key="tag.id"
            style="margin-right: 10px"
            >{{ tag.name }}</el-tag
          >
        </el-form-item>
        <el-form-item label="审核结果" prop="reason">
          <el-radio-group v-model="form.result" style="width: 178px">
            <el-radio :label="3"> 通过 </el-radio><br />
            <el-radio :label="4">
              驳回
              <el-input
                v-model="reason"
                type="text"
                :rows="2"
                :maxlength="200"
                :disabled="form.result === 4 ? false : true"
                placeholder="请输入驳回理由（必填）"
                style="margin-left: 20px"
            /></el-radio>
          </el-radio-group>
        </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="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateTgStatus, getTgInfo } from "@/api/adviser.js";

export default {
  data() {
    return {
      loading: false,
      dialog: false,
      title: "投顾审核",
      result: 0,
      reason: "",
      form: {
        username: "",
        name: "",
        type: "",
        profile: "",
        sex: "",
        tags: [],
        license: "",
        result: null,
        rules: ""
      },
      rules: {
        // reason: [
        //   { required: true, message: '请输入驳回理由', trigger: 'blur' },
        //   {
        //     min: 3,
        //     max: 200,
        //     message: '长度在 3 到 200 个字符',
        //     trigger: 'blur'
        //   }
        // ]
      }
    };
  },
  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;
      } else {
        this.$message.error(data.message);
      }
      console.log(this.form);
    },
    cancel() {
      this.resetForm();
    },
    async doSubmit() {
      if (!this.form.result) {
        return this.$message.error("请选择结果");
      }
      if (this.form.result === 4 && !this.reason) {
        return this.$message.error("请输入驳回理由");
      } else if (
        this.form.result === 4 &&
        (this.reason.length < 3 || this.reason.length > 200)
      ) {
        return this.$message.error("驳回理由长度在 3 到 200 个字符");
      }
      const params = {
        id: this.form.id
      };
      // 通过101:提交 103:通过 104:驳回 105:上架 106:下架
      if (this.form.result === 3) {
        params.event = 103;
      } else {
        // 驳回
        params.event = 104;
        params.reason = this.reason;
      }
      const { code, message } = await updateTgStatus(params);
      if (code === 0) {
        this.dialog = false;
        this.resetForm();
        this.$message.success("修改成功");
        this.$parent.getTgInfoList();
      } else {
        this.$message.error(message);
      }
    },
    resetForm() {
      this.dialog = false;
      this.form = {
        id: null,
        name: "",
        profile: "",
        sex: "",
        tagList: [],
        license: "",
        userName: "",
        userNo: "",
        showName: "",
        deptName: ""
      };
      this.reason = "";
    },
    // 分页器
    sizeChange(e) {
      this.page = 0;
      this.size = e;
    },
    pageChange(e) {
      this.page = e - 1;
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped></style>
