<template>
  <div>
    <ai-list class="list">
      <ai-title
        slot="title"
        title="店铺跟踪"
        isShowBottomBorder>
      </ai-title>
      <template slot="content">
        <div class="content">
          <ai-search-bar>
            <template #left>
              <el-button type="button" :class="'el-button el-button--primary'" @click="addStore()">添加店铺</el-button>
            </template>
            <template #right>
              <el-button size="small" circle icon="el-icon-refresh-right" @click="getList()"></el-button>
            </template>
          </ai-search-bar>
          <ai-table
            :tableData="tableData"
            :col-configs="colConfigs"
            :total="total"
            style="margin-top: 8px;"
            :current.sync="search.current" :size.sync="search.size"
            @getList="getList">
            <el-table-column slot="options" label="操作" width="200px" show-overflow-tooltip align="center" fixed="right">
              <template slot-scope="{ row }">
                <div class="table-options">
                  <el-button type="text" @click="deleteMonitor(row.id)">删除</el-button>
                  <el-button type="text" @click="renew(row.id)">续费</el-button>
                  <el-button type="text" @click="toDetail(row.id)">详情</el-button>
                  <el-button type="text" @click="toBegin(row)">采集数据</el-button>
                </div>
              </template>
            </el-table-column>
          </ai-table>
        </div>
      </template>
    </ai-list>

    <ai-dialog
      title="添加店铺"
      :visible.sync="isDlgShow"
      :close-on-click-modal="false"
      width="790px"
      customFooter
      @close="isDlgShow = false">
      <el-form class="ai-form" :model="form" label-width="120px" ref="form">
        <el-form-item
          prop="mallId"
          label="店铺ID"
          :rules="[{ required: true, message: '请输入店铺ID', trigger: 'blur' }]">
          <el-input placeholder="请输入店铺ID" v-model="form.mallId"></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="isDlgShow = false">取 消</el-button>
        <el-button type="primary" @click="saveStore">确定</el-button>
      </div>
    </ai-dialog>
  </div>
</template>

<script>
import {sendTemuAPIMessage} from '@/api/chromeApi'

  export default {
    name: 'List',

    data () {
      return {
        search: {
          current: 1,
          type: '1',
          size: 10
        },
        colConfigs: [
          { prop: 'content', label: '店铺ID', align: 'left' },
          { prop: 'lastUpdateTime', label: '最后一次更新时间', align: 'left' },
          { prop: 'status', label: '状态', align: 'left', format: v => this.$dict.getLabel('monitor_status', v), },
          { prop: 'expireTime', label: '失效时间', align: 'left' },
          { prop: 'createTime', label: '添加时间', width: '180px', fixed: 'right'}
        ],
        tableData: [],
        total: 0,

        form: {
          mallId: ''
        },

        isDlgShow: false,
        detailsVo: {},
        pageNo: 1,
        pageSize: 120
      }
    },

    created () {
      this.$dict.load('monitor_status');
      this.getList()
    },

    methods: {
      getList () {
        this.$http.post('/api/monitor/myPage',null,{
            params: {
                ...this.search
            }
        }).then(res => {
            this.tableData = res.data.records
            this.total = res.data.total
        })
      },
      saveStore () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.$http.post('/api/monitor/check',null, {params: {type: 1}}).then(res => {
              if (res.code == 0) {
                this.$http.post(`/api/monitor/add`, {content: this.form.mallId, type: this.search.type}).then(res => {
                  if (res.code == 0) {
                    this.$message.success('添加成功！')
                    this.$store.dispatch('getUserInfo')
                    this.getList()
                    this.isDlgShow = false
                  }
                })
              }
            })
          }
        })
      },
      renew(id) {
        this.$confirm('确定要续费？', '温馨提示', {
          type: 'warning'
        }).then(() => {
          this.$http.post('/api/monitor/renew',null, {
            params: {
              id: id
            }
          }
          ).then(res => {
            if (res.code == 0) {
              this.$message.success('续费成功！')
              this.$store.dispatch('getUserInfo')
              this.getList()
            }
          })
        })
      },
      deleteMonitor(id) {
        this.$confirm('确定要删除？', '温馨提示', {
          type: 'warning'
        }).then(() => {
          this.$http.post('/api/monitor/del',null, {
            params: {
              id: id
            }
          }
          ).then(res => {
            if (res.code == 0) {
              this.$message.success('删除成功！')
              this.getList()
            }
          })
        })
      },
      toDetail (id) {
        this.$emit('change', {
          type: 'Detail',
          params: {
            id: id || ''
          }
        })
      },
      addStore() {
        this.form.mallId = ''
        this.isDlgShow = true
      },
      toBegin(row) {
        this.detailsVo = {}
        this.detailsVo.monitorId = row.id
        this.detailsVo.details = []
        this.pageNo = 1

        this.beginCollect(row)
      }, 
      beginCollect(row) {
        sendTemuAPIMessage({
          url: 'api/bg/circle/c/mall/newGoodsList',
          anti: true,
          data: {
            "mall_id": row.content,
            "filter_items": "0:1",
            "page_number": this.pageNo,
            "page_size": this.pageSize
          }}).then((res) => {
            if (res.errorCode == 1000000) {
              res.result.data.goods_list.map(item => {
                let total = item.sales_tip_text[0]
                total = total.replace('+', '')
                if (total.indexOf('K') != -1) {
                  total = total.replace('K', '')
                  total = total * 1000
                } else if (total.indexOf('M') != -1) {
                  total = total.replace('M', '')
                  total = total * 1000000
                }
                this.detailsVo.details.push({
                  url: item.link_url,
                  price: item.price_info.price_schema,
                  saleTotal: total,
                  goodsId: item.goods_id,
                  imgUrl: item.thumb_url,
                  mallId: item.mall_id
                })
              })

              if (res.result.data.goods_list.length == this.pageSize) {
                this.pageNo = this.pageNo + 1
                this.beginCollect(row)
              } else {
                this.$http.post('/api/monitorDetail/addDetails',this.detailsVo
                ).then(res => {
                  if (res.code == 0) {
                    this.$message.success('店铺ID【' + row.content + '】数据采集成功！')
                    this.getList()
                  }
                })
              }
            }
          })
      }
    }
  }
</script>

<style scoped lang="scss">
</style>
