<template>
  <div>
<!--    <add-btn ref="add" :rowData="rowData" v-if="tool.checkAuth($route.name,'insert')" :disabled="disabled" @onSuccess="getDetail" style="margin-bottom:16px" />-->
    <addProduct class="inline-16"     ref="addProduct" v-if="tool.checkAuth($route.name,'insert') && !disabled"
                :params="paramsAdd" :tablecolsAdd="tablecolsAdd" title="添 加"
                @addSuccess="addProducts" @addProduct="addProduct" @uploadData="uploadData" @closeDrawer="getDetail" :querySa_brandid="rowData.sa_brandid"
    ></addProduct>
    <uploadAllData
        class="inline-16"
        v-if="tool.checkAuth($route.name,'insert') && !disabled"
        :total="total"
        @onSuccess="getDetail()"
        @handlePullApi="handlePullApi"
        @handleDelApi="handleDelApi"
        idName="sa_itemgroupmxid"
        type="del"
    ></uploadAllData>
    <!-- <tableLayout :layout="tablecols" :data="list" :opwidth="200" :custom="true" height="40vh" fixedName="operation">
      <template v-slot:customcol="scope">
          <span v-if="scope.column.data[scope.column.columnname]">{{scope.column.data[scope.column.columnname]}}</span>
          <span v-else-if="scope.column.columnname === 'itemclassname'">
            <span v-show="item.itemclassname" v-for="item in scope.column.data.itemclass" :key="item.index">{{item.itemclassname}},</span>
          </span>
          <span v-else-if="scope.column.columnname != 'operation'">--</span>
      </template>
      <template v-slot:opreation="scope">
        <div >
          <Del type="detail" v-if="tool.checkAuth($route.name,'delete')" :id="rowData.sa_itemgroupid" @deleteSuccess="getDetail" :detailId="scope.data.sa_itemgroupmxid"/>
        </div>
      </template>
    </tableLayout> -->
    <Table type="edit" :data="list" @deleteProduct="deleteProduct"></Table>
    <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>
</template>

<script>
import AddBtn from './addGroupDetailBtn'
import editBtn from './editGroupDetailBtn'
import Table from './table.vue'
import Del from './delete'
import addProduct from '@/template/addProduct/index'
import uploadAllData from '@/components/uploadAllData/index'

export default {
  data () {
    return {
      list:[],
      form: {
        product:[]
      },
      tablecols:[],
      rules: {
        product: [
          { required: true, message: '请选择商品', trigger: 'blur' },
        ],
      },
      params: {
        "id": "20220923101603",
        "version":1,
        "content": {
          "sa_itemgroupid":this.$route.query.id,
          "pageNumber":1,
          "pageSize":20
        }
      },
      paramsAdd:{
        "id": 20220923112503,
        "content": {
          "nocache":true,
          "pageNumber": 1,
          "pageSize": 100,
          "sa_itemgroupid":this.$route.query.id,
          "where": {
            "condition": ''
          }
        }
      },
      tablecolsAdd:[],
      total:0
    }
  },
  props:['rowData','disabled'],
  components:{AddBtn,Del,Table,addProduct,uploadAllData},

  watch: {
  },
  created() {
    this.getDetail()
    this.tablecolsAdd = this.tool.tabelCol(this.$route.name).addProduct.tablecols
  },
  methods: {
    async getDetail() {
      this.params.content.sa_itemgroupid = this.$route.query.id
      let res = await this.$api.requested(this.params)
      this.list = res.data
      this.total = res.total
      console.log(res,'列表数据',this.params);
      
    },
    handleClose(tag) {
      this.form.tag.splice(this.form.tag.indexOf(tag), 1);
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.params.content.pageSize = val
      this.getDetail()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.content.pageNumber = val
      this.getDetail()
    },
    async deleteProduct (row) {
      const res = await this.$api.requested({
        "id": "20220923105103",
        "content": {
          "sa_itemgroupid": this.$route.query.id,
          "sa_itemgroupmxids": [row.sa_itemgroupmxid]
        }
      })
      this.tool.showMessage(res,()=>{
        this.getDetail()
      })
    },
    /*批量添加商品*/
    async addProducts(data){
      let items = data.map(item => {
        return {
          sa_itemgroupmxid:0,
          itemno:item.itemno,
          itemid:item.itemid
        }
      })
      const res = await this.$api.requested({
        "id": 20220923110303,
        "content": {
          "sa_itemgroupid": this.$route.query.id,
          "itemclassinfos": items
        },
      })
      this.tool.showMessage(res,()=>{
        this.getDetail()
        this.$refs.addProduct.listData()
      })
    },
    /*单个添加商品*/
    async addProduct(data){
      let res = await this.$api.requested({
        "id": 20220923110303,
        "content": {
          "sa_itemgroupid": this.$route.query.id,
          "itemclassinfos": [
            {
              sa_itemgroupmxid:0,
              itemno:data.itemno,
              itemid:data.itemid
            }
          ]
        },
      })
      this.tool.showMessage(res,() => {
        this.getDetail()
        this.$refs.addProduct.listData()
      })
    },
    /*一键添加*/
    uploadData(uploadApi,data){
      uploadApi.id = 20220923110303
      uploadApi.content = {
        "sa_itemgroupid": this.$route.query.id,
        "itemclassinfos": data.map(e=>{
          return {
            sa_itemgroupmxid:0,
            itemno:e.itemno,
            itemid:e.itemid
          }
        })
      }
      this.getDetail()
      this.$refs.addProduct.listData()
    },
    handlePullApi (pullApi) {
      pullApi.content = {
        "sa_itemgroupid": 0, //订单ID
        "pageNumber": 1,
        "pageSize": 20,
        "where": {
          "condition": ""
        }
      }
      pullApi.id = 20220923101603
      pullApi.content.sa_itemgroupid = this.$route.query.id
    },
    handleDelApi (delApi,data) {
      delApi.id = 20220923105103
      delApi.content = {
        "sa_itemgroupid": this.$route.query.id,
        "sa_itemgroupmxids": data
      }
    },
  }
}

</script>
<style scoped>
/deep/.el-dialog__body {
  padding-top: 10px !important;
}
/deep/.dialog-footer {
  margin-top: 10px !important;
}
/deep/.el-form-item__content {
  width: calc(100% - 100px) !important;
}
/deep/.el-select {
  width: 100% !important;
}
/deep/.el-form-item {
  width: 100% !important;
}
/deep/.el-input{
  width: 100% !important;
}
/deep/.el-pagination {
  text-align: right;
  margin-top: 10px;
  margin-right: 40px;
}
</style>