<template>
    <ai-list class="Learning" v-loading="isLoading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading">
      <ai-title
        slot="title"
        title="跟价管理"
        tips="点击“一键拒绝”之后，需要回到“卖家中心”刷新界面，调价弹窗即可消失"
        isShowBottomBorder>
      </ai-title>
      <template slot="content">
        <ai-table
          :tableData="tableData"
          :col-configs="colConfigs"
          :isShowPagination="false"
          style="margin-top: 8px;"
          @getList="getList">
          <el-table-column slot="options" label="操作" align="center" fixed="right" width="140px">
            <template v-slot="{ row }">
              <div class="table-options">
                <el-button type="text" @click="edit(row)">处理</el-button>
              </div>
            </template>
          </el-table-column>
        </ai-table>
        <AiDialog
          title="调价列表"
          :visible.sync="isShow"
          :close-on-click-modal="false"
          customFooter
          width="80%">
          <ai-table
            :tableData="priceList"
            :col-configs="priceColConfigs"
            :isShowPagination="false"
            style="margin-top: 8px;">
            <el-table-column slot="newSupplyPrice" label="调整后价格" align="center">
              <template v-slot="{ row }">
                <div style="color: red">{{row.newSupplyPrice }}</div>
              </template>
            </el-table-column>
            <el-table-column slot="price" label="当前价格" align="center">
              <template v-slot="{ row }">
                <div style="color: green">{{row.price }}</div>
              </template>
            </el-table-column>
          </ai-table>
          <div class="dialog-footer" slot="footer">
            <el-button @click="isShow = false">关闭</el-button>
            <el-button @click="rejectAll()" type="primary">一键拒绝</el-button>
          </div>
         </AiDialog>
      </template>
    </ai-list>
  </template>
  
  <script>
    import { mapState } from 'vuex'
    import {sendChromeAPIMessage} from '@/api/chromeApi'
    import { Message } from 'element-ui'
    export default {
      data () {
        return {
          colConfigs: [
            { prop: 'mallName', label: '店铺名称', align: 'left' },
            { prop: 'priceTotal', label: '调价数量', align: 'left' }
          ],
          priceColConfigs: [
            { prop: 'productName', label: '商品名称', align: 'left' },
            { prop: 'skcId', label: 'SKC', align: 'left' },
            { prop: 'spec', label: 'SKU属性集', align: 'left' },
            { slot: 'newSupplyPrice', label: '调价后价格', align: 'left' },
            { slot: 'price', label: '当前价格', align: 'left' }
          ],
          tableData: [],
          dataList: [],
          priceList: [],
          total: 0,
          isShow: false,
          id: '',
          currentMallId: '',
          isLoading: false
        }
      },
  
      created () {
        this.getList()
      },
      computed: {
        ...mapState(['mallList']),
      },
  
      methods: {
        async getList () {
            this.isLoading = true
            for (let i = 0; i < this.mallList.length; i++) {
                let mallInfo = this.mallList[i]
                let res = await sendChromeAPIMessage({
                    url: 'marvel-mms/cn/api/kiana/gmp/bg/magneto/api/price/priceAdjust/gmpReducePricePopup',
                    needMallId: true,
                    mallId: mallInfo.mallId,
                    data: {}})
                if (res.success && res.errorCode == 1000000) {
                  let tempData = []
                  res.result.popUpAutoPass12.adjustList.map(item => {
                    let temp = {
                      id: item.id,
                      productName: item.productName,
                      skcId: item.skcId,
                      popUpType: 1,
                      createTime: res.result.createTime,
                      versionId: res.result.popUpAutoPass12.versionId,
                      newSupplyPrice: item.newSupplyPrice / 100,
                    }
                    item.skuInfoItemList.map(item1 => {
                      tempData.push({...temp, price: item1.price / 100, spec: item1.spec})
                    })
                  })
                  res.result.popUpAutoPass24.adjustList.map(item => {
                    let temp = {
                      id: item.id,
                      productName: item.productName,
                      skcId: item.skcId,
                      popUpType: 2,
                      createTime: res.result.createTime,
                      versionId: res.result.popUpAutoPass24.versionId,
                      newSupplyPrice: item.newSupplyPrice / 100,
                    }
                    item.skuInfoItemList.map(item1 => {
                      tempData.push({...temp, price: item1.price / 100, spec: item1.spec})
                    })
                  })
                  res.result.popUpOthers.adjustList.map(item => {
                    let temp = {
                      id: item.id,
                      productName: item.productName,
                      skcId: item.skcId,
                      popUpType: 3,
                      createTime: res.result.createTime,
                      versionId: res.result.popUpOthers.versionId,
                      newSupplyPrice: item.newSupplyPrice / 100,
                    }
                    item.skuInfoItemList.map(item1 => {
                      tempData.push({...temp, price: item1.price / 100, spec: item1.spec})
                    })
                  })
                  this.dataList.push({mallId: mallInfo.mallId, data: tempData})
                  this.tableData.push({mallId: mallInfo.mallId, mallName: mallInfo.mallName, priceTotal: (res.result.popUpAutoPass12.total + res.result.popUpAutoPass24.total + res.result.popUpOthers.total)})
                }
            }
            this.isLoading = false
        },
        edit(row) {
            let temp = this.dataList.filter(item => {
                return item.mallId == row.mallId
            })
            this.priceList = temp[0].data
            this.currentMallId = temp[0].mallId
            this.isShow = true
        },
        async rejectAll() {
            let check = await this.$userCheck(this.currentMallId)
            if (!this.currentMallId) {
                Message.error("请选择要操作的店铺")
                return
            }
            if (this.priceList.length == 0) {
              Message.success("该店铺无调价通知，无需处理")
              return
            }
            let popUpType1VersionId = ''
            let popUpType2VersionId = ''
            let popUpType3VersionId = ''
            let createTime = null
            let ids = []
            this.priceList.map(item => {
              if (item.popUpType == 1) {
                popUpType1VersionId = item.versionId
                createTime = item.createTime
                ids.push(item.id)
              }
            })
            let res = null
            if (ids.length > 0) {
              res = await sendChromeAPIMessage({
                url: 'marvel-mms/cn/api/kiana/gmp/bg/magneto/api/price/adjust/price-reduce-popup-click',
                needMallId: true,
                mallId: this.currentMallId,
                data: {
                  "rejectOrderIdList": ids,
                  "maxOrderId": popUpType1VersionId,
                  "popUpType": 1,
                  "createTime": createTime
                }})
            }
            ids = []
            this.priceList.map(item => {
              if (item.popUpType == 2) {
                createTime = item.createTime
                popUpType2VersionId = item.versionId
                ids.push(item.id)
              }
            })
            if (ids.length > 0) {
              res = await sendChromeAPIMessage({
                url: 'marvel-mms/cn/api/kiana/gmp/bg/magneto/api/price/adjust/price-reduce-popup-click',
                needMallId: true,
                mallId: this.currentMallId,
                data: {
                  "rejectOrderIdList": ids,
                  "maxOrderId": popUpType2VersionId,
                  "popUpType": 2,
                  "createTime": createTime
                }})
            }
            ids = []
            this.priceList.map(item => {
              if (item.popUpType == 3) {
                createTime = item.createTime
                popUpType3VersionId = item.versionId
                ids.push(item.id)
              }
            })
            if (ids.length > 0) {
              res = await sendChromeAPIMessage({
                url: 'marvel-mms/cn/api/kiana/gmp/bg/magneto/api/price/adjust/price-reduce-popup-click',
                needMallId: true,
                mallId: this.currentMallId,
                data: {
                  "rejectOrderIdList": ids,
                  "maxOrderId": popUpType3VersionId,
                  "popUpType": 3,
                  "createTime": createTime
                }})
            }
            if (res.success && res.errorCode == 1000000) {
              Message.success("调价通知一键拒绝成功")
              this.isShow = false
            } else {
              Message.error("调价通知一键拒绝失败")
            }
        }
      }
    }
  </script>
  
  <style scoped lang="scss">
  </style>
  