<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>
          <el-button type="primary" size="small" :disabled="selectProductLength == 0" @click="onSumit">批 量 添 加</el-button>
        </div>
          <Table @selectArr="selectArr" ref="table" id="itemclassid" v-model="itemno" :layout="tablecols" :data="productList" :custom="true" height="500px"  @upDateData="upDateData">
            <template v-slot:customcol="scope">
              <div v-if="scope.column.columnname == 'attinfos'">
                <previewImage 
                  v-if="scope.column.data.attinfos[0]" 
                  style="width:38px;height:38px;margin:0px" 
                  class="image" 
                  :image="scope.column.data.attinfos[0]" 
                  :list="scope.column.data.attinfos" 
                  :deletebtn="false"
                ></previewImage>
              </div>
              <div v-else-if="scope.column.columnname == 'brand'">
                {{scope.column.data[scope.column.columnname][0] ? scope.column.data[scope.column.columnname][0].brandname : '--'}}
              </div>
              <div v-else>{{scope.column.data[scope.column.columnname]}}</div>
              <span v-if="!scope.column.data[scope.column.columnname] && scope.column.data[scope.column.columnname] !== 0 && scope.column.columnname != 'operation'">--</span>
            </template>
            <template v-slot:opreation="scope">
              <el-button type="text" size="mini" @click="addProduct(scope.data)">添 加</el-button>
            </template>
          </Table>
           <div style="display:flex;justify-content:space-between;align-items:center;margin-top:10px">
<!--            <el-pagination
                style="display:inline-block"
                background
                small
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="params.content.pageNumber"
                :page-sizes="[20, 50, 100, 200]"
                layout="total, prev, pager, next, jumper"
                :total="total">
            </el-pagination>-->
          </div>
      </div>
      
      <div class="fixed__btn__panel">
        <el-button size="small" @click="drawer = false">取 消</el-button>
        <el-button type="primary" size="small" :disabled="selectProductLength == 0" @click="onSumit">批 量 添 加</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import Table from './table'
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": 20221124140102,
        "content": {
            "sa_contractid": '',
            "pageNumber": 1,
            "pageSize": 999,
            "where": {
                "condition": ""
            }
        },
      }
    };
  },
  computed:{
  },
  watch:{
  },
  updated () {
    setTimeout(() => {
      this.addProductLength = this.$parent.list.length
    },300)
  },
  created () {
    this.tablecols = this.tool.tabelCol(this.$route.name)['productClassTable'].tablecols
  },
  methods: {
    selectArr (data) {
      console.log(data,data.length);
      
      this.selectProductLength = data.length
    },
    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
      this.params.content.pageNumber = res.pageNumber
      console.log(this)
    },
    createTreeData (array) {
      var that = this
      let arr = []
      function convertToElementTree(node) {
        // 新节点
        var elNode = {
          num:node['num'],
          itemclassfullname:node['itemclassfullname'],
          itemclassname: node["itemclassname"],
          parentid:node['parentid'],
          itemclassid:node['itemclassid'],
          itemclassnum:node['itemclassnum'],
          sa_brandid:that.data.sa_brandid,
          ishide:node['ishide'],
          isdeep:node['isdeep'],
          istool:node['istool'],
          children: []
        }
          
        if (node.subdep && node.subdep.length > 0) {
          // 如果存在子节点
          for (var index = 0; index < node.subdep.length; index++) {
          // 遍历子节点, 把每个子节点看做一颗独立的树, 传入递归构造子树, 并把结果放回到新node的children中
          elNode.children.push(convertToElementTree(node.subdep[index]));
          }
        }
        return elNode;
      }
      array.forEach((element) => {
        arr.push(convertToElementTree(element))
      });
      return arr
    },
    async addProduct (data) {
      let res = await this.$api.requested({
        "id": 20221124110202,
        "content": {
            "sa_contractid": this.$route.query.id,
            "items": [
              {
                "sa_contract_itemsaleclassid": 0,
                "itemclassid": data.itemclassid,
                "discountrate": '1'
              }
            ]
        },
      })
      this.tool.showMessage(res,() => {
        this.getProductList()
        this.$emit('onSuccess')
      })
    },
    async onSumit () {
      let item = this.$refs.table.allArr.map(item => {
        return {
          "sa_contract_itemsaleclassid": 0,
          "itemclassid": item.itemclassid,
          "discountrate": '1'
        }
      })
      
      let res = await this.$api.requested({
        "id": 20221124110202,
        "content": {
            "sa_contractid": this.$route.query.id,
            "items": item
        },
      })
      this.tool.showMessage(res,() => {
        this.drawer = false
        this.$emit('onSuccess')
        this.$refs.table.allArr = []

      })
    },
    upDateData (data) {
      this.selectProductLength = data.length
    },
    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>
