<template>
  <div class="inline-16">
    <el-button type="primary" :disabled="!isEdit()" size="mini" @click="productBtn">添 加</el-button>
    <el-drawer
        title="添加商品"
        :visible.sync="drawer"
        size="70%"
        direction="rtl"
        append-to-body
        @close="drawer = false"
        :show-close="false">
      <div class="drawer__panel">
        <div style="display:flex;align-items:center">
          <el-input
              placeholder="请输入搜索内容"
              suffix-icon="el-icon-search"
              v-model="params.content.where.condition"
              style="width:200px"
              size="mini"
              class="input-with-select inline-16"
              @keyup.native.enter="listData(params.content.pageNumber=1)"
              @clear="listData(params.content.where.condition='')"
              clearable>
          </el-input>
        </div>
        <div style="margin-top: 15px">
          <selectTable v-if="drawer" ref="table" idName="itemid" @selectChange="selectChange" @upDateData="selectChange" :layout="tablecols" :data="list" :custom="true" height="500px">
            <template v-slot:customcol="scope">
              <div v-if="scope.column.columnname == 'attinfos' && scope.column.data.attinfos[0]">
                <previewImage style="height:38px;width:38px" :image="scope.column.data.attinfos[0]" :list="scope.column.data.attinfos" :deletebtn="false"></previewImage>
              </div>
              <div v-else-if="scope.column.columnname == 'itemclass'">
                  <span v-for="(item,index) in scope.column.data[scope.column.columnname]" :key="index">
                    {{item?item.itemclassname+',':'--'}}
                  </span>
                  <!-- <el-tag v-if="item" style="margin-bottom:10px">{{item.itemclassname}}</el-tag> -->
              </div>
              <div v-else-if="scope.column.columnname == 'spec'">
                {{scope.column.data.model}}/{{scope.column.data.spec}}
              </div>
              <div v-else-if="scope.column.columnname == 'brand'">
                <p v-for="(item,index) in scope.column.data[scope.column.columnname]" :key="index">
                  <span style="float: left" v-if="index === scope.column.data[scope.column.columnname].length -1">{{item?item.brandname+'':'--'}}</span>
                  <span style="float: left" v-else>{{item?item.brandname+',':'--'}}</span>
                </p>
              </div>
              <div v-else>{{scope.column.data[scope.column.columnname]}}</div>
              <div v-if="!scope.column.data[scope.column.columnname] && scope.column.columnname != 'operation' && scope.column.data[scope.column.columnname] !== 0">--</div>
            </template>
          </selectTable>
        </div>
        <div style="margin-top:16px;text-align:right">
          <el-pagination
              background
              small
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="params.content.pageNumber"
              :page-size="params.content.pageSize"
              layout="total, prev, pager, next, jumper"
              :total="total">
          </el-pagination>
        </div>
        <div class="fixed__btn__panel">
          <el-button size="small" @click="drawer = false" class="normal-btn-width">取 消</el-button>
          <el-button size="small" :disabled="!selectProduct.length" type="primary" @click="submit" class="normal-btn-width">确 定</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import selectTable from '@/components/selectTable/index'
export default {
  name: '',
  props:['data'],
  inject:['isEdit'],
  components:{selectTable},
  data() {
    return {
      drawer:false,
      tableHieght:"calc(100vh - 380px)",
      tablecols:[],
      list:[],
      total:0,
      params:{
        "id": 20230102144603,
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "sa_promotion_itemgroupid":'',
            "where": {
              "condition": ''
            }
        }
      },
      selectProduct:[]
    };
  },
  computed:{
  },
  watch:{
  },
  methods: {
    async listData(){
      this.params.content.sa_promotion_itemgroupid = this.data.sa_promotion_itemgroupid
      const res = await this.$api.requested(this.params)
      this.list = res.data
      this.total = res.total
      console.log(this.list)
    },
    async submit () {
      let arr = this.$refs.table.allArr.map(item => {
        return {
          "sa_promotion_itemsid":0,
          "itemid":item.itemid,
          "islimit":0,
          "groupqty":0,
          "orderaddqty":0,
          "orderminqty":0
        }
      })
      console.log('触发');
      
      if (arr.length) {
        console.log(arr);
        
        let res = await this.$api.requested({
          "id":20230102142403,
          "content": {
            sa_promotionid:this.$route.query.id,
            sa_promotion_itemgroupid:this.data.sa_promotion_itemgroupid,
            iteminfos: arr
          }
        })
        this.tool.showMessage(res,() => {
          this.drawer = false
          this.$emit('onSuccess')
        })
      }
    },
    selectChange (data) {
      this.selectProduct = data
    },
    productBtn () {
      this.drawer = true
      this.listData()
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.params.content.pageSize = val
      this.listData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.content.pageNumber = val
      this.listData()
    },
  },
  created () {
    this.tablecols = this.tool.tabelCol(this.$route.name).modelProductTable.tablecols
  }
};
</script>

<style scoped>

</style>
