<!--
 * @Author: liuzl 461480418@qq.com
 * @Date: 2023-01-09 15:34:20
 * @LastEditors: liuzhenlin
 * @LastEditTime: 2023-01-10 09:53:19
 * @Description: file content
 * @FilePath: \订单全流程管理系统\src\views\contract\components\EditInvoice.vue
-->
<template>
  <el-dialog :title="title" :visible.sync="editVisible" @close="handleClose">
    <el-form ref="editForm" :model="editForm" :rules="editRules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="合同编号" prop="contractCode">
            <el-input v-model="editForm.contractCode" :disabled="Boolean(details)" placeholder="请输入合同编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开票金额" prop="invoiceAmount">
            <el-input v-model.number="editForm.invoiceAmount" clearable placeholder="请输入开票金额" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="开票日期" prop="invoiceDate">
            <el-date-picker
              v-model="editForm.invoiceDate"
              placeholder="选择开票日期"
              style="width: 100%"
              type="date"
              value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开票类型" prop="invoiceType">
            <el-select v-model="editForm.invoiceType" placeholder="请选择开票类型" style="width: 100%">
              <el-option v-for="item in invoiceTypeData" :key="item.key" :label="item.value" :value="item.key" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="editForm.remark" clearable placeholder="请输入备注" rows="5" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button v-show="!editForm.id" type="primary" @click="invoiceSave">保存</el-button>
      <el-button v-show="editForm.id" type="primary" @click="contractEdit">保存</el-button>
      <el-button @click="editVisible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import to from 'await-to-js'
  import invoiceApi from '@/api/contract/invoice'
  export default {
    props: {
      details: {
        type: Object,
        default: () => {},
      },
      invoiceTypeData: {
        type: Array,
        default: () => [],
      },
    },
    data() {
      return {
        title: '新建发票',
        editVisible: false,
        editForm: {
          invoiceDate: '', //开票日期
          contractCode: '', //合同编号
          contractId: null, //合同id
          invoiceAmount: '', //开票金额
          invoiceType: '', //开票类型
          remark: '', //备注
        },
        editRules: {
          contractCode: [{ required: true, trigger: 'blur', message: '请选择合同' }],
          invoiceAmount: [{ required: true, trigger: 'blur', message: '请输入开票金额' }],
          invoiceDate: [{ required: true, trigger: 'change', message: '请选择开票日期' }],
          invoiceType: [{ required: true, trigger: 'chgange', message: '请输入开票类型' }],
        },
      }
    },
    mounted() {},
    methods: {
      async init(id) {
        if (!id) {
          this.title = '新建发票'
          if (this.details) {
            console.log(this.details)
            this.editForm.contractCode = this.details.contractCode
            this.editForm.contractId = this.details.id
            console.log(this.editForm)
          }
        } else {
          this.title = '编辑发票'
          const [err, res] = await to(invoiceApi.getDetails({ id }))
          if (err) return
          if (res.data) {
            this.editForm = res.data
          }
        }
        this.editVisible = true
      },
      // 保存发票
      async invoiceSave() {
        let params = { ...this.editForm }
        const [valid] = await to(this.$refs.editForm.validate())
        if (valid == false) return
        const [err, res] = await to(invoiceApi.addInvoice(params))
        if (err) return
        if (res.code == 200) this.$message.success(res.msg)
        else return
        this.editVisible = false
        this.$emit('invoiceSave')
      },
      // 编辑发票
      async contractEdit() {
        let params = { ...this.editForm }
        const [valid] = await to(this.$refs.editForm.validate())
        if (valid == false) return
        const [err, res] = await to(invoiceApi.updateInvoice(params))
        if (err) return
        if (res.code == 200) this.$message.success(res.msg)
        else return
        this.editVisible = false
        this.$emit('invoiceSave')
      },
      handleClose() {
        this.editForm = {
          invoiceDate: '', //开票日期
          contractCode: '', //合同编号
          contractId: null, //合同id
          invoiceAmount: '', //开票金额
          invoiceType: '', //开票类型
          remark: '', //备注
        }
        this.$refs.editForm.resetFields()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .mb10 {
    margin-bottom: 10px;
  }
  .proj-col {
    text-align: right;
  }
</style>
