<template>
  <div class="app-container">
    <!-- 顶部操作区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>报表名称:</span>
        <el-input
          v-model="listQuery.reportName"
          placeholder="报表名称"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList"> 查询 </el-button>
        <el-button type="primary" @click="handleUpdate('add')"> 上传报表 </el-button>
        <el-button type="primary" @click="handleDownloadTemp"> 下载模板 </el-button>
      </div>
    </div>
    <div class="tags-sms">
      <span class="tags-sms-span">供应商：{{ shopName }}</span>
    </div>
    <!-- 列表 -->
    <el-table v-loading="isLoading" :data="list" border style="width: 100%" height="600">
      <el-table-column label="序号" align="center" width="60">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column prop="reportDate" label="报表数据日期" align="center" width="200" />
      <el-table-column prop="reportName" label="报表名称" align="center" />
      <el-table-column prop="visible" label="报表生成状态" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.visible === 1" type="success" size="small">已生成</el-tag>
          <el-tag v-if="row.visible === 2" type="danger" size="small">未生成</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="reportTime" label="最近生成时间" align="center">
        <template slot-scope="{ row }">
          {{ row.reportTime ? row.reportTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column prop="addTime" label="添加时间" align="center">
        <template slot-scope="{ row }">
          {{ row.addTime ? row.addTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" style="margin: 2px" @click="handleSitting(row)">生成报表</el-button>
          <el-button type="primary" size="mini" style="margin: 2px" @click="handleUpdate('edit', row)"> 编辑</el-button>
          <el-button type="success" size="mini" style="margin: 2px" @click="handlePreview(row)">预览</el-button>
          <el-button type="danger" size="mini" style="margin: 2px" @click="handleDeleteTag(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[100]"
      :page-size="100"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getMarketReportList"
    />
  </div>
</template>

<script>
import { getMarketReportList, updateVisible, deleteMarketReport } from '@/api/user/customer/customer'
import { downloadWithUrl } from '@/utils'
export default {
  name: 'CustomerStatList',
  filters: {},
  data() {
    return {
      shopName: this.$route.query.shopName,
      isLoading: true,
      time: '',
      listQuery: {
        marketId: this.$route.query.marketId,
        reportName: '',
        pageNum: 1,
        pageSize: 100
      },
      list: [],
      total: 0
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    // 获取列表
    async getList() {
      // this.list = []
      this.listQuery.pageNum = 1
      this.getMarketReportList()
    },
    // 获取列表
    async getMarketReportList() {
      try {
        this.isLoading = true
        const res = await getMarketReportList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 生成报表
    handleSitting(row) {
      this.updateVisible({ id: row.id })
    },
    // 调用生成报表API
    async updateVisible(params) {
      try {
        await updateVisible(params)
        this.$message.success('报表生成成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 删除报表
    async handleDeleteTag(row) {
      try {
        await this.$confirm('确定删除该报表数据吗？', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        this.deleteMarketReport({ id: row.id })
      } catch (error) {
        this.$message.info('已取消删除操作')
      }
    },
    // 调用删除报备
    async deleteMarketReport(params) {
      try {
        await deleteMarketReport(params)
        this.$message.success('报表删除成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 下载数据报表模板
    handleDownloadTemp() {
      downloadWithUrl('/temp/供应商报表文件上传模板.zip', '供应商报表文件上传模板.zip')
    },
    // 跳转预览
    handlePreview(row) {
      const urls = `${process.env.VUE_APP_CAIMEI_URL}/supplier/charts.html?type=1&shopId=${row.shopId}&marketReportId=${row.id}`
      window.open(urls, '_blank')
    },
    // 添加/编辑
    handleUpdate(type, row) {
      if (type === 'add') {
        this.$router.push({
          path: '/user/customer/market-edit',
          query: { marketId: this.listQuery.marketId, type: type, shopName: this.$route.query.shopName }
        })
      } else {
        this.$router.push({
          path: '/user/customer/market-edit',
          query: { marketId: this.listQuery.marketId, type: type, id: row.id, shopName: this.$route.query.shopName }
        })
      }
    }
  }
}
</script>

<style>
.tags-sms {
  width: 100%;
  height: auto;
  font-size: 16px;
  color: #333333;
  line-height: 28px;
  box-sizing: border-box;
  padding: 5px;
}
</style>
