<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="customerForm" size="small" label-position="right" label-width="90px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="姓名" prop="name">
            <el-input v-model.trim="customerForm.name" :disabled="disabled" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户类型" prop="type">
            <el-input v-model.trim="customerForm.type" :disabled="disabled" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="联系方式1" prop="tel1">
            <el-input v-model.trim="customerForm.tel1" :disabled="disabled" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系方式2" prop="tel2">
            <el-input v-model.trim="customerForm.tel2" :disabled="disabled" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="证件号码" prop="cardNo">
            <el-input v-model.trim="customerForm.cardNo" :disabled="disabled" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="地址" prop="address">
            <el-input v-model.trim="customerForm.address" :disabled="disabled" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注" prop="address">
            <el-input v-model.trim="customerForm.remarks" :disabled="disabled" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addCustomer, updateCustomer } from '@/api/customer'

export default {
  name: 'EditCustomer',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      customerForm: {
        id: '', // 编号
        name: '', // 姓名
        tel1: '', // 联系方式1
        tel2: '', // 联系方式2
        cardNo: '', // 证件号码
        address: '', // 地址
        remarks: '', // 备注
        type: '', // 类型
        wx: '', // 微信
        email: '', // 邮箱
        duty: '' // 职位
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增',
        info: '详情'
      }, // 表头显示标题
      rules: {
        name: [{ required: true, message: '姓名不能为空', trigger: ['blur', 'change'] }],
        email: [{
          required: false, // 是否必填
          message: '请输入邮箱地址', // 错误提示信息
          trigger: 'blur'// 检验方式（blur为鼠标点击其他地方，）
        },
        {
          type: 'email', // 要检验的类型（number，email，date等）
          message: '请输入正确的邮箱地址',
          trigger: ['blur', 'change'] }],
        tel1: [{
          required: true,
          pattern: /(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/, // 可以写正则表达式
          message: '请输入正确的电话号码',
          trigger: ['blur', 'change'] }],
        tel2: [{
          required: false,
          pattern: /(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/, // 可以写正则表达式
          message: '请输入正确的电话号码',
          trigger: ['blur', 'change'] }]
      }, // 前端校验规则
      roleName: '',
      disabled: false,
      deptShow: true,
      btnLoading: false // 保存按钮的加载中状态
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      this.btnLoading = false
      if (dialogStatus === 'create') { // 如果是新增，清除验证
        this.disabled = false
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update' || dialogStatus === 'info') { // 如果是修改，将row中数据填写到输入框中
        this.customerForm = {
          id: row.id, // 编号
          name: row.name, // 姓名
          email: row.email, // 邮箱，
          tel1: row.tel1, // 联系方式1
          tel2: row.tel2, // 联系方式2
          cardNo: row.cardNo, // 证件号码
          address: row.address, // 地址
          remarks: row.remarks, // 备注
          type: row.type, // 类型
          wx: row.wx, // 微信
          duty: row.duty // 职位
        }
        if (dialogStatus === 'info') {
          this.disabled = true
        } else {
          this.disabled = false
        }
      }
    },
    // 重置表单
    resetForm() {
      this.customerForm = {
        id: '', // 编号
        name: '', // 姓名
        tel1: '', // 联系方式1
        tel2: '', // 联系方式2
        cardNo: '', // 证件号码
        address: '', // 地址
        remarks: '', // 备注
        type: '', // 类型
        wx: '', // 微信
        email: '', // 邮箱
        duty: '' // 职位
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.customerForm)
        if (valid) {
          this.btnLoading = true
          addCustomer(this.customerForm).then(response => {
            if (response.code === 200) {
              this.$message.success('新增成功')
              this.$emit('refresh')
              this.dialogFormVisible = false
            }
          }).catch(_ => { // 异常情况，loading置为false
            this.btnLoading = false
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          updateCustomer(this.customerForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('refresh')
              this.dialogFormVisible = false
            }
          }).catch(_ => { // 异常情况，loading置为false
            this.btnLoading = false
          })
        }
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

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

  .el-dialog{
    width:700px
  }
  .el-select{
    width: 100%;
  }
</style>
