<template>
  <div>
    <el-button v-if="tool.checkAuth($route.name,'InvioceOrder')" :disabled="status !== '新建'" style="margin-bottom:10px" size="small" type="primary" @click="onShow">添加订单行</el-button>
    <el-dialog title="可添加订单" append-to-body :visible.sync="dialogTableVisible">
      <div class="flex-align-center">
        <el-input size="small"  
          style="width:200px;margin-bottom:16px"
          suffix-icon="el-icon-search" 
          v-model="param.content.where.condition" 
          placeholder="订单号、产品编号、产品名称" @keyup.enter.native="listData(param.content.pageNumber = 1)" @clear="listData(param.content.pageNumber = 1)" clearable></el-input>&nbsp;
      </div>
      <el-table :data="list" size="mini" border>
        <el-table-column prop="sonum" label="订单号" width="150"></el-table-column>
        <el-table-column prop="enterprisename" show-overflow-tooltip label="企业名称" width="150"></el-table-column>
        <el-table-column prop="itemno" show-overflow-tooltip label="产品编号" width="150"></el-table-column>
        <el-table-column prop="itemname" label="产品名称" width="150"></el-table-column>
        <el-table-column prop="spec" label="型号" width="150">
          <template slot-scope="scope">
            {{scope.row.model}}
          </template>
        </el-table-column>
        <el-table-column prop="qty" label="订单数量" width="90"></el-table-column>
        <!-- <el-table-column prop="qty" label="发货数量"></el-table-column> -->
        <el-table-column prop="writeoffamount" label="已核销金额" width="120"></el-table-column>
        <el-table-column prop="invoiceamount" label="已开票金额" width="120"></el-table-column>
        <el-table-column prop="remarks" show-overflow-tooltip label="备注" width="150" ></el-table-column>
        <el-table-column label="操作" width="90" fixed="right">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="addOrderLine(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:['sys_enterpriseid','status'],
  data () {
    return {
      dialogTableVisible:false,
      list:[],
      param:{
        "id": "20221217085103",
        "content": {
          "sys_enterpriseid":0,
          "sa_invoiceapplyid":0,
          "pageNumber":1,
          "pageSize":20,
          "where":{
            "condition":"",
            "sonum":"",
            "begindate":"",
            "enddate":""
          }
        }
      },
      list:[],
      total:0,
      currentPage:0
    }
  },
  methods:{
    onShow () {
      this.dialogTableVisible = true
      this.listData()
    },
    async listData () {
      this.param.content.sa_invoiceapplyid = this.$route.query.id
      this.param.content.sys_enterpriseid = this.sys_enterpriseid
      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 addOrderLine (row) {
      const res = await this.$api.requested({
        "id": "20221217091203",
        "content": {
            "sa_invoiceapplyid": this.$route.query.id, 
            "iteminfos": [
              {
                "sa_invoiceapply_orderid": 0, 
                "saorderid": row.sa_orderid, 
                "sa_orderitemsid": row.sa_orderitemsid, 
                "price": row.price, 
                "invoiceamount": 0, 
                "invoiceaqty": 0
              }
            ]
        }
      })
      this.tool.showMessage(res,()=>{
        this.$emit('onSuccess')
      })
    }
  },
  mounted () {
  }
}

</script>
<style>
</style>