<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <el-dialog
      :title="!dataForm.id ? '添加' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <!-- 新增和修改表单 -->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataSubmit()" label-width="80px">
    <el-form-item label="客户代码" prop="custCode">
      <el-input v-model="dataForm.custCode" placeholder="客户代码"></el-input>
    </el-form-item>
    <el-form-item label="客户名称" prop="custName">
      <el-input v-model="dataForm.custName" placeholder="客户名称"></el-input>
    </el-form-item>
    <el-form-item label="营业执照号码" prop="licenseNumber">
      <el-input v-model="dataForm.licenseNumber" placeholder="营业执照号码"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" prop="contactWay">
      <el-input v-model="dataForm.contactWay" placeholder="联系方式"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="联系人" prop="contactPerson">
      <el-input v-model="dataForm.contactPerson" placeholder="联系人"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import * as api from '@/api/cmcust.js'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          custCode: '' ,
          custName: '' ,
          licenseNumber: '' ,
          contactWay: '' ,
          status: '' ,
          contactPerson: '' ,
          remark: '' ,
        },
        dataRule: {
          custCode: [
            { required: true, message: '客户代码  为必填项', trigger: 'blur' }
          ],
          custName: [
            { required: true, message: '客户名称  为必填项', trigger: 'blur' }
          ],
          licenseNumber: [
            { required: true, message: '营业执照号码  为必填项', trigger: 'blur' }
          ],
          contactWay: [
            { required: true, message: '联系方式  为必填项', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态  为必填项', trigger: 'blur' }
          ],
          contactPerson: [
            { required: true, message: '联系人  为必填项', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注  为必填项', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) { // 初始化表单验证规则
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(function() {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            api.cmcustDetailApi(id).then(function(res) {
              this.dataForm = res;
            }.bind(this))
          }
        }.bind(this))
      },
      // 表单数据提交
      dataSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              api.cmcustUpdateApi(this.dataForm).then(function(res) {
                this.$message.success('保存成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            } else {
              api.cmcustCreateApi(this.dataForm).then(function(res) {
                this.$message.success('新增成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            }
          }
        })
      }
    }
  }
</script>
