<template>
  <div>
    <el-button v-if="tool.checkAuth($route.name,'editBlueInvioce')" :disabled="status != '提交' && status != '新建'" size="small" type="primary" @click="onShow">添加开票明细行</el-button>
    <el-dialog title="选择开票明细行" :visible.sync="dialogTableVisible" append-to-body>
      <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="itemno" label="产品编码" width="150"></el-table-column>
        <el-table-column prop="itemname" 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="invoiceaqty" label="本次开票数量" width="120"></el-table-column>
        <el-table-column prop="invoiceamount" label="本次开票金额" width="120">
          <template slot-scope="scope">
            {{tool.formatAmount(scope.row.invoiceamount,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": 20221228093803,
        "content": {
          "pageNumber":1,
          "pageSize":20,
          "sa_invoiceapplyid":this.$route.query.id,
          "where":{
            "condition":""
          }
        }
      },
      list:[],
      total:0,
      currentPage:0
    }
  },
  methods:{
    onShow () {
      this.dialogTableVisible = true
      this.listData()
    },
    async listData () {
      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.taxrate = 0.13
      row.sa_invoicebill_itemid = 0
      const res = await this.$api.requested({
        "id": "20221227152903",
        "content": {
          "sa_invoicebillid":this.sa_invoicebillid,
          "sa_invoiceapplyid":this.$route.query.id,
          "iteminfos":[row]
        }
      })
      this.tool.showMessage(res,()=>{
        this.listData()
        this.$emit('onSuccess')
      })
    } 
  },
  mounted () {
    this.listData()
  }
}

</script>
<style>
</style>