<template>
  <el-dialog
    :title="!dataForm.clientId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="客户名称" prop="clientName">
      <el-input v-model="dataForm.clientName" placeholder="客户名称"></el-input>
    </el-form-item>
    <el-form-item label="法人代表" prop="juridicalPerson">
      <el-input v-model="dataForm.juridicalPerson" placeholder="法人代表"></el-input>
    </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="industry">
      <el-input v-model="dataForm.industry" placeholder="行业"></el-input>
    </el-form-item>
    <el-form-item label="是否启用" prop="isOn">
      <el-input v-model="dataForm.isOn" placeholder="是否启用"></el-input>
    </el-form-item>
    <el-form-item label="创建日期" prop="createTime">
      <el-input v-model="dataForm.createTime" 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,
        dataForm: {
          clientId: 0,
          clientName: '',
          juridicalPerson: '',
          address: '',
          industry: '',
          isOn: '',
          createTime: ''
        },
        dataRule: {
          clientName: [
            { required: true, message: '客户名称不能为空', trigger: 'blur' }
          ],
          juridicalPerson: [
            { required: true, message: '法人代表不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          industry: [
            { required: true, message: '行业不能为空', trigger: 'blur' }
          ],
          isOn: [
            { required: true, message: '是否启用不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.clientId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.clientId) {
            this.$http({
              url: this.$http.adornUrl(`/client/client/info/${this.dataForm.clientId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.clientName = data.client.clientName
                this.dataForm.juridicalPerson = data.client.juridicalPerson
                this.dataForm.address = data.client.address
                this.dataForm.industry = data.client.industry
                this.dataForm.isOn = data.client.isOn
                this.dataForm.createTime = data.client.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/client/client/${!this.dataForm.clientId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'clientId': this.dataForm.clientId || undefined,
                'clientName': this.dataForm.clientName,
                'juridicalPerson': this.dataForm.juridicalPerson,
                'address': this.dataForm.address,
                'industry': this.dataForm.industry,
                'isOn': this.dataForm.isOn,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
