<template>
  <div class="container">
    <div class="tagBox">
      <!-- <div>{{ tag }}</div> -->
      <span>{{ tag }}</span>
      <!-- <div>{{ list.length }}人</div> -->
    </div>
    <div class="table-container">
      <el-table
        ref="productTable"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
        @sort-change="changeTableSort"
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="姓名" align="center">
          <template slot-scope="scope">
            <div
              class="handleStuDetail"
              @click="handleStuDetail(scope.row.userId)"
            >
              {{ scope.row.nameCn }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="性别" align="center">
          <template slot-scope="scope">{{
            scope.row.sex == 0 ? "女" : scope.row.sex == 1 ? "男" : ""
          }}</template>
        </el-table-column>
        <el-table-column label="客户状态" align="center">
          <template slot-scope="scope">{{ scope.row.userStatus }}</template>
        </el-table-column>
        <el-table-column label="渠道" align="center">
          <template slot-scope="scope">
            <div v-show="scope.row.channel == 1">主动推荐引流</div>
            <div v-show="scope.row.channel == 2">客服自主添加</div>
            <div v-show="scope.row.channel == 3">搜索引擎引流</div>
            <div v-show="scope.row.channel == 4">外部资源推荐</div>
            <div v-show="scope.row.channel == 5">自媒体引流</div>
            <div v-show="scope.row.channel == 6">地推活动</div>
          </template>
        </el-table-column>

        <el-table-column label="意向度" align="center">
          <template slot-scope="scope">{{
            scope.row.planStatus == 1
              ? "低"
              : scope.row.planStatus == 2
              ? "中"
              : scope.row.planStatus == 3
              ? "高"
              : ""
          }}</template>
        </el-table-column>

        <el-table-column label="微信号" align="center">
          <template slot-scope="scope">{{ scope.row.wechatNumber }}</template>
        </el-table-column>
        <el-table-column label="学校" align="center">
          <template slot-scope="scope">{{ scope.row.school }}</template>
        </el-table-column>
        <el-table-column label="最近沟通记录" align="center">
          <template slot-scope="scope">{{ scope.row.context }}</template>
        </el-table-column>
        <el-table-column
          label="最近沟通时间"
          :sortable="'custom'"
          align="center"
          prop="addTime"
        >
          <template slot-scope="scope">{{ scope.row.addTime }}</template>
        </el-table-column>
        <el-table-column
          label="下次沟通时间"
          :sortable="'custom'"
          align="center"
          prop="nextChatTime"
        >
          <template slot-scope="scope">{{ scope.row.nextChatTime }}</template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        layout="total,sizes,prev, pager, next,jumper"
        :page-size="pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-button @click="del" class="delTag" type="danger">删除</el-button>
    <!-- <div v-for="(item, index) in multipleSelection" :key="index">
      {{ item.userId }},,,,,{{ listQuery.tagId }}
    </div> -->
    <!-- <div>{{ delId }}</div> -->
  </div>
</template>

<script>
// import { singleTag, deleteList } from "@/api/cso";
import { singleTag, xuefuSingleTag, deleteList } from "@/api/cso";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};

export default {
  name: "singleTagPage",
  data() {
    return {
      tag: "",
      num: "",
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      pageSize: null,
      pageNum: null,
      total: null,
      listLoading: true,
      multipleSelection: [],
      delId: [],
      roles: ""
    };
  },
  methods: {
    getSingleTag() {
      // 有点蠢，先这么用着吧
      singleTag(this.listQuery).then(res => {
        this.listLoading = false;

        this.list = res.data.list;
        this.pageNum = res.data.pageNum;
        this.pageSize = res.data.pageSize;
        this.total = res.data.total;

        this.$forceUpdate();
        console.log("客服部标签");
      });
    },
    getXfSingleTag() {
      // 有点蠢，先这么用着吧
      xuefuSingleTag(this.listQuery).then(res => {
        this.listLoading = false;

        this.list = res.data.list;
        this.pageNum = res.data.pageNum;
        this.pageSize = res.data.pageSize;
        this.total = res.data.total;

        this.$forceUpdate();
        console.log("学服部标签");
      });
    },
    handleSelectionChange(val) {
      this.delId = [];
      this.multipleSelection = val;
      //   let arr = [];
      val.forEach(item => {
        // arr.push(item.userId);
        this.delId.push(item.userId);
        // this.delId = [...new Set(arr)];
      });
    },
    changeTableSort(column) {
      //   console.log("prop", column.prop);
      //   console.log("order", column.order);
      // console.log("column", column);
      if (column.prop == "addTime") {
        this.listQuery.secondSort = null;
        if (column.order == "ascending") {
          this.listQuery.firstSort = 0;
        } else if (column.order == "descending") {
          this.listQuery.firstSort = 1;
        }
      }
      if (column.prop == "nextChatTime") {
        this.listQuery.firstSort = null;
        if (column.order == "ascending") {
          this.listQuery.secondSort = 0;
        } else if (column.order == "descending") {
          this.listQuery.secondSort = 1;
        }
      }
      if (column.order == null) {
        this.listQuery.secondSort = this.listQuery.firstSort = null;
      }
      // this.getSingleTag();
      if (
        this.roles.includes("客服部老师") ||
        this.roles.includes("客服部主管")
      ) {
        console.log("客服部老师");
        this.getSingleTag();
      }
      if (
        this.roles.includes("学服部老师") ||
        this.roles.includes("学服部主管")
      ) {
        console.log("学服部老师");
        this.getXfSingleTag();
      }
      console.log("changeTableSort");
    },
    handleCurrentChange(val) {
      // 翻页
      this.listQuery.pageNum = val;
      // this.getSingleTag();
      if (
        this.roles.includes("客服部老师") ||
        this.roles.includes("客服部主管")
      ) {
        console.log("客服部老师");
        this.getSingleTag();
      }
      if (
        this.roles.includes("学服部老师") ||
        this.roles.includes("学服部主管")
      ) {
        console.log("学服部老师");
        this.getXfSingleTag();
      }
    },
    handleStuDetail(id) {
      // 测试用的
      this.$router.push({
        path: "/pms/stuDetail",
        query: {
          id: id
        }
      });
      // console.log(123);
    },
    del() {
      //   this.$refs[formName].validate(valid => {
      //   if (valid) {
      //     this.$confirm("是否要提交", "提示", {
      //       confirmButtonText: "确定",
      //       cancelButtonText: "取消",
      //       type: "warning"
      //     }).then(() => {
      //       createUser(this.form).then(response => {
      //         this.$message({
      //           type: "success",
      //           message: "提交成功",
      //           duration: 1000
      //         });
      //         // location.reload();
      //       });
      //     });
      //   } else {
      //     console.log("error submit!!");
      //     console.log(valid);
      //     return false;
      //   }
      // });
      if (this.delId.length > 0) {
        this.$confirm("是否要删除", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          deleteList({
            tagId: this.listQuery.tagId * 1,
            userIds: this.delId
          }).then(res => {
            this.$message({
              type: "success",
              message: "删除成功",
              duration: 1000
            });
            // this.getSingleTag();
            if (
              this.roles.includes("客服部老师") ||
              this.roles.includes("客服部主管")
            ) {
              console.log("客服部老师");
              this.getSingleTag();
            }
            if (
              this.roles.includes("学服部老师") ||
              this.roles.includes("学服部主管")
            ) {
              console.log("学服部老师");
              this.getXfSingleTag();
            }
          });
        });
      } else {
        this.$message({
          type: "error",
          message: "无内容",
          duration: 1000
        });
      }
    }
  },
  mounted() {
    this.listQuery.tagId = this.$route.query.tagId;
    // console.log(typeof this.listQuery.tagId, "typeof this.listQuery.tagId");
    this.tag = this.$route.query.tag;
    this.num = this.$route.query.num;
    // console.log(this.listQuery.tagId, "this.listQuery.tagId");
    // this.getSingleTag();
    this.roles = this.$store.state.user.roles;
    if (
      this.roles.includes("客服部老师") ||
      this.roles.includes("客服部主管")
    ) {
      console.log("客服部老师");
      this.getSingleTag();
    }
    if (
      this.roles.includes("学服部老师") ||
      this.roles.includes("学服部主管")
    ) {
      console.log("学服部老师");
      this.getXfSingleTag();
    }
    // console.log(this.roles, "this.roles");
  }
};
</script>

<style lang="scss" scoped>
.container {
  // display: flex;
  // flex-direction: column;
  padding: 20px;
}
.tagBox {
  // display: flex;
  // justify-content: center;
  // align-items: center;

  span {
    // margin: 3px

    border: 3px solid #3498db;
    color: #3498db;
    font-weight: bold;

    padding: 5px 10px;
    margin: 10px;
    border-radius: 20px;
  }
}
.delTag {
  margin-top: 20px;
}
.handleStuDetail {
  cursor: pointer;
}
</style>
