<template>
  <div class="container">
    <!-- <h1>kefuBind</h1> -->
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList('form')"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="resetForm('form')"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          ref="form"
          size="small"
          label-width="140px"
          @keyup.enter.native="handleSearchList()"
        >
          <!-- <el-form-item label="姓名：" prop="customerName">
            <el-input
              style="width: 203px"
              v-model="listQuery.customerName"
              placeholder=""
            ></el-input>
          </el-form-item> -->

          <!-- <el-form-item label="审课学校：" prop="auditSchool">
            <el-input
              style="width: 203px"
              v-model="listQuery.auditSchool"
              placeholder=""
            ></el-input>
          </el-form-item> -->

          <!-- <el-form-item label="邮箱号：" prop="mail">
            <el-input
              style="width: 203px"
              v-model="listQuery.mail"
              placeholder=""
            ></el-input>
          </el-form-item> -->

          <!-- <el-form-item label="微信号：" prop="wechatNumber">
            <el-input
              style="width: 203px"
              v-model="listQuery.wechatNumber"
              placeholder=""
            ></el-input>
          </el-form-item> -->

          <el-form-item label="归属老师：" prop="teacherId">
            <el-select clearable v-model="listQuery.teacherId">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.nickName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <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
              ? "男"
              : scope.row.sex == 2
              ? "未知"
              : ""
          }}</template>
        </el-table-column>

        <el-table-column label="客户状态" align="center">
          <template slot-scope="scope">{{
            scope.row.userStatus == 0
              ? "未报名"
              : scope.row.userStatus == 1
              ? "在读学员"
              : scope.row.userStatus == 2
              ? "退费学员"
              : scope.row.userStatus == 3
              ? "结课学员"
              : ""
          }}</template>
        </el-table-column>

        <el-table-column label="渠道" align="center">
          <template slot-scope="scope">{{
            scope.row.channel == 1
              ? "转介"
              : scope.row.channel == 2
              ? "活动"
              : ""
          }}</template>
        </el-table-column>

        <el-table-column label="意向度" align="center">
          <template slot-scope="scope">{{
            scope.row.userStatus == 1
              ? "低"
              : scope.row.userStatus == 2
              ? "中"
              : scope.row.userStatus == 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="最近沟通记录" 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="addTime"
        >
          <template slot-scope="scope">{{
            scope.row.studyCareAllocateTime
          }}</template>
        </el-table-column> -->

        <!-- <el-table-column label="选课数量" align="center">
          <template slot-scope="scope">{{
            scope.row.chooseCourseNum
          }}</template>
        </el-table-column> -->

        <!-- <el-table-column label="审课大学" align="center">
          <template slot-scope="scope">{{ scope.row.auditSchool }}</template>
        </el-table-column> -->

        <el-table-column label="分配老师" align="center">
          <template slot-scope="scope">
            <el-select
              clearable
              v-model="scope.row.studyCareId"
              :placeholder="scope.row.customerCareName"
            >
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.nickName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              @click="
                handleUpdateAllocate(scope.row.studyCareId, scope.row.userId)
              "
              size="mini"
              >确认</el-button
            >
          </template>
        </el-table-column>
        <!-- <el-table-column
          label="分配时间"
          :sortable="'custom'"
          align="center"
          prop="allocateTime"
        >
          <template slot-scope="scope">{{ scope.row.allocateTime }}</template>
        </el-table-column> -->
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @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>
  </div>
</template>

<script>
import {
  xfSelectBind,
  kfSelectBind,
  adminList,
  xfUpdateAllocate,
  kfUpdateAllocate
} from "@/api/cso";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};
export default {
  name: "kefuBind",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      pageSize: null,
      pageNum: null,
      total: null,
      listLoading: true,
      multipleSelection: [],
      formLabelWidth: "120px",
      options: [],
      teacherId: "",
      roles: ""
    };
  },
  methods: {
    getData() {
      kfSelectBind(this.listQuery).then(res => {
        // console.log(res, "getData");
        this.listLoading = false;

        this.list = res.data.list;
        this.pageNum = res.data.pageNum;
        this.pageSize = res.data.pageSize;
        this.total = res.data.total;
        // console.log(this.list, "this.list,xfSelectBind");
        this.$forceUpdate();
      });
    },
    getAdminList() {
      adminList(1).then(res => {
        // console.log(res, "getAdminList");
        this.options = res.data;
      });
    },
    handleUpdateAllocate(teacherId, userId) {
      this.$confirm("是否要确认", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        // console.log(teacherId, userId, "teacherId");
        if (teacherId) {
          kfUpdateAllocate({
            teacherId,
            userId
          }).then(res => {
            this.getData();
            this.$forceUpdate();
          });

          // xfUpdateAllocate({
          //   teacherId,
          //   userId
          // }).then(res => {
          //   this.getData();
          //   this.$forceUpdate();
          // });
        } else {
          this.$message({
            type: "error",
            message: "请分配老师",
            duration: 1000
          });
        }
      });
    },
    handleCurrentChange(val) {
      // 翻页
      this.listQuery.pageNum = val;
      this.getData();
    },
    handleSizeChange(val) {
      //切换展示条数
      this.listQuery.pageSize = val;
      this.getData();
    },

    handleStuDetail(id) {
      this.$router.push({
        path: "/pms/stuDetail",
        query: {
          id: id
        }
      });
      // console.log(123);
    },
    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 == "allocateTime") {
        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.getData();
      console.log("changeTableSort");
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSearchList() {
      //搜索
      // this.listQuery.pageNum = 1;
      this.getData();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.getData();
    }
  },

  mounted() {
    this.roles = this.$store.state.user.roles;
    this.getData();
    this.getAdminList();
    // console.log(this.roles.includes("学服部主管"), "学服部主管");
    // console.log(this.roles.includes("客服部主管"), "客服部主管");
  }
};
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
}
.handleStuDetail {
  cursor: pointer;
}
</style>
