<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" type="flex" justify="center">
        <el-col :span="16">
          <el-form-item label="电话号码" prop="tel">
            <el-input v-model.trim="customerForm.tel" :disabled="disabled" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" type="flex" justify="center">
        <el-col :span="16" >
          <el-form-item label="备注" prop="name">
            <el-input v-model.trim="customerForm.name" :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 { addBlack } from '@/api/blackCus'

export default {
  name: 'EditBlack',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      customerForm: {
        name: '', // 姓名
        tel: '' // 联系方式1
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增',
        info: '详情'
      }, // 表头显示标题
      rules: {
        name: [{ required: true, message: '备注不能为空', trigger: ['blur', 'change'] }],
        tel: [{
          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'] }]
      }, // 前端校验规则
      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()
        })
      }
    },
    // 重置表单
    resetForm() {
      this.customerForm = {
        id: '', // 编号
        name: '', // 姓名
        tel1: '', // 联系方式1
        tel2: '', // 联系方式2
        cardNo: '', // 证件号码
        address: '', // 地址
        remarks: '', // 备注
        type: '', // 类型
        wx: '', // 微信
        email: '', // 邮箱
        duty: '' // 职位
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.customerForm)
        if (valid) {
          this.btnLoading = true
          addBlack(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>
