<template>
  <ai-list class="list" v-loading="isLoading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading">
    <ai-title
      slot="title"
      title="调价管理"
      tips="每页为100条商品数据"
      isShowBottomBorder>
      <template #rightBtn>
        <div class="title-right">
          <div>
            <label style="width:90px">店铺：</label>
            <el-select v-model="form.mallId" placeholder="请选择" size="small">
              <el-option
                v-for="item in $store.state.mallList"
                :key="item.mallId"
                :label="item.mallName"
                :value="item.mallId">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>
    </ai-title>
    <template slot="content">
      <div class="content">
        <ai-search-bar>
          <template #left>
            <div class="search-item">
              <label>起始页：</label>
              <el-input size="small"  placeholder="请输入起始页" type="number" v-model="startPage"></el-input>
            </div>
            <div class="search-item">
              <label>结束页：</label>
              <el-input size="small"  placeholder="请输入起始页" type="number" v-model="endPage"></el-input>
            </div>
          </template>
          <template #right>
            <el-button type="primary" @click="toLoad">加载</el-button>
          </template>
        </ai-search-bar>
        <ai-card title="数据明细" style="padding-bottom: 40px;">
          <template #right>
              <json-excel
                  :data="tableData"
                  :fields="jsonFields"
                  name="调价列表.xls"
                  worksheet="调价列表">
                  <el-button type="primary" :disabled="tableData.length == 0">下载数据</el-button>
                </json-excel>
          </template>
          <ai-table
            :isShowPagination="false"
            :tableData="tableData"
            :col-configs="colConfigs"
            :total="tableData.length"
            height="700"
            style="margin-top: 8px;"
            @getList="() => {}">

            <el-table-column slot="productName" width="250px" :show-overflow-tooltip='true' label="商品名称"  fixed="left">
              <template slot-scope="scope">
                <div>
                  <el-image :src="scope.row.image" style="width: 40px; height: 40px" class="image" :preview-src-list="[scope.row.image]" />
                  {{ scope.row.productName }}
                </div>
              </template>
            </el-table-column>
          </ai-table>
        </ai-card>
      </div>
    </template>
  </ai-list>
</template>

<script>
import { Message } from 'element-ui'
import {sendChromeAPIMessage} from '@/api/chromeApi'
import {timestampToTime} from '@/utils/date'
import JsonExcel from 'vue-json-excel'

  export default {
    name: 'MyNormalOrder',

    data () {
      return {
        form: {
          mallId: ''
        },
        startPage: 1,
        endPage: 10,
        reqData: {
          pageInfo: {
            pageSize: 100,
            pageNo: 1
          },
          status: 2
        },
        colConfigs: [
          { slot: 'productName', label: '商品名称', width: '250px', align: 'left', fixed: 'left' },
          { prop: 'priceOrderSn', label: '调价单号', width: '140px', align: 'left', fixed: 'left' },
          { prop: 'skcId', label: 'SKC ID', width: '120px', align: 'left' },
          { prop: 'skcExtCode', label: 'SKC货号', width: '100px', align: 'left' },
          { prop: 'productSkuId', label: 'SKU ID', width: '120px', align: 'left' },
          { prop: 'skuExtCode', label: 'SKU货号', width: '100px', align: 'left' },
          { prop: 'spec', label: 'SKU属性', width: '100px', align: 'left' },
          { prop: 'priceBeforeExchange', label: '原申报价格', width: '100px', align: 'left' },
          { prop: 'newSupplyPrice', label: '调整后申报价格', width: '100px', align: 'left' },
          { prop: 'operateUserName', label: '操作人', width: '100px', align: 'left' },
          { prop: 'operateSourceName', label: '操作页面', width: '100px', align: 'left' },
          { prop: 'operateTime', label: '操作时间', width: '100px', align: 'left' },
          { prop: 'source', label: '申请来源', width: '120px', align: 'left' },
          { prop: 'adjustReason', label: '调价原因', align: 'left' }
        ],
        isLoading: false,
        tableData: [],
        jsonFields: {
          "商品名称": "productName",
          "图片": "image",
          "调价单号": "priceOrderSn",
          "SKC ID": "skcId",
          "SKC货号": "skcExtCode",
          "SKU ID": "productSkuId",
          "SKU货号": "skuExtCode",
          "SKU属性": "spec",
          "原申报价格": "priceBeforeExchange",
          "调整后申报价格": "newSupplyPrice",
          "操作人": "operateUserName",
          "操作页面": "operateSourceName",
          "操作时间": "operateTime",
          "申请来源": "source",
          "调价原因": "adjustReason"
        },
        currentIndex: 0
      }
    },
    components: {
      JsonExcel
    },
    created () {
    },
    methods: {
      beforeGetList() {
        this.$userCheck(this.form.mallId).then(() => {
          this.toLoad()
        }).catch((err) => {
          this.form.mallId = ''
        })
      },
      toLoad() {
        if (!this.form.mallId) {
          Message.error("请选择店铺")
          return
        }
        if (!this.startPage || (this.startPage < 1)) {
          Message.error("起始页不能为空，且不能小于1")
          return
        }
        if (!this.endPage || (this.startPage < 1)) {
          Message.error("结束页不能为空")
          return
        }
        if (this.startPage > this.endPage) {
          Message.error("起始页不能大于结束页")
          return
        }
        this.reqData.pageInfo.pageNo = this.startPage
        this.tableData = []
        this.currentIndex = 0
        this.isLoading = true
        this.load()
      },
      async load() {
        let res = await sendChromeAPIMessage({
          url: 'marvel-mms/cn/api/kiana/magneto/price-adjust/page-query',
          needMallId: true,
          mallId: this.form.mallId,
          anti: true,
          data: this.reqData})
        if (res.errorCode == 1000000) {
          for(let i = 0;i < res.result.list.length; i++) {
            let item = res.result.list[i];
            let data = {};
            data.id = item.id
            data.productName = item.productName
            data.image = item.image
            data.priceOrderSn = item.priceOrderSn
            data.skcId = item.skcId
            data.skcExtCode = item.skcExtCode
            data.source = item.source
            data.adjustReason = item.adjustReason
            data.newSupplyPrice = item.newSupplyPrice / 100

            for(let k = 0; k < item.skuInfoItemList.length; k++) {
              data = {...data,
                productSkuId: item.skuInfoItemList[k].productSkuId,
                priceBeforeExchange: item.skuInfoItemList[k].priceBeforeExchange / 100,
                skuExtCode: item.skuInfoItemList[k].skuExtCode,
                spec: item.skuInfoItemList[k].spec,
                operateUserName: null,
                operateTime: null,
                operateSourceName: null
              }

              this.tableData.push(data)
            }

          }
          if (res.result.list.length == this.reqData.pageInfo.pageSize && this.reqData.pageInfo.pageNo < this.endPage) {
            this.reqData.pageInfo.pageNo ++
            await this.load()
          } else {
            await this.getPriceAdjustLog()
            this.isLoading = false
          }
        }
      },
      async getPriceAdjustLog() {
        for (let i = 0; i < this.tableData.length; i++) {
          let res = await sendChromeAPIMessage({
            url: 'marvel-mms/cn/api/kiana/gmp/bg/magneto/api/price/adjust/log',
            needMallId: true,
            mallId: this.form.mallId,
            anti: true,
            data: {priceOrderId: this.tableData[i].id}})
          if (res.errorCode == 1000000) {
            if (res.result.list && res.result.list.length > 0) {
              this.tableData[i].operateSourceName = res.result.list[res.result.list.length - 1].operateSourceName
              this.tableData[i].operateTime = timestampToTime(res.result.list[res.result.list.length - 1].operateTime)
              this.tableData[i].operateUserName = res.result.list[res.result.list.length - 1].operateUserName
            }
          }
        }
      }
    }
  }
</script>

<style scoped lang="scss">
  .list {
    .title-right {
      display: flex;
      align-items: center;

      & > div:first-child {
        margin-right: 20px;
      }
    }
    ::v-deep.ai-list {
      .ai-list__content--right-wrapper {
        background: transparent;
        box-shadow: none;
        padding: 0!important;
      }
    }
  }
</style>