<template>
  <div class="app-container">
    <!-- SOP信息卡片 -->
    <div style="margin-bottom: 10px">
      <el-card>
        <span class="custom-style" style="display: block; margin-bottom: 10px">SOP编号：{{ queryParams.sopId }}</span>
        <span class="custom-style" style="display: block;">营期编号：{{ queryParams.sopUserId }}</span>
      </el-card>
    </div>

    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="联系人ID" prop="wxContactId">
        <el-input
          v-model="queryParams.wxContactId"
          placeholder="请输入联系人ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小程序ID" prop="fsUserId">
        <el-input
          v-model="queryParams.fsUserId"
          placeholder="请输入小程序ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户评级" prop="grade">
        <el-select v-model="queryParams.grade" placeholder="请选择客户评级" clearable size="small">
          <el-option label="无" :value="0" />
          <el-option label="A" :value="1" />
          <el-option label="B" :value="2" />
          <el-option label="C" :value="3" />
          <el-option label="D" :value="4" />
          <el-option label="E" :value="5" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="正常" :value="0" />
          <el-option label="禁用" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    <!-- 客户列表表格 -->
    <el-table border v-loading="loading" :data="customerList">
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column label="用户ID" align="center" prop="customerId" width="180" />
      <el-table-column label="总完课天数" align="center" prop="finishCout" width="110" />
      <el-table-column label="最近完课时间" align="center" prop="finishTime" width="160">
        <template slot-scope="scope">
          <span v-if="scope.row.finishTime">{{ parseTime(scope.row.finishTime) }}</span>
          <span v-else style="color: #999;">-</span>
        </template>
      </el-table-column>
      <el-table-column label="连续完课天数" align="center" prop="finishCourseDays" width="110" />
      <el-table-column label="客户标签" align="center" prop="tagNames" min-width="150" show-overflow-tooltip />
      <el-table-column label="客户评级" align="center" prop="grade" width="90">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.grade === 0" type="info">无</el-tag>
          <el-tag v-else-if="scope.row.grade === 1" type="success">A</el-tag>
          <el-tag v-else-if="scope.row.grade === 2" type="primary">B</el-tag>
          <el-tag v-else-if="scope.row.grade === 3" type="warning">C</el-tag>
          <el-tag v-else-if="scope.row.grade === 4" type="danger">D</el-tag>
          <el-tag v-else-if="scope.row.grade === 5" type="danger">E</el-tag>
          <el-tag v-else type="info">-</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="7天未看课" align="center" prop="isDaysNotStudy" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isDaysNotStudy === 0" type="success">否</el-tag>
          <el-tag v-else-if="scope.row.isDaysNotStudy === 1" type="warning">是</el-tag>
          <el-tag v-else type="info">-</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="success">正常</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="warning">禁用</el-tag>
          <el-tag v-else type="info">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getSopUserLogsDetail } from "@/api/wx/sopUserLogsWx";

export default {
  name: "SopUserLogsDetailWx",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户列表数据
      customerList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sopId: null,
        sopUserId: null,
        wxContactId: null,
        fsUserId: null,
        grade: null,
        status: null
      }
    };
  },
  created() {
    // 从路由参数获取sopUserId和sopId
    const sopUserId = this.$route.params.sopUserId || this.$route.query.sopUserId;
    const sopId = this.$route.params.sopId || this.$route.query.sopId;

    if (sopUserId) {
      this.queryParams.sopUserId = sopUserId;
    }
    if (sopId) {
      this.queryParams.sopId = sopId;
    }

    // 加载数据
    this.getList();
  },
  methods: {
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      getSopUserLogsDetail(this.queryParams.sopUserId, this.queryParams).then(response => {
        this.customerList = response.rows || [];
        this.total = response.total || 0;
        this.loading = false;
      }).catch(error => {
        console.error('查询个微SOP营期详情失败:', error);
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>

<style scoped>
.custom-style {
  font-size: 14px;
  color: #606266;
}
</style>
