<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="sys_user表id" prop="userId">
          <el-input v-model="formData.userId" placeholder="请输入sys_user表id"/>
        </el-form-item>
        <el-form-item label="用户类型" prop="typeCode">
          <el-input v-model="formData.typeCode" placeholder="请输入用户类型"/>
        </el-form-item>
        <el-form-item label="所属企业code（部门表code）" prop="deptCode">
          <el-input v-model="formData.deptCode" placeholder="请输入所属企业code（部门表code）"/>
        </el-form-item>
        <el-form-item label="客户名称（政企客户填企业名称）" prop="cusName">
          <el-input v-model="formData.cusName" placeholder="请输入客户名称（政企客户填企业名称）"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="phoneNumber">
          <el-input v-model="formData.phoneNumber" placeholder="请输入联系方式"/>
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker clearable v-model="formData.birthday" type="date" value-format="timestamp"
                          placeholder="选择生日"/>
        </el-form-item>
        <el-form-item label="住址" prop="address">
          <el-input v-model="formData.address" 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="idCard">
          <el-input v-model="formData.idCard" placeholder="请输入身份证号"/>
        </el-form-item>
        <el-form-item label="身份证照片">
          <ImageUpload v-model="formData.idCardImage"/>
        </el-form-item>
        <el-form-item label="客户初始来源" prop="dataFrom">
          <el-input v-model="formData.dataFrom" placeholder="请输入客户初始来源"/>
        </el-form-item>
        <el-form-item label="最近业务办理时间" prop="nearDoTime">
          <el-date-picker clearable v-model="formData.nearDoTime" type="date" value-format="timestamp"
                          placeholder="选择最近业务办理时间"/>
        </el-form-item>
        <el-form-item label="最近办理业务（数据字典业务标识）">
          <Editor v-model="formData.nearDoContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="邀请者user_id" prop="inviter">
          <el-input v-model="formData.inviter" placeholder="请输入邀请者user_id"/>
        </el-form-item>
        <el-form-item label="邀请者类型" prop="inviterType">
          <el-select v-model="formData.inviterType" placeholder="请选择邀请者类型">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="客户状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </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 CustomerMainApi from '@/api/base/customer';
import ImageUpload from '@/components/ImageUpload';
import Editor from '@/components/Editor';

export default {
  name: "CustomerMainForm",
  components: {
    ImageUpload,
    Editor,
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        userId: undefined,
        typeCode: undefined,
        deptCode: undefined,
        cusName: undefined,
        phoneNumber: undefined,
        birthday: undefined,
        address: undefined,
        sex: undefined,
        idCard: undefined,
        idCardImage: undefined,
        dataFrom: undefined,
        nearDoTime: undefined,
        nearDoContent: undefined,
        inviter: undefined,
        inviterType: undefined,
        status: undefined,
      },
      // 表单校验
      formRules: {},
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await CustomerMainApi.getCustomerMain(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 CustomerMainApi.updateCustomerMain(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await CustomerMainApi.createCustomerMain(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        userId: undefined,
        typeCode: undefined,
        deptCode: undefined,
        cusName: undefined,
        phoneNumber: undefined,
        birthday: undefined,
        address: undefined,
        sex: undefined,
        idCard: undefined,
        idCardImage: undefined,
        dataFrom: undefined,
        nearDoTime: undefined,
        nearDoContent: undefined,
        inviter: undefined,
        inviterType: undefined,
        status: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
