<template>
  <div class="app-container">
    <!-- 顶部操作区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>统计日期:</span>
        <el-date-picker
          v-model="time"
          type="daterange"
          unlink-panels
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          @change="getList"
        />
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList"> 查询 </el-button>
        <el-button type="primary" icon="el-icon-plus" @click="handleAdds('add')"> 添加数据 </el-button>
      </div>
    </div>
    <!-- 列表 -->
    <el-table v-loading="isLoading" :data="list" border style="width: 100%" height="650">
      <el-table-column prop="auditTime" label="统计日期" align="center">
        <template slot-scope="{ row }">
          {{ row.auditTime ? row.auditTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column prop="productName" label="公众号" align="center" width="120" />
      <el-table-column prop="clubName" label="小红书" align="center" width="120" />
      <el-table-column prop="clubName" label="微博" align="center" width="120" />
      <el-table-column prop="clubName" label="其他渠道" align="center" width="120" />
      <el-table-column prop="reportName" label="意向强烈" align="center" width="120" />
      <el-table-column prop="reportName" label="意向普通" align="center" width="120" />
      <el-table-column prop="addTime" label="添加时间" align="center" width="120">
        <template slot-scope="{ row }">
          {{ row.addTime ? row.addTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" style="margin: 2px" @click="handleAdds('edit')"> 编辑 </el-button>
          <el-button type="primary" size="mini" style="margin: 2px" @click="handleRecordDetail(row)">
            生成链接
          </el-button>
          <el-button type="primary" size="mini" style="margin: 2px" @click="handlePathSum(row)"> 数据总结 </el-button>
          <el-button type="danger" size="mini" style="margin: 2px" @click="handleDelete(row)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[20]"
      :page-size="20"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getReportList"
    />
    <el-dialog title="审核" :visible.sync="dialogFormVisible" width="440px">
      <el-form ref="dataForm" :rules="rules" :model="form" label-position="right">
        <el-form-item label="状态:" prop="status">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option label="请选择" value="" />
            <el-option label="审核通过" :value="2" />
            <el-option label="审核不通过" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item prop="auditText">
          <el-input
            v-model="form.auditText"
            type="textarea"
            placeholder="请填写备注,200字以内"
            maxlength="200"
            :autosize="{ minRows: 5, maxRows: 5 }"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleReportCount">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getReportList, reportCount, deleteReport } from '@/api/user/consult/consult'
import pickerOptions from '@/utils/time-picker.js'
export default {
  name: 'ReportList',
  filters: {},
  data() {
    return {
      isLoading: true,
      pickerOptions,
      time: '',
      listQuery: {
        shopId: this.$route.query.shopId,
        startAddTime: '', // 开始时间
        endAddTime: '', // 结束时间
        pageNum: 1,
        pageSize: 20
      },
      list: [],
      total: 0,
      dialogFormVisible: false,
      form: {
        reportId: '',
        status: '',
        auditText: ''
      },
      rules: {
        status: [{ required: true, message: '请选择审核状态', trigger: 'blur' }],
        auditText: [{ required: true, message: '请填写备注', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    // 获取行为记录列表
    getList() {
      this.listQuery.pageNum = 1
      this.list = []
      if (this.time && this.time.length > 0) {
        this.listQuery.startAddTime = this.time[0]
        this.listQuery.endAddTime = this.time[1]
      } else {
        this.listQuery.startAddTime = ''
        this.listQuery.endAddTime = ''
      }
      this.getReportList()
    },

    // 获取关键词列表
    async getReportList() {
      try {
        this.isLoading = true
        const res = await getReportList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 审核报备
    handleExamine(row) {
      this.form.reportId = row.reportId
      this.dialogFormVisible = true
    },
    // 确定审核
    async handleReportCount() {
      try {
        await this.$refs.dataForm.validate()
        this.reportCount(this.form)
      } catch (error) {
        console.log(error)
      }
    },
    // 调用审核报备
    async reportCount(params) {
      await reportCount(params)
      this.$message.success('审核成功')
      this.closeDialog()
      this.getList()
    },
    // 添加标签取消
    closeDialog() {
      this.$refs.dataForm.resetFields()
      this.dialogFormVisible = false
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确定删除这条数据吗？删除后意向客户将不再统计这条数据。', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        this.deleteReport(row)
      } catch (error) {
        this.$message.info('已取消删除操作')
      }
    },
    // 调用删除报备
    async deleteReport(row) {
      try {
        await deleteReport(row.reportId)
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 添加数据
    handleAdds(type) {
      this.$router.push({
        path: '/user/customer/customer-ambition-edit',
        query: { type: type, shopId: this.listQuery.shopId }
      })
    },
    // 数据总结
    handlePathSum(row) {
      this.$router.push({
        path: '/user/customer/customer-ambition-sum',
        query: { type: row, shopId: this.listQuery.shopId }
      })
    },
    // 机构跳转
    handleClubHrefLink(row) {
      return (
        process.env.VUE_APP_ADMIN_URL +
        `/a/user/jumpLink/toOldAdmin?type=1&clubId=${row.clubId}&clubName=${row.corporateName}`
      )
    },
    // 查看详情
    handleRecordDetail(row) {
      this.$router.push({
        path: '/user/consult/detail',
        query: { reportId: row.reportId }
      })
    },
    checkedInput(event) {
      const pattern = /[^0-9.]/g
      return event.replace(pattern, '')
    }
  }
}
</script>

<style></style>
