<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>
            <div class="search-item">
              <label>状态：</label>
              <el-select v-model="status" placeholder="请选择" size="small">
                <el-option
                  v-for="item in statusList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </div>
            <div class="search-item">
              <label>是否合规待办：</label>
              <el-select v-model="isViolationInfo" placeholder="请选择" size="small">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </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="800"
            style="margin-top: 8px;"
            @getList="() => {}">

            <el-table-column slot="productName" width="200px" :show-overflow-tooltip='true' label="商品名称"  fixed="left">
              <template slot-scope="scope">
                <div>
                  <el-image :src="scope.row.mainImageUrl" style="width: 40px; height: 40px" class="image" :preview-src-list="[scope.row.mainImageUrl]" />
                  {{ 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: 'SellerSelect',

    data () {
      return {
        form: {
          mallId: ''
        },
        startPage: 1,
        endPage: 10,
        reqData: {
          pageNum: 1,
          pageSize: 100,
          secondarySelectStatusList: []
        },
        colConfigs: [
          { slot: 'productName', label: '商品名称', width: '180px', align: 'left', fixed: 'left' },
          { prop: 'category', label: '分类', width: '220px', align: 'left', fixed: 'left' },
          { prop: 'siteName', label: '站点', width: '140px', align: 'left', fixed: 'left' },
          { prop: 'productId', label: 'SPU ID', width: '120px', align: 'left' },
          { prop: 'productSkcId', label: 'SKC ID', width: '120px', align: 'left' },
          { prop: 'extCode', label: 'SKC货号', width: '120px', align: 'left' },
          { prop: 'punishInfo', label: '违规信息', width: '120px', align: 'left' },
          { prop: 'violationInfo', label: '合规待办项', width: '200px', align: 'left' },
          { prop: 'supplierPrice', label: '申报价格(CNY)', width: '180px', align: 'left' },
          { prop: 'createdAt', label: '创建时间', width: '160px', align: 'left' }
        ],
        isLoading: false,
        tableData: [],
        jsonFields: {
          "商品名称": "productName",
          "图片": "mainImageUrl",
          "分类": "category",
          "站点": "siteName",
          "SPU ID": "productId",
          "SKC ID": "productSkcId",
          "SKC货号": "extCode",
          "违规信息": "punishInfo",
          "合规待办项": "violationInfo",
          "申报价格(CNY)": "supplierPrice",
          "创建时间": "createdAt"
        },
        statusList: [
          {id: 1, name: '未发布到站点'},
          {id: 2, name: '已下架/终止'}
        ],
        options: [
          {label: '是', value: 1},
          {label: '否', value: 0}
        ],
        status: 1,
        isViolationInfo: ''
      }
    },
    components: {
      JsonExcel
    },
    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
        }
        if (this.status == 1) {
          this.reqData.secondarySelectStatusList = [10, 11]
        } else if (this.status == 2) {
          this.reqData.secondarySelectStatusList = [13]
        }
        if (this.isViolationInfo == 0) {
          this.reqData.supplierTodoTypeList = []
        } else if (this.isViolationInfo == 1) {
          this.reqData.supplierTodoTypeList = [7]
        }
        this.reqData.pageNum = this.startPage
        this.tableData = []
        this.isLoading = true
        this.load()
      },
      load() {
        sendChromeAPIMessage({
          url: 'marvel-supplier/api/xmen/select/search',
          needMallId: true,
          mallId: this.form.mallId,
          anti: true,
          data: this.reqData}).then((res) => {
            if (res.errorCode == 1000000) {
              for(let i = 0;i < res.result.dataList.length; i++) {
                let item = res.result.dataList[i];
                let data = {};
                data.productName = item.productName
                data.mainImageUrl = item.carouselImageUrlList[0]
                data.productId = item.productId
                data.productSkcId = item.productSkcId
                data.siteName = item.siteName
                data.createdAt = timestampToTime(item.productCreatedAt) 

                data.category = item.fullCategoryName.join('->')

                for (let k = 0; k < item.skcList.length; k++) {
                  data = {...data, productSkcId: item.skcList[k].skcId, punishInfo: item.skcList[k].punishInfo, extCode: item.skcList[k].extCode, supplierPrice: item.skcList[k].supplierPrice}

                  if (item.skcList[k].addSiteViolationInfoVO && item.skcList[k].addSiteViolationInfoVO.violationCodeList?.length > 0) {
                    let temp = ''
                    for (let j = 0; j < item.skcList[k].addSiteViolationInfoVO.violationCodeList.length; j++) {
                      if (item.skcList[k].addSiteViolationInfoVO.violationCodeList[j] == 1) {
                        temp += ',商品资质'
                      }
                      if (item.skcList[k].addSiteViolationInfoVO.violationCodeList[j] == 2) {
                        temp += ',商品标签实拍图'
                      }
                    }
                    temp = temp.substring(1)
                    data.violationInfo = temp
                  }

                  this.tableData.push(data)
                }

              }
              if (res.result.dataList.length == this.reqData.pageSize && this.reqData.pageNum < this.endPage) {
                this.reqData.pageNum ++
                this.load()
              } else {
                this.isLoading = false
              }
            }
          })
      }
    }
  }
</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>