<template>
<div class="container normal-panel">
  <div class="normal-margin">
    <!-- <slot name="operation"></slot> -->
    <div class="flex-align-center">
      <el-radio-group class="inline-16" size="small" v-model="params.content.istool" @change="onRadioChange">
        <el-radio-button :label="0">产品</el-radio-button>
        <el-radio-button :label="1">工具</el-radio-button>
      </el-radio-group>
      <el-input style="width:300px" 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;
<!--      <ExportBtn v-if="tool.checkAuth($route.name,'export')" :tablecols="layout" :param="params" :total="total" excelTitle="购物车"></ExportBtn>-->
      <exportFile :param="params"  fileName="购物车"></exportFile>
    </div>
  </div>
  <div class="produtMag-panel">
    <tableMainLayout v-show="params.content.istool == '0'" :layout="tableColsProduct" :data="tableData" height="calc(100vh - 325px)" fixedName="operation" :width="true" :custom="true" :isCheck="true" @checkboxCallBack="selectionChange">
      <template v-slot:customcol="scope">
        <div v-if="scope.column.columnname === 'adefieldShoppingcart'">
          <el-tag size="mini" type="primary" effect="dark">{{scope.column.data.brandname}}</el-tag>&nbsp;
          <el-tag size="mini" type="primary" effect="dark">{{scope.column.data.tradefield_shoppingcart}}</el-tag>
        </div>
        <div v-else-if="scope.column.columnname === 'model'">
          <p><span>{{scope.column.data.model.length >10?scope.column.data.model.substring(0,9) + '...':scope.column.data.model}}</span></p>
        </div>
        <div v-else-if="scope.column.columnname === 'spec'">
          <p><span>{{scope.column.data.spec.length >10?scope.column.data.spec.substring(0,9) + '...':scope.column.data.spec}}</span></p>
        </div>
        <div v-else-if="scope.column.columnname === 'oldprice'">
          <span><small>¥</small>{{tool.formatAmount(scope.column.data[scope.column.columnname],2)}}</span>
        </div>
        <div v-else-if="scope.column.columnname === 'qty'">
          <el-input-number size="mini" v-model="scope.column.data.qty" :min="scope.column.data.orderminqty" @change="changeQty(scope.column.data.$index,scope.column.data)" :step="scope.column.data.orderaddqty" label="输入数量"></el-input-number>
        </div>
        <div v-else-if="scope.column.columnname === 'amount'">
          <p style="color:red;font-weight:500">¥&nbsp;{{tool.formatAmount(scope.column.data.oldprice * scope.column.data.qty,2)}}</p>
        </div>
        <p v-else>{{scope.column.data[scope.column.columnname] || scope.column.columnname === 'operation'?scope.column.data[scope.column.columnname]:'--'}}</p>
      </template>
      <template v-slot:opreation="scope">
        <slot name="del" :data="scope.data"></slot>
      </template>
    </tableMainLayout>
    <tableMainLayout v-show="params.content.istool == '1'" :layout="tableColsTool" :data="tableData" height="calc(100vh - 325px)" fixedName="operation" :width="true" :custom="true" :isCheck="true" @checkboxCallBack="selectionChange">
      <template v-slot:customcol="scope">
        <div v-if="scope.column.columnname === 'adefieldShoppingcart'">
          <el-tag size="mini" type="primary" effect="dark">{{scope.column.data.brandname}}</el-tag>&nbsp;
          <el-tag size="mini" type="primary" effect="dark">{{scope.column.data.tradefield_shoppingcart}}</el-tag>
        </div>
        <div v-else-if="scope.column.columnname === 'model'">
          <p><span>{{scope.column.data.model.length >10?scope.column.data.model.substring(0,9) + '...':scope.column.data.model}}</span></p>
        </div>
        <div v-else-if="scope.column.columnname === 'spec'">
          <p><span>{{scope.column.data.spec.length >10?scope.column.data.spec.substring(0,9) + '...':scope.column.data.spec}}</span></p>
        </div>
        <div v-else-if="scope.column.columnname === 'oldprice'">
          <span><small>¥</small>{{tool.formatAmount(scope.column.data[scope.column.columnname],2)}}</span>
        </div>
        <div v-else-if="scope.column.columnname === 'qty'">
          <el-input-number size="mini" v-model="scope.column.data.qty" :min="scope.column.data.orderminqty" @change="changeQty(scope.column.data.$index,scope.column.data)" :step="scope.column.data.orderaddqty" label="输入数量"></el-input-number>
        </div>
        <div v-else-if="scope.column.columnname === 'amount'">
          <p style="color:red;font-weight:500">¥&nbsp;{{tool.formatAmount(scope.column.data[scope.column.columnname],2)}}</p>
        </div>
        <p v-else>{{scope.column.data[scope.column.columnname] || scope.column.columnname === 'operation'?scope.column.data[scope.column.columnname]:'--'}}</p>
      </template>
      <template v-slot:opreation="scope">
        <slot name="del" :data="scope.data"></slot>
      </template>
    </tableMainLayout>
    <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 previewImage from '@/components/previewImage/index'
import ExportBtn from './exportBtn'
import exportFile from '@/components/export_file/index2'

export default {
  data () {
    return {
      params:{
        "id": 20220924095302,
        "content": {
          "pageNumber": 1,
          "pageSize": 9999,
          "istool":0,
          "where": {
            "condition": ''
          }
        }
      },
      tableSelectData:[],
      tableData: [],
      total:0,
      currentPage:0,
      layout:[],
      tableCols:[],
      tableColsProduct:[],
      tableColsTool:[]
    }
  },
  components:{
    previewImage,
    ExportBtn,
    exportFile
  },
  methods:{
    async listData () {
      const res = await this.$api.requested(this.params)
      this.tableData = res.data
      this.total = res.total
      this.currentPage = res.pageNumber
      console.log(res.data);

    },
    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
      if (this.params.content.istool == 0) {
         let _isbrandSame = this.tableSelectData.some(item=>item.sa_brandid !== this.tableSelectData[0].sa_brandid)

        if (_isbrandSame) return this.$message({message:"当前存在不同品牌产品请检查",type:'error'})

        let _isFieldSame = this.tableSelectData.some(item=>item.tradefield_shoppingcart !== this.tableSelectData[0].tradefield_shoppingcart)

        if (_isFieldSame) return this.$message({message:"当前存在不同领域产品请检查",type:'error'})

        this.$emit('selection',val)
      } else {
        this.$emit('selection',val)
      }
    },
    clearSelection () {
      this.$refs.multipleTable.clearSelection();
    },
    onRadioChange () {
      this.params.content.pageNumber = 1
      if (this.params.content.istool == 0){
        this.tableCols = this.tool.tabelCol(this.$route.name).productsTable.tablecols
      }else {
        this.tableCols = this.tool.tabelCol(this.$route.name).toolsTable.tablecols
      }
      this.listData()
      this.$emit('onRadioChange',this.params.content.istool)
    },
    async changeQty (index,row) {
      if (row.qty < row.orderminqty){
        row.qty = row.orderminqty
        const res = await this.$api.requested({
          "id": 20220924104302,
          "content": {
            "sa_shoppingcartid": row.sa_shoppingcartid,
            "qty": row.qty //数量
          },
        })
        this.$emit('qtyChange')
      }else {
        if ((row.qty - row.orderminqty)/row.orderaddqty > 0){
          let k = Math.ceil((row.qty - row.orderminqty)/row.orderaddqty)
          row.qty = k * row.orderaddqty + row.orderminqty
          const res = await this.$api.requested({
            "id": 20220924104302,
            "content": {
              "sa_shoppingcartid": row.sa_shoppingcartid,
              "qty": row.qty //数量
            },
          })
          this.$set(this.tableData,index,row)
          this.$emit('qtyChange')
        }else {
          const res = await this.$api.requested({
            "id": 20220924104302,
            "content": {
              "sa_shoppingcartid": row.sa_shoppingcartid,
              "qty": row.qty //数量
            },
          })
          this.$emit('qtyChange')
        }
      }

    }
  },
  mounted () {
    this.listData()
    this.layout = this.tool.tabelCol(this.$route.name).exportTable.tablecols
    this.tableCols = this.tool.tabelCol(this.$route.name).productsTable.tablecols
    this.tableColsProduct = this.tool.tabelCol(this.$route.name).productsTable.tablecols
    this.tableColsTool = this.tool.tabelCol(this.$route.name).toolsTable.tablecols
  }
}

</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>
