<template>
  <el-dialog
    :title="!dataForm.contractId ? '新增' : '修改'"
    :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="contractType">
      <el-input v-model="dataForm.contractType" placeholder="合同类型"></el-input>
    </el-form-item>
    <el-form-item label="是否是主合同" prop="isMaster">
      <el-input v-model="dataForm.isMaster" placeholder="是否是主合同"></el-input>
    </el-form-item>
    <el-form-item label="合同编号" prop="contractNumber">
      <el-input v-model="dataForm.contractNumber" placeholder="合同编号"></el-input>
    </el-form-item>
    <el-form-item label="客户id" prop="clientId">
      <el-input v-model="dataForm.clientId" placeholder="客户id"></el-input>
    </el-form-item>
    <el-form-item label="物料信息" prop="materialJson">
      <el-input v-model="dataForm.materialJson" placeholder="物料信息"></el-input>
    </el-form-item>
    <el-form-item label="是否验收" prop="isAcceptance">
      <el-input v-model="dataForm.isAcceptance" 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-item label="创建日期" prop="acceptanceTime">
      <el-input v-model="dataForm.acceptanceTime" placeholder="创建日期"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remarks">
      <el-input v-model="dataForm.remarks" 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: {
          contractId: 0,
          contractType: '',
          isMaster: '',
          contractNumber: '',
          clientId: '',
          materialJson: '',
          isAcceptance: '',
          createTime: '',
          acceptanceTime: '',
          remarks: ''
        },
        dataRule: {
          contractType: [
            { required: true, message: '合同类型不能为空', trigger: 'blur' }
          ],
          isMaster: [
            { required: true, message: '是否是主合同不能为空', trigger: 'blur' }
          ],
          contractNumber: [
            { required: true, message: '合同编号不能为空', trigger: 'blur' }
          ],
          clientId: [
            { required: true, message: '客户id不能为空', trigger: 'blur' }
          ],
          materialJson: [
            { required: true, message: '物料信息不能为空', trigger: 'blur' }
          ],
          isAcceptance: [
            { required: true, message: '是否验收不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ],
          acceptanceTime: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ],
          remarks: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.contractId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.contractId) {
            this.$http({
              url: this.$http.adornUrl(`/tickets/contract/info/${this.dataForm.contractId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.contractType = data.contract.contractType
                this.dataForm.isMaster = data.contract.isMaster
                this.dataForm.contractNumber = data.contract.contractNumber
                this.dataForm.clientId = data.contract.clientId
                this.dataForm.materialJson = data.contract.materialJson
                this.dataForm.isAcceptance = data.contract.isAcceptance
                this.dataForm.createTime = data.contract.createTime
                this.dataForm.acceptanceTime = data.contract.acceptanceTime
                this.dataForm.remarks = data.contract.remarks
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tickets/contract/${!this.dataForm.contractId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'contractId': this.dataForm.contractId || undefined,
                'contractType': this.dataForm.contractType,
                'isMaster': this.dataForm.isMaster,
                'contractNumber': this.dataForm.contractNumber,
                'clientId': this.dataForm.clientId,
                'materialJson': this.dataForm.materialJson,
                'isAcceptance': this.dataForm.isAcceptance,
                'createTime': this.dataForm.createTime,
                'acceptanceTime': this.dataForm.acceptanceTime,
                'remarks': this.dataForm.remarks
              })
            }).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>
