<template>
  <div>
    <el-button type="text" size="mini" :disabled="status !== '新建'" @click="onShow">修 改</el-button>
    <el-dialog title="修改开票信息" append-to-body :visible.sync="dialogFormVisible" width="400px">
      <el-alert
        style="margin-bottom:10px"
        v-if="showErrorMsg"
        title="本次开票金额超过可开票金额!"
        type="error">
      </el-alert>
      <el-form :model="form" size="small" label-position="right" label-width="100px">
        <el-form-item label="可开票金额">
          <el-input disabled v-model="form.uninvoiceamount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="订单单价">
          <el-input disabled v-model="form.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="本次开票金额">
          <el-input v-model="form.invoiceamount" @change="amountInputChange" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="本次开票数量">
          <el-input v-model="form.invoiceaqty" @change="qtyInputChange" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" style="width:120px" @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="warning" size="small" style="width:120px" :disabled="showErrorMsg" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props:['row','status'],
  data () {
    return {
      form:{},
      dialogFormVisible:false,
      showErrorMsg:false
    }
  },
  methods:{
    onShow () {
      this.dialogFormVisible = true
      this.form = Object.assign({},this.form,this.row)
    },
    amountInputChange () {
      this.form.invoiceaqty = this.form.invoiceamount / this.form.price
      this.checkAmount()
    },
    qtyInputChange () {
      this.form.invoiceamount = this.form.invoiceaqty * this.form.price
      this.checkAmount()
    },
    checkAmount () {
      if (this.form.invoiceamount > this.form.uninvoiceamount) {
        this.showErrorMsg = true
      } else {
        this.showErrorMsg = false
      }
    },
    async submit () {
      const res = await this.$api.requested({
        "id": "20221217091203",
        "content": {
          "sa_invoiceapplyid": this.$route.query.id, 
          "iteminfos": [
              {
                "sa_invoiceapply_orderid": this.form.sa_invoiceapply_orderid, 
                "saorderid": this.form.sa_orderid, 
                "sa_orderitemsid": this.form.sa_orderitemsid, 
                "price": this.form.price, 
                "invoiceamount": this.form.invoiceamount, 
                "invoiceaqty": 0
              }
          ]
        }
      })
      this.tool.showMessage(res,()=>{
        this.$emit('onSuccess')
        this.dialogFormVisible = false
      })
    },
  },

}

</script>
<style>
</style>