<template>
  <div>
    <el-button size="small" type="text" @click="onShow(drawer = true)">添加产品</el-button>
    <el-drawer
      title="添加产品"
      :visible.sync="drawer"
      append-to-body
      direction="rtl"
      :show-close="false"
      size="80%">
      <div class="drawer__panel">
        <div class="flex-align-center flex-between " style="margin-top:-10px;margin-bottom: 10px">
          <div class="flex-align-center">
            <el-input size="small"  suffix-icon="el-icon-search" v-model="param.content.where.condition" placeholder="产品名称,编号" @keyup.enter.native="listData(param.content.pageNumber = 1)" @clear="listData(param.content.pageNumber = 1)" clearable></el-input>&nbsp;
          </div>
          <div style="float: right">
            <el-button style="float: right" type="primary" size="mini" @click="onSubmit"  :disabled="tableSelectData.length === 0 ">批量添加</el-button>
          </div>
        </div>
        <div class="produtMag-panel" style="margin-top: 10px">
          <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="55">
            </el-table-column>
            <el-table-column
                align="center"
                label="产品图"
                width="80">
              <template slot-scope="scope">
                <div v-if="scope.row.attinfos[0]">
                  <previewImage style="height:38px;width:38px" :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="型号/规格">
              <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="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" :key="index">
                  <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="180">
              <template slot-scope="scope">
                <p v-for="(item,index) in scope.row.itemclass" :key="index">
                  <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="牌价"
                width="100">
            </el-table-column>
            <el-table-column
                label="操作"
                width="90"
                fixed="right">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="addProduct(scope.row)">添加</el-button>
              </template>
            </el-table-column>
          </el-table>
<!--          <div class="container normal-panel" style="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 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-sizes="[20, 50, 100, 200]"
                  layout="total, prev, pager, next, jumper"
                  :total="total">
              </el-pagination>
            </div>
          </div>
        </div>

      </div>
    </el-drawer>
  </div>
</template>

<script>
import previewImage from '@/components/previewImage/index.vue'
export default {
  props:['id','mainData'],
  components:{
    previewImage
  },
  data () {
    return {
      drawer:false,
      param:{
        "id": 20221208105403,
        "content": {
          "sa_projectid":this.id,
          "where": {
            "condition": ""
          }
        }
      },
      list:[],
      currentPage:0,
      selectTotal:0,
      total:0,
      tableSelectData:[],
      selectData:[]
    }
  },
  methods:{
    async listData () {
      this.param.content.sa_salesforecastbillid = this.$route.query.id
      const res = await this.$api.requested(this.param)
      this.list = res.data
      this.total = res.total
      this.currentPage = res.pageNumber
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.param.content.pageSize = val
      this.listData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.param.content.pageNumber = val
      this.listData()
    },
    onShow () {
      console.log(this.mainData)
      this.listData()
      this.selectListData()
    },
    async addProduct (row) {
      console.log(row,"添加商品")
      row.sa_salesforecastid = 0
      const res = await this.$api.requested({
        "id": 20220906155003,
        "content": {
            "sa_salesforecastmodelid":this.mainData.sa_salesforecastmodelid,
            "sa_salesforecastbillid":this.$route.query.id,
            "sa_projectid":this.id,
            "itemclassinfos": [{
              "sa_salesforecastid":0,
              "itemid":row.itemid,
              "itemclassnum":'',
              "orderqty": 1,
              "orderamount": row.marketprice,
              "invoiceqty": 1,
              "invoiceamount": row.marketprice,
              "outqty": 1,
              "outamount": row.marketprice
            }]
        }
      })
      this.tool.showMessage(res,()=>{
        this.$emit('onSuccess')
        this.listData()
        this.selectListData()
      })
    },
    /*选择要添加的数据*/
    selectionChange(val){
      console.log(val)
      this.tableSelectData = []
      this.tableSelectData = val
      let obj = this.tableSelectData.map(e=>{
        return {
          "sa_salesforecastid":0,
          "itemid":e.itemid,
          "itemclassnum":'',
          "orderqty": 1,
          "orderamount": e.marketprice,
          "invoiceqty": 1,
          "invoiceamount": e.marketprice,
          "outqty": 1,
          "outamount": e.marketprice
        }
      })
      this.selectData = obj
      console.log(this.selectData)
    },
    /*批量添加产品*/
    async onSubmit(){
      const res = await this.$api.requested({
        "id": 20220906155003,
        "content": {
          "sa_salesforecastmodelid":this.mainData.sa_salesforecastmodelid,
          "sa_salesforecastbillid":this.$route.query.id,
          "sa_projectid":this.id,
          "itemclassinfos": this.selectData
        }
      })
      this.tool.showMessage(res,()=>{
        this.$emit('onSuccess')
        this.listData()
        this.selectListData()
      })
    },
    /*已选产品*/
    async selectListData(){
      const res = await this.$api.requested({
        "id": 20220906154703,
        "content": {
          "sa_salesforecastbillid":this.$route.query.id,
          "where":{
            "sa_projectid":'',
            "condition":""
          }
        }
      })
      this.selectTotal = res.total
    }
  },
  mounted () {
    
  }
}

</script>
<style>
</style>