<template>
  <div>
    <el-button v-if="tool.checkAuth($route.name,'editRedInvioce')" :disabled="status != '审核'" size="small" type="primary" @click="onShow">添加红冲明细行</el-button>
    <el-dialog title="选择开票明细行" :visible.sync="dialogTableVisible" append-to-body>
      <el-input
        style="width:200px;margin-bottom:10px"
        size="small"
        suffix-icon="el-icon-search"
        placeholder="搜索"
        v-model="param.content.where.condition"
        @keyup.enter.native="listData(param.content.pageNumber = 1)"
        @clear="listData(param.content.pageNumber = 1)" clearable
      ></el-input>
      <el-table :data="list" size="mini" border>
        <el-table-column prop="sonum" label="订单号" width="150"></el-table-column>
        <!-- <el-table-column prop="type" label="订单类型" width="90"></el-table-column>
        <el-table-column prop="checkdate" label="审核日期" width="150"></el-table-column> -->
        <el-table-column prop="goodscode" label="产品编码" width="150"></el-table-column>
        <el-table-column prop="goodsname" label="产品名称" width="150"></el-table-column>
        <el-table-column prop="price" label="订单单价" width="90">
          <template slot-scope="scope">
            {{tool.formatAmount(scope.row.price,2)}}
          </template>
        </el-table-column>
        <el-table-column prop="num" label="本次开票数量" width="120"></el-table-column>
        <el-table-column prop="taxincludedamount" label="本次开票金额" width="120">
          <template slot-scope="scope">
            {{tool.formatAmount(scope.row.taxincludedamount,2)}}
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="备注" min-width="150"></el-table-column>
        <el-table-column label="操作" width="90" fixed="right">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="addRow(scope.row)">添 加</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="container" style="text-align:right">
        <el-pagination
          background
          small
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[20, 50, 100, 200]"
          layout="total,sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props:['sa_invoicebillid','status'],
  data () {
    return {
      dialogTableVisible:false,
      param:{
        "id": 2024052409322604,
        "content": {
          "pageNumber":1,
          "pageSize":20,
          "sa_invoicebillid":'',
          "where":{
            "condition":""
          }
        }
      },
      list:[],
      total:0,
      currentPage:0
    }
  },
  methods:{
    onShow () {
      this.dialogTableVisible = true
      this.listData()
    },
    async listData () {
      this.param.content.sa_invoicebillid = this.sa_invoicebillid
      const res = await this.$api.requested(this.param)
      this.list = res.data
      this.total = res.total
      this.currentPage = res.pageNumber
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.param.content.pageSize = val
      this.listData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.param.content.pageNumber = val
      this.listData()
    },
    // 添加明细
    async addRow (row) {
      row.invoiceamount = row.taxincludedamount
      row.invoiceaqty = row.num
      const res = await this.$api.requested({
        "id": "20230228090903",
        "content": {
          "sa_invoicebillid":this.sa_invoicebillid,
          "sa_invoiceapplyid":this.$route.query.id,
          "iteminfos":[{
            "sa_invoicebill_itemid": row.sa_invoicebill_itemid,
            "sa_invoiceapply_orderid":row.sa_invoiceapply_orderid,
            "itemno": row.goodscode,
            "itemname": row.goodsname,
            "spec": row.spectype,
            "price": row.price,
            "invoiceaqty": row.num,
            "invoiceamount": row.taxincludedamount,
            "taxrate":row.taxrate,
            "unit":row.unit
          }]
        }
      })
      this.tool.showMessage(res,()=>{
        this.listData()
        this.$emit('onSuccess')
      })
    }
  },
  mounted () {
    this.listData()
  }
}

</script>
<style>
</style>
