<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">
            <div>
              <el-button style="float: right" type="primary" size="mini" @click="batchSelect" :disabled="tableSelectData.length === 0 ">批量添加</el-button>
            </div>
          </div>
        </div>
        <div class="container">
          <selectClass ref="classShow" @brandChange="brandChange" @onClassChange="onClassChange" @Search="Search" @clearSearch="clearSearch" @clickStandard="clickStandard" @clickField="clickField" :default="true"></selectClass>
        </div>
        <div class="produtMag-panel">
          <el-table
              ref="multipleTable"
              height="calc(100vh - 370px)"
              :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
                prop="erpitemno"
                label="品号"
                width="180">
            </el-table-column>
            <el-table-column
                prop="standards"
                label="标准"
                width="100">
            </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="material"
                label="材质"
                width="180">
              <template slot-scope="scope">
                <p><span>{{scope.row.material || '--'}}</span></p>
              </template>
            </el-table-column>
            <el-table-column
                prop="device"
                label="装置"
                width="180">
              <template slot-scope="scope">
                <p><span>{{scope.row.device || '--'}}</span></p>
              </template>
            </el-table-column>
            <el-table-column
                prop="prodline"
                label="产线"
                width="180">
              <template slot-scope="scope">
                <p><span>{{scope.row.prodline || '--'}}</span></p>
              </template>
            </el-table-column>
            <el-table-column
                prop="specalnote"
                label="特殊说明"
                width="180">
              <template slot-scope="scope">
                <p><span>{{scope.row.specalnote || '--'}}</span></p>
              </template>
            </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
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="params.content.pageNumber"
                  :page-sizes="[20, 50, 100, 200]"
                  :page-size="100"
                  layout="total,sizes, 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:["sa_projectid",'type'],
  components:{uploadFile, previewImage,selectClass},
  data(){
    return {
      dialogProductVisible:false,
      tablecols:[],
      list:[],
      total:0,
      currentPage:0,
      tableSelectData:[],
      selectData:[],
      selectTotal:0,
      params:{
        "id": 20221020164903,
        "content": {
          "sa_projectid":'',
          "sa_quotedpriceid":"",
          "pageNumber": 1,
          "pageSize": 100,
          "where": {
            "condition": "",
            "tradefield":"",
            "itemclassid":"",
            "sa_brandid":"",
            "standards":""
          }

        }
      },
      timer:0
    }
  },
  methods:{
    onShow(){
     /* this.dialogProjectVisible = true*/
      /*this.$emit('checkFormProject','project')*/
      /*this.productData()*/
      this.dialogProductVisible = true
      setTimeout(()=>{
        this.params.content.where.tradefield = this.$refs.classShow.field_act
      /*  this.params.content.where.standards = this.$refs.classShow.standard_act*/
        /* this.params.content.where.itemclassid = this.$refs.classShow.class_act*/
        this.params.content.where.sa_brandid = this.$refs.classShow.brand_act
        this.productData()
        this.selectListData()
      },1000)


    },
    onColes(){
      this.dialogProductVisible = false
      this.$refs.classShow.queryClass()
      this.$refs.classShow.queryStandard()
      this.$refs.classShow.queryBrands()
      this.$refs.classShow.queryAgentiInfo()

    },
    async onSubmit(data){
      console.log(this.selectData,"添加选择的商品")
      const res = await this.$api.requested({
        "id": 20221021095403,
        "content": {
          "sa_quotedpriceid":this.$route.query.id,     //sat_notice_classid<=0时 为新增
          "items":data
        }
      })
      this.tool.showMessage(res,()=>{
        /*this.dialogProductVisible = false*/
        this.$emit('productAdd')
      /*  this.queryProduct(this.sa_quotedpriceid,this.type)*/
        this.productData()
        this.selectListData()
      })
    },
    batchSelect(){
      this.debounce(this.onSubmit(this.selectData),300)()
    },
    onSelect(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(this.selectData),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.$route.query.id
      this.params.content.sa_projectid = this.sa_projectid
      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.params.content.pageSize = val
      this.productData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.content.pageNumber = val
      this.productData()
    },
    onSuccess(){
      this.productData()
    },
    clearData(){
      this.search = ""
      this.params.content.where.condition = this.search
      this.productData()
    },
    queryClick(){
      this.params.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.$route.query.id     //sat_notice_classid<=0时 为新增
        }
      })
      this.selectTotal = res.total
    },

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

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