<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form size="small" :model="inputForm" ref="inputForm" @keyup.enter.native="doSubmit()"
             label-width="80px" v-loading="loading" :class="method==='viewInvoice'?'readonly':''" :disabled="method==='viewInvoice'" @submit.native.prevent>
      <el-row :gutter="15">
        <el-col :span="12">
          <el-form-item label="业务编号" prop="businessCode" :rules="[{max: 50, message: '最大长度不能超过50个字符', trigger: 'blur'}]">
            <el-input v-model="inputForm.businessCode" placeholder="业务编号" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="业务类型" prop="businessType" :rules="[{max: 50, message: '最大长度不能超过50个字符', trigger: 'blur'}]">
            <el-input v-model="inputForm.businessType" placeholder="业务类型" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发票代码" prop="invoiceCode" :rules=" [{required: true, message: '发票代码不能为空', trigger: 'blur'}]">
            <el-input v-model="inputForm.invoiceCode" placeholder="发票代码" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发票号码" prop="invoiceNumber" :rules=" [{required: true, message: '发票号码不能为空', trigger: 'blur'}]">
            <el-input v-model="inputForm.invoiceNumber" placeholder="发票号码" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="购方企业名称" prop="firmName" :rules="[{max: 50, message: '最大长度不能超过50个字符', trigger: 'blur'}]">
            <el-input v-model="inputForm.firmName" placeholder="购方企业名称" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开票日期" prop="makeTime"
                        :rules="[
                  {required: true, message:'收款日期不能为空', trigger:'blur'}
                 ]">
            <el-date-picker
              style="width: 100%;"
              v-model="inputForm.makeTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              :disabled="true"
              placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商品名称" prop="name" :rules="[{max: 50, message: '最大长度不能超过50个字符', trigger: 'blur'}]">
            <el-input v-model="inputForm.name" placeholder="商品名称" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="金额" prop="money" :rules="[{max: 50, message: '最大长度不能超过50个字符', trigger: 'blur'}]">
            <el-input v-model="inputForm.money" placeholder="金额" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="税额" prop="tax" :rules="[{max: 50, message: '最大长度不能超过50个字符', trigger: 'blur'}]">
            <el-input v-model="inputForm.tax" placeholder="税额" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收入" prop="income" :rules="[{max: 50, message: '最大长度不能超过50个字符', trigger: 'blur'}]">
            <el-input v-model="inputForm.income" placeholder="收入" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目经理" prop="proposer" :rules="[{max: 50, message: '最大长度不能超过50个字符', trigger: 'blur'}]">
            <el-input v-model="inputForm.proposer" placeholder="项目经理" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门" prop="partner" :rules="[{max: 50, message: '最大长度不能超过50个字符', trigger: 'blur'}]">
            <el-input v-model="inputForm.partner" placeholder="部门" :disabled="true"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="收款日期" prop="gatheringTime"
                        :rules="[
                  {required: true, message:'收款日期不能为空', trigger:'blur'}
                 ]">
            <el-date-picker
              style="width: 100%;"
              v-model="inputForm.gatheringTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false" icon="el-icon-circle-close">关闭</el-button>
      <el-button size="small" v-if="method != 'viewInvoice'" type="primary" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import ReimbursementSys from '@/api/reimbursementSys/reimbursementSysService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          parent: {
            id: ''
          },
          businessCode: '', // 业务编号
          invoiceCode: '', // 发票代码
          invoiceNumber: '', // 发票号码
          firmName: '', // 购方企业名称
          makeTime: '', // 开票日期
          name: '', // 商品名称
          money: '', // 金额
          tax: '', // 税额
          income: '', // 收入
          proposer: '', // 申请人
          partner: '', // 合伙人
          businessType: '', // 业务类型
          gatheringTime: '' // 收款日期
        }
      }
    },
    reimbursementSys: null,
    created () {
      this.reimbursementSys = new ReimbursementSys()
    },
    methods: {
      init (method, obj) {
        this.method = method
        if (method === 'editInvoice') {
          this.title = '修改发票信息'
        } else if (method === 'viewInvoice') {
          this.title = '查看发票信息'
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs['inputForm'].resetFields()
          this.inputForm.id = obj.id
          this.inputForm.parent.id = obj.parent.id
          this.inputForm.parent.name = obj.parent.name
          if (method === 'editInvoice' || method === 'viewInvoice') { // 修改或者查看
            this.loading = true
            this.reimbursementSys.queryInvoiceById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.reimbursementSys.saveInvoice(this.inputForm).then(({data}) => {
              this.loading = false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
