<template>
  <div>
    <el-button-group v-if="tool.checkAuth($route.name,'editBlueInvioce')" style="margin-bottom:10px">
      <el-button size="small" type="primary" :disabled="data.status !== '新建' && data.status !== '提交'" @click="addInvoiceapp">一键添加发票</el-button>
      <el-button size="small" type="primary" :disabled="data.status !== '提交' && data.status != '提交'" @click="addInvoiceapp2">添加发票</el-button>
      <el-button size="small" type="primary" :disabled="selectData.length === 0 || (data.status !== '提交' && data.status != '审核') || data.byhand !== 0" @click="clickCreateBlueInvioce">勾选创建发票</el-button>
      <el-button size="small" type="primary" :disabled="selectData.length === 0 || (data.status !== '提交' && data.status != '审核') || data.byhand !== 0" @click="clickInvioceQuery">发票创建查询</el-button>
      <el-button size="small" type="primary" :disabled="selectData.length === 0 ||  (data.status !== '提交' && data.status != '审核')" @click="dialogFormVisible=true">红冲勾选行</el-button>
    </el-button-group>
    <invoiceTable :data="data" ref="invoiceTable" @activeRow="activeRow" @selection="selection"></invoiceTable>
    <invoiceTablemx :status="data.status" ref="invoiceTablemx" @onSuccess="mxOnSuccess"></invoiceTablemx>

    <el-dialog append-to-body :visible.sync="dialogFormVisible" width="500px">
      <div slot="title" style="font-size: 15px">
        操作
      </div>
      <el-row :gutter="20">
        <el-form :model="form" :rules="rules" ref="form" label-width="90px" label-position="right" size="mini">
          <el-col :span="24">
            <el-form-item label="单号" prop="billinfono">
              <el-input v-model="form.billinfono" placeholder="单号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" placeholder="备注"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="warning" @click="blueInvioceToRed" class="normal-btn-width btn-warning">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import invoiceTable from './components/invoiceTable.vue'
import invoiceTablemx from './components/invoiceTablemx.vue'

export default {
  props:['data'],
  components:{
    invoiceTable,
    invoiceTablemx
  },
  data () {
    return {
      sa_invoicebillid:0,
      selectData:[],
      form: {
        "sa_invoicebillids":[],
        "billinfono":"",
        "remarks":""
      },
      dialogFormVisible:false,
      rules:{
        billinfono:[
          { required: true, message: '请输入单号', trigger: 'blur'},
        ],
      }
    }
  },
  methods:{
    // 一键添加发票
    async addInvoiceapp () {
      const res = await this.$api.requested({
        "id": "20221223151803",
        "version":1,
        "content": {
          "sa_invoiceapplyid":this.$route.query.id
        }
      })
      this.tool.showMessage(res,()=>{
        this.$refs['invoiceTable'].listData()
      })
    },
    async addInvoiceapp2 () {
      const res = await this.$api.requested({
        "id": "20230225133303",
        "version":1,
        "content": {
          "sa_invoiceapplyid":this.$route.query.id
        }
      })
      this.tool.showMessage(res,()=>{
        this.$refs['invoiceTable'].listData()
      })
    },
    activeRow (row){
      this.$refs['invoiceTablemx'].listData(row.sa_invoicebillid)
    },
    selection (selection) {
      this.selectData = selection
    },
    clickCreateBlueInvioce () {
      this.selectData.forEach(e=>{
        this.createBlueInvioce(e.sa_invoicebillid)
      })
    },
    // 创建蓝字发票
    async createBlueInvioce(sa_invoicebillid) {
      const res = await this.$api.requested({
        "id": "20221226151803",
        "content": {
          "sa_invoiceapplyid":this.$route.query.id,
          "sa_invoicebillid":sa_invoicebillid
        }
      })
      this.tool.showMessage(res,()=>{
        this.$refs['invoiceTable'].listData()
        this.clickInvioceQuery()
      })
    },
    clickInvioceQuery () {
      this.selectData.forEach(e=>{
        this.invioceQuery(e.sa_invoicebillid)
      })
    },
    // 发票查询
    async invioceQuery (sa_invoicebillid) {
      const res = await this.$api.requested({
        "id": "20221226152003",
        "content": {
          "sa_invoicebillid":sa_invoicebillid
        }
      })
      this.tool.showMessage(res,()=>{
        this.$refs['invoiceTable'].listData()
      })
    },
    // 红冲蓝字发票
    async blueInvioceToRed () {
      this.form.sa_invoicebillids = this.selectData.map(e=>{
        return e.sa_invoicebillid
      })
      const res = await this.$api.requested({
        "id": "20221226091503",
        "content": this.form
      })
      this.tool.showMessage(res,()=>{
        this.$refs['invoiceTable'].listData()
        this.dialogFormVisible = false
      })
    },
    mxOnSuccess () {
      this.$refs['invoiceTable'].listData()
    }
  }
}

</script>
<style>
</style>