<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="企业id（base_company表中的id）" prop="corpId">
                      <el-input v-model="formData.corpId" placeholder="请输入企业id（base_company表中的id）" />
                    </el-form-item>
                    <el-form-item label="用户id（system_users表的id）" prop="userId">
                      <el-input v-model="formData.userId" placeholder="请输入用户id（system_users表的id）" />
                    </el-form-item>
                    <el-form-item label="部门id(system_dept表中的id)" prop="deptId">
                      <el-input v-model="formData.deptId" placeholder="请输入部门id(system_dept表中的id)" />
                    </el-form-item>
                    <el-form-item label="工号" prop="workNo">
                      <el-input v-model="formData.workNo" placeholder="请输入工号" />
                    </el-form-item>
                    <el-form-item label="姓名" prop="name">
                      <el-input v-model="formData.name" placeholder="请输入姓名" />
                    </el-form-item>
                    <el-form-item label="手机号" prop="tel">
                      <el-input v-model="formData.tel" placeholder="请输入手机号" />
                    </el-form-item>
                    <el-form-item label="性别" prop="sex">
                      <el-radio-group v-model="formData.sex">
                            <el-radio label="1">请选择字典生成</el-radio>
                      </el-radio-group>
                    </el-form-item>
                    <el-form-item label="家庭住址" prop="address">
                      <el-input v-model="formData.address" placeholder="请输入家庭住址" />
                    </el-form-item>
                    <el-form-item label="工作日期" prop="workDate">
                      <el-date-picker clearable v-model="formData.workDate" type="date" value-format="timestamp" placeholder="选择工作日期" />
                    </el-form-item>
                    <el-form-item label="工龄" prop="workYear">
                      <el-input v-model="formData.workYear" placeholder="请输入工龄" />
                    </el-form-item>
                    <el-form-item label="入职日期" prop="joinedDate">
                      <el-date-picker clearable v-model="formData.joinedDate" type="date" value-format="timestamp" placeholder="选择入职日期" />
                    </el-form-item>
                    <el-form-item label="司龄" prop="joinedYear">
                      <el-input v-model="formData.joinedYear" placeholder="请输入司龄" />
                    </el-form-item>
                    <el-form-item label="学历" prop="education">
                      <el-input v-model="formData.education" placeholder="请输入学历" />
                    </el-form-item>
                    <el-form-item label="个人简介">
                      <Editor v-model="formData.content" :min-height="192"/>
                    </el-form-item>
                    <el-form-item label="唯一推广码（须保证全平台唯一，规则：字母+数字；字母全大写）" prop="uniqueCode">
                      <el-input v-model="formData.uniqueCode" placeholder="请输入唯一推广码（须保证全平台唯一，规则：字母+数字；字母全大写）" />
                    </el-form-item>
                    <el-form-item label="附件ids（infra_file表中的id，多个英文逗号拼接）" prop="fileIds">
                      <el-input v-model="formData.fileIds" placeholder="请输入附件ids（infra_file表中的id，多个英文逗号拼接）" />
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as StaffApi from '@/api/company/staff';
  import Editor from '@/components/Editor';
      export default {
    name: "StaffForm",
    components: {
          Editor,
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            corpId: undefined,
                            userId: undefined,
                            deptId: undefined,
                            workNo: undefined,
                            name: undefined,
                            tel: undefined,
                            sex: undefined,
                            address: undefined,
                            workDate: undefined,
                            workYear: undefined,
                            joinedDate: undefined,
                            joinedYear: undefined,
                            education: undefined,
                            content: undefined,
                            uniqueCode: undefined,
                            fileIds: undefined,
        },
        // 表单校验
        formRules: {
        },
                        };
    },
    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await StaffApi.getStaff(id);
            this.formData = res.data;
            this.title = "修改企业管理-员工信息";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增企业管理-员工信息";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
          const data = this.formData;
                  // 修改的提交
          if (data.id) {
            await StaffApi.updateStaff(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await StaffApi.createStaff(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            corpId: undefined,
                            userId: undefined,
                            deptId: undefined,
                            workNo: undefined,
                            name: undefined,
                            tel: undefined,
                            sex: undefined,
                            address: undefined,
                            workDate: undefined,
                            workYear: undefined,
                            joinedDate: undefined,
                            joinedYear: undefined,
                            education: undefined,
                            content: undefined,
                            uniqueCode: undefined,
                            fileIds: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>
