<template>
  <el-dialog
    :title="!id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <h4>基础信息</h4>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="dataForm.customerName" placeholder="客户名称"></el-input>
      </el-form-item>
      <el-form-item label="客户级别" prop="level">
        <el-select
          v-model="dataForm.level"
          remote
          placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="dataForm.address"  placeholder="详细地址"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="tel">
        <el-input v-model="dataForm.tel"  placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="邮编">
        <el-input v-model="dataForm.zipCode" placeholder="邮编"></el-input>
      </el-form-item>
      <el-form-item label="传真">
        <el-input v-model="dataForm.fax" placeholder="传真"></el-input>
      </el-form-item>
      <el-form-item label="网址">
        <el-input v-model="dataForm.url" placeholder="网址"></el-input>
      </el-form-item>
      <el-form-item label="公司简介">
        <el-input v-model="dataForm.introduction" type="textarea" placeholder="公司简介"></el-input>
      </el-form-item>
      <h4>业务联系人</h4>
      <el-form-item label="联系人" prop="contact">
        <el-input v-model="dataForm.contact"  placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="contactTel">
        <el-input v-model="dataForm.contactTel"  placeholder="手机号码"></el-input>
      </el-form-item>
      <el-form-item label="职位">
        <el-input v-model="dataForm.contactPosition"  placeholder="职位"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="dataForm.contactEmail"  placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="对接业务员" prop="bizManager">
        <el-input v-model="dataForm.bizManager"  placeholder="对接业务员"></el-input>
      </el-form-item>
      <h4>财务信息</h4>
      <el-form-item label="企业税号" prop="taxNumber">
        <el-input v-model="dataForm.taxNumber"  placeholder="企业税号"></el-input>
      </el-form-item>
      <el-form-item label="发票抬头" prop="invoiceTitle">
        <el-input v-model="dataForm.invoiceTitle"  placeholder="发票抬头"></el-input>
      </el-form-item>
      <el-form-item label="开户银行" prop="bank">
        <el-input v-model="dataForm.bank"  placeholder="开户银行"></el-input>
      </el-form-item>
      <el-form-item label="银行账号" prop="account">
        <el-input v-model="dataForm.account"  placeholder="银行账号"></el-input>
      </el-form-item>
      <el-form-item label="单位地址" prop="billingAddr">
        <el-input v-model="dataForm.billingAddr"  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="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        options: [
          {
            value: 'A',
            label: 'A'
          },
          {
            value: 'B',
            label: 'B'
          }
        ],
        id: 0,
        islook: false,
        dataForm: {
          account: '',
          address: '',
          bank: '',
          billingAddr: '',
          bizManager: '',
          contact: '',
          contactEmail: '',
          contactPosition: '',
          contactTel: '',
          customerName: '',
          customerType: 0,
          fax: '',
          introduction: '',
          invoiceTitle: '',
          level: '',
          taxNumber: '',
          tel: '',
          url: '',
          zipCode: ''
        },
        dataRule: {
          customerName: [{ required: true, message: '客户名称不能为空', trigger: 'blur' }],
          level: [{ required: true, message: '客户级别不能为空', trigger: 'blur' }],
          address: [{ required: true, message: '详细地址不能为空', trigger: 'blur' }],
          tel: [{ required: true, message: '电话不能为空', trigger: 'blur' }],
          contact: [{ required: true, message: '联系人不能为空', trigger: 'blur' }],
          contactTel: [{ required: true, message: '手机号码不能为空', trigger: 'blur' }],
          bizManager: [{ required: true, message: '对接业务员不能为空', trigger: 'blur' }],
          taxNumber: [{ required: true, message: '企业税号不能为空', trigger: 'blur' }],
          invoiceTitle: [{ required: true, message: '发票抬头不能为空', trigger: 'blur' }],
          bank: [{ required: true, message: '开户银行不能为空', trigger: 'blur' }],
          account: [{ required: true, message: '银行账号不能为空', trigger: 'blur' }],
          billingAddr: [{ required: true, message: '单位地址不能为空', trigger: 'blur' }]
        }
      }
    },
    methods: {
      init (id) {
        this.id = id || 0
        this.visible = true
        if (this.id) {
          this.$http({
            url: this.$http.adornUrl(`/biz-service/cusCustomer/info/${this.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === '200') {
              this.dataForm = data.data
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user-service/user/${!this.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      validateField (type) {
        this.$refs.dataForm.validateField(type)
      }
    }
  }
</script>
