<template>
  <div>
    <el-button size="small" type="primary"  @click="onShow">新增关联商品</el-button>
    <el-dialog
        :visible.sync="drawer"
        width="1100px"
        @close="onClose"
        append-to-body
    >
      <search @searchActive="searchActive" :placeholder="placeholder"></search>
      <div>
        <tablelayout :layout="tablecols" :data="productsList" :custom="true" :checkbox="true" height="calc(100vh - 452px)" @checkboxCallBack="checkboxCallBack">
          <template v-slot:customcol="scope">
            <div v-if="scope.column.columnname === 'province'">
              <span>{{scope.column.data.province}} - {{scope.column.data.city}} - {{scope.column.data.county}}</span>
            </div>
            <div v-else-if="scope.column.columnname === 'isonsale'">
              <span>{{scope.column.data.isonsale ? '上架' : '下架'}}</span>
            </div>
            <p v-else-if="scope.column.columnname === 'status'">
              <span style="color:#52C41A" v-if="scope.column.data[[scope.column.columnname]] == '审核'">{{scope.column.data[[scope.column.columnname]]}}</span>
              <span style="color:#000000" v-else-if="scope.column.data[[scope.column.columnname]] == '新建'">{{scope.column.data[[scope.column.columnname]]}}</span>
            </p>
            <p v-else>{{scope.column.data[scope.column.columnname]}}</p>
          </template>

        </tablelayout>

      </div>
      <div>
        <div style="margin-top: 10px;float: left">已选{{selected}}个商品，共{{total}}个商品</div>
        <div  style="margin-top: 10px;text-align:right">
          <el-pagination
              background
              @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>
      </div>
      <div class="dialog-footer">
        <el-button size="small" @click="drawer = false" class="normal-btn-width">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit"  class="normal-btn-width btn-primary">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import tablelayout from '@/components/table/index2'
import search from './search'
export default {
  name: "add",
  props:["plm_technicalinfoid"],
  data(){
    return {
      drawer:false,
      add:false,
      placeholder:'商品名称',
      form:{
        itemid:''
      },
      rules:{
        itemid: [
          { required: true, message: '档案名称不可为空', trigger: 'blur' },
        ],
      },
      productsList:[],
      multipleSelection: [],
      tablecols:[],
      tableHieght:420,
      selected:0,
      total:0,
      currentPage:0,
      params:{
        "id": 20220923140602,
        "content": {
          "pageNumber":1,
          "pageSize":20,
          "where": {
            "condition": ""
          }
        }
      }
    }
  },
  components:{
    tablelayout,
    search
  },
  mounted() {

  },
  created() {
    this.tablecols = this.tool.tabelCol(this.$route.name).relationProductTable.tablecols
  },
  methods: {
    onShow(){
      this.drawer = true
      this.productsData()
    },
    onClose(){
      this.drawer = false
      this.form.itemid=''
    },
    async productsData(){
      const res = await this.$api.requested(this.params)
      console.log(res)
      this.productsList = res.data
      this.total = res.total
      this.currentPage = res.pageNumber
    },
    checkboxCallBack(val){
      console.log(val)
      this.multipleSelection = val;
      this.selected = val.length

    },
    async onSubmit(){
      console.log(this.multipleSelection)
      let arr = this.multipleSelection.map(e=>{
        return {
          plm_technicalinfo_itemid:0,
          itemid:e.itemid
        }
      })
      const res = await this.$api.requested({
        "id": "20220926102003",
        "version":1,
        "content": {
          "plm_technicalinfoid":this.plm_technicalinfoid,
          "iteminfos":arr
        }
      })
      this.tool.showMessage(res, () => {
        this.drawer = false
        this.$emit('addSuccess')
      })
    },
    searchActive(data) {
      this.params.content.where.condition = data
      this.productsData()
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.param.content.pageSize = val
      this.productsData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.param.content.pageNumber = val
      this.productsData()
    },
  }
}
</script>

<style scoped>

</style>