<template>
  <!-- 弹出框表单 -->
  <el-drawer
    :title="title"
    :visible.sync="open"
    :direction="direction"
    :destroy-on-close="true"
    size="50%"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <Field
          :span="20"
          label="员工姓名"
          prop="name"
          v-model="form.name"
          placeholder="请输入员工姓名"
        />
        <Field
          :span="20"
          label="所属公司"
          prop="companyIds"
          v-model="form.companyIds"
          :multiple="true"
          type="select"
          :enum-data="dict.companyIds"
          placeholder="请输入所属公司"
        />
        <Field
          :span="20"
          label="职位"
          prop="positionId"
          v-model="form.positionId"
          type="select"
          :enum-data="dict.positionId"
          placeholder="请输入职位ID"
        />
        <Field
          :span="20"
          label="联系电话"
          prop="phoneNumber"
          v-model="form.phoneNumber"
          placeholder="请输入联系电话"
        />
        <!-- <Field :span="20" label="员工状态" type="select" prop="staffStatus" v-model="form.staffStatus" :enum-data="dict.staffStatus" placeholder="请输入员工状态"/> -->
        <Field :span="20" label="头像" prop="photoPath" placeholder="请输入照片"
          ><imageUpload
            v-model="form.photoPath"
            prePath="/file/preview"
            :slotP="'请上传 62*62px 的2MB以内的图片'"
          />
        </Field>
        <Field
          :span="20"
          label="邮件地址"
          prop="email"
          v-model="form.email"
          placeholder="请输入邮件地址"
        />
        <Field
          :span="20"
          label="备注"
          prop="remark"
          v-model="form.remark"
          type="textarea"
          placeholder="请输入备注"
        />
      </el-row>

      <form-buttons
        @submit="submitForm"
        v-if="pageInfo.type != 'view'"
        noCancelBtn
      />
    </el-form>
  </el-drawer>
</template>

<script>
import form from "@/assets/mixins/formdialog";
export default {
  name: "StaffDetail",
  mixins: [form],
  components: {},
  created() {
    this.changePath("staff");
  },
  data() {
    var checkEmail = (rule, value, callback) => {
      let mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
      if (!value) {
        //如果输入为空直接返回
        callback();
      } else {
        //如果字符串 string 中含有与 RegExpObject 匹配的文本，则返回 true，否则返回 false。
        if (mailReg.test(value)) {
          //匹配成功返回
          callback();
        } else {
          //匹配不成功返回错误显示
          callback(
            new Error("邮箱格式:xx@xx.xx，只含数字、大小写字母、下划线、横杠")
          );
        }
      }
    };
    var checkPhone = (rule, value, callback) => {
      let phoneReg = /^1[3|4|5|6|7|8|9][0-9]{9}$/;
      if (!value) {
        callback();
      } else {
        if (phoneReg.test(value)) {
          callback();
        } else {
          callback(new Error("请输入正确格式的11位电话号码"));
        }
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "员工基本信息",
      // 是否显示弹出层
      open: false,
      direction: "rtl",
      toString: ["gender", "staffType", "staffStatus", "source", "positionId"],
      toArrays: ["companyIds"],

      toDate: ["birthday", "entryDate", "regularDate", "leaveDate"],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入员工姓名", trigger: "blur" },
          { max: 64, message: "最多只能录入64个字符", trigger: "blur" },
        ],
        positionId: [
          { required: true, message: "请选择职位", trigger: "blur" },
        ],
        companyIds: [
          { required: true, message: "请选择所属企业", trigger: "blur" },
        ],
        phoneNumber: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
          { validator: checkPhone, trigger: "blur" },
          { max: 11, message: "最多只能录入11个字符", trigger: "blur" },
        ],
        email: [{ validator: checkEmail, trigger: "blur" }],
      },
    };
  },

  methods: {
    /** 编辑 */
    edit(row) {
      this.reset();
      this.query = { id: row.id };
      this.urls.currUrl = "staff/edit";
      this.getData();
      this.pageInfo.type = "edit";
      this.title = "修改员工基本信息";
    },
    /** 新增 */
    add(row) {
      this.reset();
      this.urls.currUrl = "staff/add";
      this.getData();
      this.pageInfo.type = "add";
      this.title = "新增员工基本信息";
    },
    /** 查看*/
    view(row) {
      this.reset();
      this.query = { id: row.id };
      this.urls.currUrl = "staff/view";
      this.getData();
      this.pageInfo.type = "view";
      this.title = "员工基本信息详细";
    },
    /**取消按钮 */
    cancel() {
      this.open = false;
    },
    /**获取数据后弹框 */
    afterRender(data) {
      this.open = true;
    },

    beforeSubmit(data) {
      data.companyIds = data.companyIds.join(",");

      return data;
    },

    afterSubmit(data) {
      this.open = false;
      this.$emit("ok");
    },

    // 表单重置
    reset() {
      this.form = {
        name: "",
        gender: 1,
        birthday: null,
        photoPath: "",
        Email: "",
        phoneNumber: "",
        idCard: "",
        workNum: "",
        companyName: "",
        positionId: null,
        positionName: "",
        staffType: 1,
        staffStatus: 1,
        source: 1,
        registerPath: "",
        entryDate: null,
        regularDate: null,
        leaveDate: null,
        remark: "",
        sumViews: 0,
        viewsByDay: 0,
        sendBusinessCardTimes: 0,
      };
      this.resetForm("form");
    },
    resetForm(refName) {
      if (this.$refs[refName]) {
        this.$refs[refName].resetFields();
      }
    },
  },
};
</script>
