<template>
  <!-- <el-dialog
    :title="!id ? '新增' : display ? '详情' : '编辑'"
    :close-on-click-modal="false"
    :visible.sync="visible"> -->
  <div>
    <div class="my-title">{{ !id ? '新增' : display ? '详情' : '编辑' }}</div>
    <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" :disabled="display" placeholder="客户名称"></el-input>
      </el-form-item>
      <el-form-item label="客户类型" prop="customerType">
        <el-select
          v-model="dataForm.customerType"
          :disabled="display"
          placeholder="请选择">
          <el-option v-for="item in optionsCT"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客户级别" prop="level">
        <el-select
          v-model="dataForm.level"
          :disabled="display"
          remote
          placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.code"
            :label="item.value"
            :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="dataForm.address" :disabled="display" placeholder="详细地址"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="tel">
        <el-input v-model="dataForm.tel" :disabled="display" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="邮编">
        <el-input v-model="dataForm.zipCode" :disabled="display" placeholder="邮编"></el-input>
      </el-form-item>
      <el-form-item label="传真">
        <el-input v-model="dataForm.fax" :disabled="display" placeholder="传真"></el-input>
      </el-form-item>
      <el-form-item label="网址">
        <el-input v-model="dataForm.url" :disabled="display" placeholder="网址"></el-input>
      </el-form-item>
      <el-form-item label="公司简介">
        <el-input v-model="dataForm.introduction" :disabled="display" type="textarea" placeholder="公司简介"></el-input>
      </el-form-item>
      <h4>业务联系人</h4>
      <el-form-item label="联系人" prop="contact">
        <el-input v-model="dataForm.contact" :disabled="display" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="contactTel">
        <el-input v-model="dataForm.contactTel" :disabled="display" placeholder="手机号码"></el-input>
      </el-form-item>
      <el-form-item label="职位">
        <el-input v-model="dataForm.contactPosition" :disabled="display" placeholder="职位"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="dataForm.contactEmail" :disabled="display" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="对接业务员" prop="bizManager">
        <user-component v-model="dataForm.bizManager" :disabled="display" :user-id.sync="dataForm.bizManager" @userSelected="change"/>
        <!-- <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" :disabled="display" placeholder="企业税号"></el-input>
      </el-form-item>
      <el-form-item label="发票抬头" prop="invoiceTitle">
        <el-input v-model="dataForm.invoiceTitle" :disabled="display" placeholder="发票抬头"></el-input>
      </el-form-item>
      <el-form-item label="开户银行" prop="bank">
        <el-input v-model="dataForm.bank" :disabled="display" placeholder="开户银行"></el-input>
      </el-form-item>
      <el-form-item label="银行账号" prop="account">
        <el-input v-model="dataForm.account" :disabled="display" placeholder="银行账号"></el-input>
      </el-form-item>
      <el-form-item label="单位地址" prop="billingAddr">
        <el-input v-model="dataForm.billingAddr" :disabled="display" placeholder="单位地址"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onChose">取消</el-button>
      <el-button v-if="!display" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </div>
  <!-- </el-dialog> -->
</template>

<script>
  import UserComponent from '../common/user-component'
  import { getLevel } from '@/api/cus'
  import { isMobile } from '@/utils/validate'
  export default {
    components: { UserComponent },
    computed: {
      orgId: {
        get () { return this.$store.state.user.orgId }
      }
    },
    data () {
      let validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        options: [],
        // 客户类型
        optionsCT: [
          {
            value: '0',
            label: '公司'
          },
          {
            value: '1',
            label: '个人'
          }
        ],
        id: 0,
        display: false,
        dataForm: {},
        dataRule: {
          customerName: [{ required: true, message: '客户名称不能为空', trigger: 'blur' }],
          customerType: [{required: true, message: '客户类型不能为空', trigger: 'blur'}],
          level: [{ required: true, message: '客户级别不能为空', trigger: 'blur' }],
          address: [{ required: true, message: '详细地址不能为空', trigger: 'blur' }],
          tel: [
            { required: true, message: '电话不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ],
          contact: [{ required: true, message: '联系人不能为空', trigger: 'blur' }],
          contactTel: [
            { required: true, message: '手机号码不能为空', trigger: 'blur' },
            { validator: validateMobile, 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: {
      onChose () {
        this.$emit('onChose')
      },
      async init (id, disabled) {
        this.dataForm = {}
        this.display = disabled
        this.id = id || 0
        this.visible = true
        await getLevel().then(({data}) => {
          if (data && data.code === '200') {
            this.options = data.data
          }
        })
        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
            }
          })
        }
      },
      change (item) {
        this.dataForm.bizManager = item.value
        this.dataForm.bizManagerName = item.name
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/biz-service/cusCustomer/${!this.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({...this.dataForm, orgId: this.orgId})
            }).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.onChose()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      validateField (type) {
        this.$refs.dataForm.validateField(type)
      }
    }
  }
</script>
