<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">
        <span>手机号:</span>
        <el-input
          v-model="listQuery.consultMobile"
          placeholder="手机号"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>跟进状态:</span>
        <el-select v-model="listQuery.followUpStatus" 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>
      </div>
    </div>
    <!-- 列表 -->
    <el-table v-loading="isLoading" :data="list" border style="width: 100%" height="600">
      <el-table-column prop="consultName" fixed label="姓名" align="center" />
      <el-table-column prop="consultMobile" label="手机号" align="center">
        <template slot-scope="{ row }">
          {{ row.consultMobile ? row.consultMobile : '---' }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="登记时间" align="center">
        <template slot-scope="{ row }">
          {{ row.createTime ? row.createTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column prop="followUpStatus" label="跟进状态" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.followUpStatus === 0" type="success" size="small">已跟进</el-tag>
          <el-tag v-if="row.followUpStatus === 1" type="warning" size="small">未跟进</el-tag>
          <span v-if="row.name">({{ row.name }})</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" style="margin: 2px" @click="handleSitting(row)">
            设置跟进状态
          </el-button>
          <el-button type="danger" size="mini" style="margin: 2px" @click="handleAddRemaks(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"
    />
    <!-- 设置跟进状态 -->
    <el-dialog title="设置跟进状态" :visible.sync="dialogFormVisible" width="500px">
      <el-form ref="dataForm" :model="renewCustome" label-position="top">
        <el-form-item label="请设置跟进状态：" prop="followUpStatus" :rules="rules.followUpStatus">
          <el-radio v-model="renewCustome.followUpStatus" :label="0">已跟进</el-radio>
          <el-radio v-model="renewCustome.followUpStatus" :label="1">未跟进</el-radio>
        </el-form-item>
        <el-form-item label="请选择跟进人员：" prop="serviceProviderId" :rules="rules.serviceProviderId">
          <el-select v-model="renewCustome.serviceProviderId">
            <el-option value="" label="请选择" />
            <el-option
              v-for="item in sellerList"
              :key="item.serviceProviderId"
              :label="item.linkMan"
              :value="item.serviceProviderId"
            />
          </el-select>
        </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 { getCmInformationList, renewFollowUpStatus } from '@/api/user/customer/customer'
import { getServiceList } from '@/api/library/keyword'
export default {
  name: 'CustomerStatList',
  filters: {},
  data() {
    return {
      activeName: 'first',
      isLoading: true,
      time: '',
      listQuery: {
        shopId: this.$route.query.shopId,
        consultName: '',
        consultMobile: '',
        pageNum: 1,
        pageSize: 100
      },
      list: [],
      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)
      }
    },
    // 潜客备注
    handleAddRemaks(row) {
      this.$router.push({
        path: '/user/customer/customer-remarks',
        query: { id: row.id }
      })
    }
  }
}
</script>

<style></style>
