<template>
  <div class="app-container">
    <!-- 顶部操作区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>报表名称:</span>
        <el-input
          v-model="listQuery.consultName"
          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="time" label="报表数据日期" align="center" width="200" />
      <el-table-column prop="title" label="报表名称" align="center" />
      <el-table-column prop="endTime" label="添加时间" align="center" width="200">
        <template slot-scope="{ row }">
          {{ row.endTime ? row.endTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" style="margin: 2px" @click="handleUpdate('edit', row)"> 编辑</el-button>
          <el-button type="primary" size="mini" style="margin: 2px" @click="handleSitting(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="getCmInformationList"
    />
  </div>
</template>

<script>
import { getCmInformationList, renewFollowUpStatus } from '@/api/user/customer/customer'
import { downloadWithUrl } from '@/utils'
import { getServiceList } from '@/api/library/keyword'
export default {
  name: 'CustomerStatList',
  filters: {},
  data() {
    return {
      shopName: this.$route.query.shopName,
      isLoading: true,
      time: '',
      listQuery: {
        shopId: this.$route.query.shopId,
        consultName: '',
        consultMobile: '',
        pageNum: 1,
        pageSize: 100
      },
      list: [
        {
          time: '2023-11',
          title: '超清秀11月报表',
          endTime: '2023-12-18 09:55:51'
        },
        {
          time: '2023-10',
          title: '超清秀10月报表',
          endTime: '2023-12-18 09:55:51'
        },
        {
          time: '2023-08',
          title: '超清秀09月报表',
          endTime: '2023-12-18 09:55:51'
        }
      ],
      total: 0,
      dialogFormVisible: false,
      sellerList: [],
      renewCustome: {
        id: '',
        serviceProviderId: '',
        followUpStatus: ''
      },
      rules: {
        followUpStatus: [{ required: true, message: '请设置跟进状态', trigger: 'blur' }],
        serviceProviderId: [{ required: true, message: '请选择跟进人员', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    // 获取潜客统计列表
    async getList() {
      // this.list = []
      this.listQuery.pageNum = 1
      this.getCmInformationList()
    },
    // 获取潜客统计列表
    async getCmInformationList() {
      try {
        this.isLoading = true
        const res = await getCmInformationList(this.listQuery)
        // this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 确认设置跟进状态
    handleConfirm() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.renewFollowUpStatus(this.renewCustome)
        } else {
          return false
        }
      })
    },
    // 设置状态
    handleSitting(row) {
      this.renewCustome.id = row.id
      this.renewCustome.followUpStatus = row.followUpStatus
      this.getServiceList()
      this.dialogFormVisible = true
    },
    // 调用跟进状态API
    async renewFollowUpStatus(params) {
      try {
        await renewFollowUpStatus(params)
        this.dialogFormVisible = false
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 获取协销列表选项
    async getServiceList() {
      try {
        const res = await getServiceList()
        this.sellerList = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 删除报表
    async handleDeleteTag(row) {
      try {
        await this.$confirm('确定删除该报表数据吗？', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        // this.deleteReport(row)
      } catch (error) {
        this.$message.info('已取消删除操作')
      }
    },
    // 下载数据报表模板
    handleDownloadTemp() {
      downloadWithUrl('/temp/market-import.xlsx', '供应商数据报表.xlsx')
    },
    // 跳转预览
    handlePreview(row) {
      console.log('row', row)
      const urls = `${process.env.VUE_APP_CAIMEI_URL}/supplier/charts.html?shopId=${1634}&mouth=11`
      window.open(urls, '_blank')
    },
    // 添加/编辑
    handleUpdate(type, row) {
      if (type === 'add') {
        this.$router.push({
          path: '/user/customer/market-edit',
          query: { type: type }
        })
      } else {
        this.$router.push({
          path: '/user/customer/market-edit',
          query: { type: type, id: row.id }
        })
      }
    }
  }
}
</script>

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