<template>
  <div>
    <div >
      <el-input
          size="small"
          style="width: 200px;float: left"
          suffix-icon="el-icon-search"
          v-model="param.content.where.condition"
          placeholder="产品名称,编号"
          @keyup.enter.native="productData(param.content.pageNumber = 1)"
          @clear="productData(param.content.pageNumber = 1)"
          clearable>
      </el-input>&nbsp;
      <product_table style="float: right" :id="data.sa_projectid"  ref="quoterPrice" @productAdd="productInitialization"></product_table>
    </div>
    <div class="produtMag-panel">
      <el-table
          ref="multipleTable"
          :data="list"
          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"
            label="产品图"
            width="100">
          <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>&nbsp;/&nbsp;<span>{{scope.row.spec}}</span></p>
          </template>
        </el-table-column>
        <el-table-column
            prop="caliber"
            label="口径"
            width="180">
          <template slot-scope="scope">
            <p><span>{{scope.row.caliber?scope.row.caliber:'--'}}</span></p>
          </template>
        </el-table-column>
        <el-table-column
            prop="pressure"
            label="压力"
            width="180">
          <template slot-scope="scope">
            <p><span>{{scope.row.pressure?scope.row.pressure:'--'}}</span></p>
          </template>
        </el-table-column>
        <el-table-column
            prop="material"
            label="材质"
            width="180">
          <template slot-scope="scope">
            <p><span>{{scope.row.material?scope.row.material:'--'}}</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="180">
          <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="num"
            label="数量"
            width="180">
          <template slot-scope="scope">
            <el-input-number  v-model="scope.row.qty" size="mini" :min="1"  label="描述文字" @change="onChangeNum(scope.row.qty,scope.row,scope.$index)"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column
            prop="marketprice"
            label="牌价"
            width="100">
          <template slot-scope="scope">
            <p><span>{{scope.row.marketprice?scope.row.marketprice:'--'}}</span></p>
          </template>
        </el-table-column>
        <el-table-column
            prop="discountrate"
            label="折扣(%)"
            width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.discountrate" size="mini" placeholder="请输入折扣" @change="onChangeDiscountrate(scope.row.discountrate,scope.row,scope.$index)"></el-input>
          </template>
        </el-table-column>
        <el-table-column
            prop="price"
            label="单价"
            width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.price" size="mini" placeholder="请输入单价" @change="onChangePrice(scope.row.price,scope.row,scope.$index)"></el-input>
          </template>
        </el-table-column>
        <el-table-column
            prop="amount"
            label="金额"
            width="100">
          <template slot-scope="scope">
            <p><span>{{scope.row.amount}}</span></p>
          </template>
        </el-table-column>
        <el-table-column
            prop="specialoffer"
            label="是否特价"
            width="100">
          <template slot-scope="scope">
            <i class="el-icon-check" v-if="scope.row.specialoffer === 1"  style="color: red;font-size: 20px"></i>
          </template>
        </el-table-column>
        <el-table-column
            prop="operation"
            label="操作"
            width="100">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="deleteProduct(scope.row)">删除</el-button>
          </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="param.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";
import product_table from '../components/productTable'

export default {
  props:["data"],
  name: "productInventory",
  components:{uploadFile, previewImage,product_table},
  data(){
    return {
      tablecols:[],
      list:[],
      total:0,
      currentPage:0,
      changeList:[],
      param:{
        "id": 20221021095503,
        "version":1,
        "content": {
          "sa_quotedpriceid":'',
          "pageNumber": 1,
          "pageSize": 20,
          "where": {
            "condition": ""
          }

        }
      }
    }
  },
  methods:{
    /*产品配置信息新增初始化*/
    async productInitialization(id){
      this.param.content.sa_quotedpriceid = id
      const res = await this.$api.requested(this.param)
      this.list = res.data
      this.total = res.total
      for (var i=0 ;i<this.list.length;i++){

        this.list[i].discountrate = this.list[i].discountrate * 100

      }
      console.log(this.list,'产品配置信息')
    },
    /*产品配置信息*/
    async productData(id){
      this.param.content.sa_quotedpriceid = id
      const res = await this.$api.requested(this.param)
      this.list = res.data
      this.total = res.total
      for (var i=0 ;i<this.list.length;i++){
        this.list[i].discountrate = this.list[i].discountrate * 100
      }
      console.log(this.list,'产品配置信息')
    },
    /*修改折扣或单价的时候刷新*/
    async productDiscountrateData(id){
      this.param.content.sa_quotedpriceid = id
      const res = await this.$api.requested(this.param)
      this.list = res.data
      this.total = res.total
      for (var i=0 ;i<this.list.length;i++){
        this.list[i].discountrate = this.list[i].discountrate * 100
      }
      console.log(this.list,'产品配置信息')
    },
    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(){},
    checkForm(){
      this.$emit('checkForm')
    },
    quoterPrice(id,type){
      console.log(id,"报价单id")
      console.log(type,"项目报价")
      this.$refs.quoterPrice.queryProduct(id,type)
    },
    onChangeNum (val,data,index) {
      console.log(val,data,index)
      /* data.amount = data.qty * data.price*/
      this.$set(this.list,index,data)
      this.$nextTick(()=>{
        let obj = this.list.map(e=>{
          return {
            sa_quotedprice_itemsid:e.sa_quotedprice_itemsid,
            itemid:e.itemid,
            oldprice:e.marketprice,
            price:e.price,
            discountrate:(e.discountrate/100).toFixed(4),
            qty:e.qty
          }
        })
        this.changeData(obj)
      })
    },
    onChangeDiscountrate(val,data,index) {
      /* data.price = data.marketprice * (data.discountrate / 100)
       data.amount = (data.qty * data.price).toFixed(2)*/
      this.$set(this.list,index,data)
      this.$nextTick(()=>{
        let obj = this.list.map(e=>{
          return {
            sa_quotedprice_itemsid:e.sa_quotedprice_itemsid,
            itemid:e.itemid,
            oldprice:e.marketprice,
            price:0,
            discountrate:(e.discountrate/100).toFixed(4),
            qty:e.qty
          }
        })
        this.changeDiscountrateData(obj)
      })
    },
    onChangePrice(val,data,index){
      /*data.discountrate = ((data.price / data.marketprice) * 100).toFixed(2)
      data.amount = data.qty * data.price*/
      this.$set(this.list,index,data)
      this.$nextTick(()=>{
        this.objSet(this.list)
      })
    },
    objSet(data){
      let obj = data.map(e=>{
        return {
          sa_quotedprice_itemsid:e.sa_quotedprice_itemsid,
          itemid:e.itemid,
          oldprice:e.marketprice,
          price:e.price,
          discountrate:0,
          qty:e.qty
        }
      })
      this.changeDiscountrateData(obj)
    },
    async changeData(obj){
      console.log(this.list,"数据")
      const res = await this.$api.requested({
        "id": 20221021095403,
        "version":1,
        "content": {
          "sa_quotedpriceid":this.list[0].sa_quotedpriceid,     //sat_notice_classid<=0时 为新增
          "items":obj
        }
      })
      console.log(res)
      this.productData(res.data[0].sa_quotedpriceid)
    },
    async changeDiscountrateData(obj){
      console.log(this.list,"数据")
      const res = await this.$api.requested({
        "id": 20221021095403,
        "version":1,
        "content": {
          "sa_quotedpriceid":this.list[0].sa_quotedpriceid,     //sat_notice_classid<=0时 为新增
          "items":obj
        }
      })
      console.log(res)
      this.productDiscountrateData(res.data[0].sa_quotedpriceid)
    },
    async deleteProduct(val){
      console.log(val)
      const res = await this.$api.requested({
        "id": 20221021095603,
        "version":1,
        "content": {
          "sa_quotedprice_itemsids":[val.sa_quotedprice_itemsid]     //sat_notice_classid<=0时 为新增
        }
      })
      this.tool.showMessage(res,()=>{
        this.productData(val.sa_quotedpriceid)
      })
    }
  },
  mounted() {
    console.log(this.data)
    this.productData(this.data.sa_quotedpriceid)
  }
}
</script>

<style scoped>
.produtMag-panel{
  margin-top: 35px;
  background: #fff;
  border-radius:5px;

  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:80px;height:80px;margin:0px auto;
}
</style>