<template>
  <div>
    <el-button size="small"  type="primary" icon="el-icon-plus" @click="onShow">自项目产品添加</el-button>
    <el-drawer
        title="自项目产品添加"
        :visible.sync="dialogProductVisible"
        append-to-body
        size="80%"
        :show-close="false"
        direction="rtl"
        @close="onColes">
      <div class="drawer__panel">
        <div class="flex-align-center flex-between ">
          <slot name="operation"></slot>
          <div class="flex-align-center">
            <el-input size="small"  suffix-icon="el-icon-search" v-model="params.content.where.condition" placeholder="产品名称,编号" @keyup.enter.native="productData(params.content.pageNumber = 1)" @clear="productData(params.content.pageNumber = 1)" clearable></el-input>&nbsp;
          </div>
          <div style="margin-top:-10px;margin-bottom: 10px;float: right">
            <el-button style="float: right" type="primary" size="mini" @click="batchSelect" :disabled="tableSelectData.length === 0 ">批量添加</el-button>
          </div>
        </div>
        <div class="container">
          <selectClass @brandChange="brandChange" @onClassChange="onClassChange" @Search="Search" @clearSearch="clearSearch" @clickStandard="clickStandard" @clickField="clickField" :default="true"></selectClass>
        </div>
        <div class="produtMag-panel">
          <el-table
              ref="multipleTable"
              :data="list"
              style="width: 100%"
              :header-cell-style="{height:'50px',color:'#606266',fontWeight:'400'}"
              :cell-style="{height:'50px',color:'#666666',fontWeight:'400'}"
              @selection-change="selectionChange">
            <el-table-column
                type="selection"
                width="42"
                fixed>
            </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>
              </template>
            </el-table-column>
            <el-table-column
                prop="itemno"
                label="产品编号"
                width="180">
            </el-table-column>
            <el-table-column
                prop="itemname"
                label="产品名称"
                width="180">
            </el-table-column>
            <el-table-column
                label="型号"
                width="180">
              <template slot-scope="scope">
                <p><span>{{scope.row.model}}</span></p>
              </template>
            </el-table-column>
            <el-table-column
                label="规格"
                width="180">
              <template slot-scope="scope">
                <p><span>{{scope.row.spec}}</span></p>
              </template>
            </el-table-column>
            <el-table-column
                prop="caliber"
                label="口径"
                width="180">
            </el-table-column>
            <el-table-column
                prop="pressure"
                label="压力"
                width="180">
            </el-table-column>
            <el-table-column
                prop="material"
                label="材质"
                width="180">
            </el-table-column>
            <el-table-column
                prop="brand"
                label="品牌"
                width="180">
              <template slot-scope="scope">
                <p v-for="(item,index) in scope.row.brand">
                  <span style="float: left" v-if="index === scope.row.brand.length -1">{{item?item.brandname+'':'--'}}</span>
                  <span style="float: left" v-else>{{item?item.brandname+',':'--'}}</span>
                </p>
              </template>
            </el-table-column>
            <el-table-column
                prop="itemclass"
                label="类别"
                width="0">
              <template slot-scope="scope">
                <p v-for="(item,index) in scope.row.itemclass">
                  <span style="float: left" v-if="index === scope.row.itemclass.length -1">{{item?item.itemclassname+'':'--'}}</span>
                  <span style="float: left" v-else>{{item?item.itemclassname+',':'--'}}</span>
                </p>
              </template>
            </el-table-column>
            <el-table-column
                prop="unitid"
                label="计量单位"
                width="100">
              <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="marketprice"
                label="牌价(元)"
                fixed="right"
                width="100">
              <template slot-scope="scope">
                  <span >{{scope.row.marketprice?scope.row.marketprice:'--'}}</span>
              </template>
            </el-table-column>
            <el-table-column
                prop="totalPrice"
                fixed="right"
                label="操作"
                width="80">
              <template slot-scope="scope">
                <el-button type="text" @click="onSelect(scope.row)">添 加</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div>
            <div style="float: left">已选:{{selectTotal}}个商品</div>
            <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>
      </div>
      <div class="fixed__btn__panel">
        <el-button size="small" @click="onColes" class="normal-btn-width">取 消</el-button>
<!--        <el-button size="small" type="primary" @click="onSubmit"  class="normal-btn-width">确 定</el-button>-->
      </div>
    </el-drawer>
  </div>
</template>

<script>
import uploadFile from "@/components/upload/hw_obs_upload";
import previewImage from "@/components/previewImage";
import selectClass from './Select'
export default {
  name: "productTable",
  props:["id"],
  components:{uploadFile, previewImage,selectClass},
  data(){
    return {
      dialogProductVisible:false,
      type:'',
      tablecols:[],
      list:[],
      total:0,
      currentPage:0,
      tableSelectData:[],
      selectData:[],
      selectTotal:0,
      sa_quotedpriceid:'',
      params:{
        "id": 20221020164903,
        "version":1,
        "content": {
          "sa_projectid":'',
          "sa_quotedpriceid":"",
          "pageNumber": 1,
          "pageSize": 20,
          "where": {
            "condition": "",
            "tradefield":"",
            "itemclassid":"",
            "sa_brandid":"",
            "standards":""
          }

        }
      },
      timer:0
    }
  },
  methods:{
    onShow(){
     /* this.dialogProjectVisible = true*/
      this.$emit('checkFormProject','project')
      /*this.productData()*/

    },
    onColes(){
      this.dialogProductVisible = false
    },
    async onSubmit(){
      console.log(this.selectData,"添加选择的商品")
      const res = await this.$api.requested({
        "id": 20221021095403,
        "content": {
          "sa_quotedpriceid":this.sa_quotedpriceid,     //sat_notice_classid<=0时 为新增
          "items":this.selectData
        }
      })
      this.tool.showMessage(res,()=>{
        /*this.dialogProductVisible = false*/
        this.$emit('productAdd')
        this.queryProduct(this.sa_quotedpriceid,this.type)
        this.selectListData()
      })
    },
    batchSelect(){
      this.debounce(this.onSubmit,300)()
    },
    onSelect(val){
      console.log(val,"选择的商品")
      this.tableSelectData = []
      this.tableSelectData[0] = val
      let obj = this.tableSelectData.map(e=>{
        return {
          sa_quotedprice_itemsid:'0',
          itemid:e.itemid,
          oldprice:e.marketprice,
          /*price:(e.marketprice * 100)/100,*/
          price:e.price,
          discountrate:e.discountrate,
          qty:e.qty
        }
      })
      this.selectData = obj
      /*this.$nextTick(()=>{
        this.onSubmit()
      })*/
      this.debounce(this.onSubmit,300)()
    },
    debounce (fn, wait) {
      let that = this
      return function () {
        if (that.timer !== null) clearTimeout(that.timer)
        that.timer = setTimeout(fn, wait)
      }
    },
    /*获取产品信息*/
    queryProduct(id,type){
      this.dialogProductVisible = true
      this.params.content.sa_projectid = this.id
      this.type = type
      this.sa_quotedpriceid = id
      this.productData()
    },
    /*产品配置信息*/
    async productData(){
      this.params.content.sa_quotedpriceid = this.sa_quotedpriceid
      const res = await this.$api.requested(this.params)
      this.list = res.data
      this.total = res.total
      this.$nextTick(()=>{
        if (this.type === '项目报价'){
          for (let i=0;i<this.list.length;i++){
            this.$refs.multipleTable.toggleRowSelection(this.list[i],true)
          }
        }
      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.param.content.pageSize = val
      this.productData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.param.content.pageNumber = val
      this.productData()
    },
    onSuccess(){
      this.productData()
    },
    clearData(){
      this.search = ""
      this.param.content.where.condition = this.search
      this.productData()
    },
    queryClick(){
      this.param.content.where.condition = this.search
      this.productData()
    },
    selectionChange(val){
      this.tableSelectData = []
      this.tableSelectData = val
      if (this.type === '项目报价'){
        let obj = this.tableSelectData.map(e=>{
          return {
            sa_quotedprice_itemsid:'0',
            itemid:e.itemid,
            oldprice:e.marketprice,
            /*price:(e.marketprice * 100)/100,*/
            price:e.price,
            discountrate:e.discountrate,
            qty:e.qty
          }
        })
        this.selectData = obj
      }else {
        let obj = this.tableSelectData.map(e=>{
          return {
            sa_quotedprice_itemsid:'0',
            itemid:e.itemid,
            oldprice:e.marketprice,
            /*price:(e.marketprice * 100)/100,*/
            price:e.price,
            discountrate:e.discountrate,
            qty:e.qty
          }
        })
        this.selectData = obj
      }
    },
    /*已选产品数据*/
    async selectListData(){
      const res = await this.$api.requested({
        "id": 20221021095503,
        "content": {
          "sa_quotedpriceid":this.sa_quotedpriceid     //sat_notice_classid<=0时 为新增
        }
      })
      console.log(res,"已选产品123456")
      this.selectTotal = res.total
      console.log(res.total,"已选产品数量")
    },

    brandChange(id) {
      this.brandId = id
      this.queryProduct(this.sa_quotedpriceid,this.type)
    },
    onClassChange (n) {
      this.params.content.where.itemclassid = n.itemclassid
      this.queryProduct(this.sa_quotedpriceid,this.type)
    },
    Search(data) {
      this.params.content.where.condition = data
      this.params.content.pageNumber = 1
      this.queryProduct(this.sa_quotedpriceid,this.type)
    },
    clearSearch() {
      this.params.content.where.condition = ''
      this.params.content.pageNumber = 1
      this.queryProduct(this.sa_quotedpriceid,this.type)
    },
    clickField (item) {
      console.log(item,'clickField')
      this.params.content.where.tradefield = item.value
      this.queryProduct(this.sa_quotedpriceid,this.type)
    },
    clickStandard (item){
      this.params.content.where.standards = item.value
      this.queryProduct(this.sa_quotedpriceid,this.type)
    }
  },
  mounted() {
  }
}
</script>

<style scoped>
.image {
  width:38px;height:38px;margin:0px auto;
}
</style>