<template>
<div class="container"> 
  <div class="flex-align-center flex-between normal-margin">
    <slot name="operation"></slot>
    <div class="flex-align-center">
      <el-input size="small" v-model="params.content.where.condition" placeholder="产品名称,编号" @keyup.enter.native="listData(params.content.pageNumber = 1)" @clear="listData(params.content.pageNumber = 1)" clearable></el-input>&nbsp;
      <slot name="add"></slot>
    </div>
  </div>
  <div class="produtMag-panel">
    <el-table
      ref="multipleTable"
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{height:'50px',color:'#768093',fontWeight:'100'}"
      :cell-style="{height:'50px',color:'#768093',fontWeight:'200'}"
      @selection-change="selectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        align="center"
        prop="isonsale"
        label="上架状态"
        width="80">
        <template slot-scope="scope">
          <slot name="onsale" :data="scope.row"></slot>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="产品图"
        width="80">
        <template slot-scope="scope">
          <div v-if="scope.row.attinfos[0]">
            <previewImage class="image" :image="scope.row.attinfos[0]" :list="scope.row.attinfos" :deletebtn="false"></previewImage>
          </div>
          <uploadFile  v-else :folderid="folderid" accept=".JPG,.PNG,.jpg,.png" btntype="smallimage" :bindData="{ownertable:'plm_item',ownerid:scope.row.itemid,usetype:'default'}" @onSuccess="listData"></uploadFile>
        </template>
      </el-table-column>
      <el-table-column
        prop="itemname"
        label="产品名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="itemno"
        label="产品编号"
        width="180">
      </el-table-column>
      <el-table-column
        label="型号/规格">
        <template slot-scope="scope">
          <p><span>{{scope.row.model}}</span>&nbsp;/&nbsp;<span>{{scope.row.spec}}</span></p>
        </template>
      </el-table-column>
      <el-table-column
        prop="unitid"
        label="起订量/增量"
        width="150">
        <template slot-scope="scope">
          <p><span>{{scope.row.orderminqty}}</span>&nbsp;/&nbsp;<span>{{scope.row.orderaddqty}}</span></p>
        </template>
      </el-table-column>
      <el-table-column
        prop="unitid"
        label="计量单位"
        width="90">
        <template slot-scope="scope">
          <el-tag size="mini" type="info" effect="plain">{{scope.row.unitname}}/{{scope.row.axunitname}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        width="90">
        <template slot-scope="scope">
          <span :style="scope.row.status === '新建'?{color:'#52C41A'}:{color:'red'}">{{scope.row.status}}</span>
        </template>
      </el-table-column>
      <el-table-column width="250">
        <template slot-scope="scope">
          <slot name="edit" :data="scope.row"></slot>
          <slot name="del" :data="scope.row"></slot>
          <el-dropdown size="small">
            <el-button type="primary" size="small">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <slot name="setclass" :data="scope.row"></slot>
              <slot name="setimage" :data="scope.row"></slot>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top:16px;text-align:right">
      <el-pagination
        background
        small
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="params.content.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</div>
</template>

<script>
import uploadFile from '@/components/upload/hw_obs_upload'
import previewImage from '@/components/previewImage/index'

export default {
  data () {
    return {
      folderid:JSON.parse(sessionStorage.getItem('folderid')).appfolderid,
      params:{
        "id": 20220923140602,
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "where": {
            "condition": ''
          }
        }
      },
      tableSelectData:[],
      tableData: [],
      total:0,
      currentPage:0
    }
  },
  components:{
    uploadFile,
    previewImage
  },
  methods:{
    async listData () {
      const res = await this.$api.requested(this.params)
      this.tableData = res.data
      this.total = res.total
      this.currentPage = res.pageNumber
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.params.content.pageSize = val
      this.listData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.content.pageNumber = val
      this.listData()
    },
    selectionChange (val) {
      this.tableSelectData = val
    },
    // 根据状态批量选择数据
    selection (type) {
      let arr = []
      let soureData = []

      // 判断是否是主动勾选数据,设置数据源
      this.tableSelectData.length > 0 ? soureData = this.tableSelectData:soureData = this.tableData

      if (type === 'check') {
        arr = soureData.filter(e=>{
          return e.status === '新建'
        })
      } else {
        arr = soureData.filter(e=>{
          return e.status === '审核'
        })
      }
      // 若为主动勾选数据则不执行循环
      this.tableSelectData.length > 0 ?'':arr.forEach(row => {
        this.$refs.multipleTable.toggleRowSelection(row);
      });
      this.$emit('selectData',arr)
    },
    clearSelection () {
      this.$refs.multipleTable.clearSelection();
    }
  },
  mounted () {
    this.listData()
  }
}

</script>
<style>
</style>
<style scoped>
.produtMag-panel{
  /* margin: 30px; */
  padding:0 10px 10px 10px;
  background: #fff;
  border-radius:5px;
  overflow: hidden;
  border:1px solid rgb(0 0 0 / 5%)
  /* box-shadow: 0 5px 5px rgb(0 0 0 / 10%);
  transform: translate3d(0,-2px,0); */
}
.image {
  width:40px;height:40px;margin:0px auto;
}
</style>