<template>
  <div class="app-container">
    <!-- 顶部操作区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>ID:</span>
        <el-input
          v-model="listQuery.reportId"
          style="width: 120px"
          placeholder="ID"
          clearable
          @input="(e) => (listQuery.reportId = checkedInput(e))"
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>商品名称:</span>
        <el-input
          v-model="listQuery.productName"
          placeholder="商品名称"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>机构名称:</span>
        <el-input
          v-model="listQuery.clubName"
          placeholder="机构名称"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>报备人:</span>
        <el-input
          v-model="listQuery.reportName"
          style="width: 120px"
          placeholder="报备人"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>审核状态:</span>
        <el-select v-model="listQuery.status" style="width: 120px" clearable @change="getList">
          <el-option value="" label="请选择" />
          <el-option :value="1" label="待审核" />
          <el-option :value="2" label="审核通过" />
          <el-option :value="3" label="审核未通过" />
        </el-select>
      </div>
      <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>
      </div>
    </div>
    <!-- 列表 -->
    <el-table v-loading="isLoading" :data="list" border style="width: 100%" height="650">
      <el-table-column prop="reportId" label="ID" align="center" width="50" />
      <el-table-column prop="mainImage" label="商品图片" align="center" width="100">
        <template v-if="row.mainImage" slot-scope="{ row }">
          <el-popover placement="top-start" title="" width="180" trigger="hover">
            <img :src="row.mainImage" alt="" style="width: 100px; height: 100px" />
            <img slot="reference" :src="row.mainImage" alt="" style="width: 50px; height: 50px" />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="productName" label="商品名称" align="center" width="200" />
      <el-table-column prop="clubName" label="机构名称" align="center" width="180" />
      <el-table-column prop="reportName" label="报备人" align="center" width="120" />
      <el-table-column prop="reportText" label="报备事由" align="center" />
      <el-table-column prop="addTime" label="创建时间" align="center" width="100">
        <template slot-scope="{ row }">
          {{ row.addTime ? row.addTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="审核状态" align="center" width="100">
        <template slot-scope="{ row }">
          <el-tag v-if="row.status === 1" type="warning" size="small">待审核</el-tag>
          <el-tag v-else-if="row.status === 2" type="success" size="small">已审核</el-tag>
          <el-tag v-else type="danger" size="small">审核未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="auditName" label="审核人" align="center" width="120" />
      <el-table-column prop="auditTime" label="审核时间" align="center" width="100">
        <template slot-scope="{ row }">
          {{ row.auditTime ? row.auditTime : '---' }}
        </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="handleExamine(row)"> 审核 </el-button>
          <el-button type="primary" size="mini" style="margin: 2px" @click="handleRecordDetail(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="[10, 20, 30, 100]"
      :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'
const defaultListQuery = {
  reportId: '', // 报备Id
  productName: '', // 咨询商品名称
  clubName: '', // 机构名称
  reportName: '', // 报备人
  status: '', // 状态 1.已审核 2.待审核 3.审核未通过
  startAddTime: '', // 开始时间
  endAddTime: '', // 结束时间
  pageNum: 1,
  pageSize: 20
}
export default {
  name: 'ReportList',
  filters: {},
  data() {
    return {
      isLoading: true,
      pickerOptions,
      time: '',
      listQuery: Object.assign({}, defaultListQuery),
      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)
      }
    },
    // 机构跳转
    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>
