<template>
  <div class="app-container">
    <!-- 顶部操作区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>供应商名称:</span>
        <el-input
          v-model="listQuery.shopName"
          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="0" label="统计中" />
          <el-option :value="1" label="暂停统计" />
        </el-select>
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList"> 查询 </el-button>
        <el-button type="primary" @click="shopDialogVisible = true"> 添加供应商 </el-button>
      </div>
    </div>
    <!-- 列表 -->
    <el-table v-loading="isLoading" :data="list" border style="width: 100%" height="660">
      <el-table-column prop="shopName" fixed label="供应商名称" align="center" />
      <el-table-column prop="linkMan" label="联系人" align="center" />
      <el-table-column prop="mobile" label="手机号" align="center">
        <template slot-scope="{ row }">
          {{ row.mobile ? row.mobile : '---' }}
        </template>
      </el-table-column>
      <el-table-column prop="shopStatus" label="供应商状态" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.shopStatus === 90" type="success" size="small">已上线</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="统计状态" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.status === 0" type="success" size="small">统计中</el-tag>
          <el-tag v-if="row.status === 1" type="danger" size="small">暂停统计</el-tag>
        </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 fixed="right" label="操作" align="center" width="400">
        <template slot-scope="{ row }">
          <!-- <el-button type="primary" size="mini" style="margin: 4px" @click="handleRecordDetail(7, row)">
            意向客户数据
          </el-button> -->
          <el-button type="primary" size="mini" style="margin: 4px" @click="handleRecordDetail(1, row)">
            潜客统计列表
          </el-button>
          <el-button type="primary" size="mini" style="margin: 4px" @click="handleRecordDetail(2, row)">
            相关文章
          </el-button>
          <el-button type="primary" size="mini" style="margin: 4px" @click="handleRecordDetail(3, row)">
            相关搜索词
          </el-button>
          <el-button type="success" size="mini" style="margin: 4px" @click="handleRecordDetail(4, row)">
            页面访问统计
          </el-button>
          <el-button type="success" size="mini" style="margin: 4px" @click="handleRecordDetail(5, row)">
            弹窗样式
          </el-button>
          <el-button type="warning" size="mini" style="margin: 4px" @click="handleRecordDetail(6, 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="getCustomerShopList"
    />
    <!--  选择供应商弹窗 -->
    <shop-dialog v-if="shopDialogVisible" ref="shopDialog" @confirm="handleAddShopConfirm" @cancel="handleCancel" />
    <!-- 设置统计状态 -->
    <el-dialog title="设置统计状态" :visible.sync="dialogFormVisible" width="500px">
      <el-form ref="dataForm" :model="renewCustome" :rules="rules" label-position="right">
        <el-form-item prop="status">
          <el-radio v-model="renewCustome.status" :label="0">统计中</el-radio>
          <el-radio v-model="renewCustome.status" :label="1">暂停统计</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCustomerShopList, saveCustomerShop, renewCustomerShop } from '@/api/user/customer/customer'
import ShopDialog from './components/shop-dialog'
export default {
  name: 'RecordList',
  components: { ShopDialog },
  filters: {},
  data() {
    return {
      isLoading: true,
      listQuery: {
        shopName: '',
        status: '',
        pageNum: 1,
        pageSize: 20
      },
      list: [],
      total: 0,
      shopDialogVisible: false,
      dialogFormVisible: false,
      renewCustome: {
        id: '',
        status: ''
      },
      rules: {
        status: [{ required: true, message: '请设置统计状态', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    // 获取行为记录列表
    getList() {
      this.list = []
      this.listQuery.pageNum = 1
      this.getCustomerShopList()
    },
    // 确认选择供应商
    handleAddShopConfirm(data) {
      console.log('data', data)
      const shopIds = []
      data.forEach((ele) => {
        shopIds.push(ele.shopId)
      })
      this.saveCustomerShop({ shopIds: shopIds.join(',') })
    },
    // 取消选择供应商
    handleCancel() {
      this.shopDialogVisible = false
      this.$refs.shopDialog.visible = false
    },
    // 确认设置统计状态
    handleConfirm() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.renewCustomerShop(this.renewCustome)
        } else {
          return false
        }
      })
    },
    // 获取潜客搜集供应商列表
    async getCustomerShopList() {
      try {
        this.isLoading = true
        const res = await getCustomerShopList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 保存添加统计供应商
    async saveCustomerShop(params) {
      try {
        await saveCustomerShop(params)
        this.shopDialogVisible = false
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 统计状态
    async renewCustomerShop(params) {
      try {
        await renewCustomerShop(params)
        this.dialogFormVisible = false
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 查看详情
    handleRecordDetail(type, row) {
      switch (type) {
        case 1: // 潜客统计列表
          this.$router.push({
            path: '/user/customer/customer-stat-list',
            query: { shopId: row.shopId }
          })
          break
        case 2: // 相关文章
          this.$router.push({
            path: '/user/customer/customer-article-list',
            query: { shopId: row.shopId }
          })
          break
        case 3: // 相关搜索词
          this.$router.push({
            path: '/user/customer/customer-search-list',
            query: { shopId: row.shopId }
          })
          break
        case 4: // 页面访问
          this.$router.push({
            path: '/user/customer/customer-page-list',
            query: { shopId: row.shopId }
          })
          break
        case 5: // 弹窗样式
          this.$router.push({
            path: '/user/customer/customer-popup-edit',
            query: { shopId: row.shopId }
          })
          break
        case 6:
          this.renewCustome.id = row.id
          this.renewCustome.status = row.status
          console.log('renewCustome', this.renewCustome)
          this.dialogFormVisible = true
          break
        case 7: // 意向客户数据
          this.$router.push({
            path: '/user/customer/customer-ambition-list',
            query: { shopId: row.shopId }
          })
          break
      }
    }
  }
}
</script>

<style></style>
