<template>
  <div>
    <el-button type="primary" size="small" :disabled="disabled" @click="addBtn">添 加</el-button>
    <el-drawer append-to-body size="80%" :with-header="false" :visible.sync="drawer">
      <div class="container">
        <p class="normal-title normal-margin">产品清单</p>
        <div style="display:flex;justify-content:space-between;align-items:center">
          <el-input size="small" style="width:200px;margin-bottom:10px" v-model="params.content.where.condition" placeholder="输入搜索内容" @clear="getProductList(params.content.pageNumber = 1)" @keyup.native.enter="getProductList(params.content.pageNumber = 1)" clearable></el-input>
          <div>
            <el-button type="primary" size="small" :disabled="selectProductLength == 0" @click="onSumit($refs.table.tableSelectData)">批 量 添 加</el-button>
          </div>
        </div>
        <Table @selectArr="selectArr" ref="table" v-model="itemno" :layout="tablecols" :data="productList" :custom="true" height="400px"  @upDateData="upDateData" @selection="selectArr" @onSelect="addProduct">
        </Table>
        <div style="display:flex;justify-content:space-between;align-items:center;margin-top:10px">
          <span>已添加产品数:{{addProductLength}}</span>
          <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="params.content.pageNumber"
              :page-sizes="[20, 50, 100, 200]"
              :page-size="100"
              layout="total,sizes, prev, pager, next, jumper"
              :total="total">
          </el-pagination>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
// import Table from '@/HDrpManagement/ProductGroupMag/modules/table'
import Table from '@/components/productTable/index'
import previewImage from '@/components/previewImage/index'
export default {
  props:['disabled'],
  name: '',
  components:{Table,previewImage},
  data() {
    return {
      drawer:false,
      form:{
        qty:'',
        discountrate:'',
        price:'',

      },
      selectProductLength:0,
      addProductLength:0,
      tablecols:[],
      productList:[],
      itemno:'',
      total:0,
      params: {
        "id": 20221124093602,
        "content": {
          "sa_contractid": '',
          "pageNumber": 1,
          "pageSize": 100,
          "where": {
              "condition": "",
              "istool":0//是否是工具
          }
        },
      }
    };
  },
  computed:{
  },
  watch:{
  },
  updated () {
    setTimeout(() => {
      this.addProductLength = this.$parent.total
    },300)
  },
  created () {
    this.tablecols = this.tool.tabelCol(this.$route.name)['productTable'].tablecols
  },
  methods: {
    addBtn () {
      this.drawer = true
      this.getProductList()
    },
    async getProductList () {
      this.params.content.sa_contractid = this.$route.query.id
      let res = await this.$api.requested(this.params)
      this.productList = res.data
      this.total = res.total
      console.log(this.productList);
      
    },
    async addProduct (data) {
      let res = await this.$api.requested({
        "id": 20221123164402,
        "content": {
            "sa_contractid": this.$route.query.id,
            "items": [
              {
                "itemid": data.itemid,
                "price": data.marketprice,
                "qty": 1,
                'marketprice':data.marketprice,
                type:'折扣系数'
              }
            ]
        },
      })
      this.tool.showMessage(res,() => {
        this.getProductList()
        this.$emit('onSuccess')
      })
    },
    async onSumit (data) {
      let item = data.map(item => {
        return {
          "itemid": item.itemid,
          "price": item.marketprice,
          "qty": 1,
          'marketprice':item.marketprice,
          type:'折扣系数'
        }
      })
      let res = await this.$api.requested({
        "id": 20221123164402,
        "content": {
            "sa_contractid": this.$route.query.id,
            "items": item
        },
      })
      this.tool.showMessage(res,() => {
        this.$emit('onSuccess')
        data = []
        this.getProductList()

      })
    },
    selectArr (data) {
      this.selectProductLength = data.length
    },
    upDateData (data) {
      this.selectProductLength = data.length
      console.log(data);
      
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.params.content.pageSize = val
      this.getProductList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.content.pageNumber = val
      this.getProductList()
    },
  },
};
</script>

<style scoped>
</style>
