<template>
  <div class="container">
    <!-- <h1>newClew</h1>
    <h1>待沟通线索</h1> -->
    <el-card
      v-if="
        roles.includes('客服部主管') ||
          roles.includes('学服部主管') ||
          roles.includes('超级管理员')
      "
      class="filter-container"
      shadow="never"
    >
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch('listQuery')"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          ref="listQuery"
          size="small"
          label-width="140px"
          @keyup.enter.native="handleSearchList()"
        >
          <el-form-item label="归属老师：">
            <el-select
              clearable
              style="width: 203px"
              v-model="listQuery.teacherId"
            >
              <el-option
                v-for="item in teacherList"
                :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="stuList"
        :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">
            <span v-if="scope.row.userStatus == 0">未报名</span>
            <span v-if="scope.row.userStatus == 1">在读学员</span>
            <span v-if="scope.row.userStatus == 2">退费学员</span>
            <span v-if="scope.row.userStatus == 3">结课学员</span>
          </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.addTime }}</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="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleAddRecord(scope.row.userId)"
              >添加沟通记录
            </el-button>
          </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="nextChatTime"
        >
          <template slot-scope="scope">{{ scope.row.nextChatTime }}</template>
        </el-table-column> -->
      </el-table>
      <el-dialog
        title="添加沟通记录"
        :visible.sync="recordVisible"
        @close="reset"
      >
        <el-form :model="chatRecord" ref="addRecord">
          <el-form-item label="意向度" :label-width="formLabelWidth">
            <el-select clearable v-model="chatRecord.planStatus">
              <el-option label="低" :value="1"></el-option>
              <el-option label="中" :value="2"></el-option>
              <el-option label="高" :value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="下次沟通时间" :label-width="formLabelWidth">
            <el-date-picker
              v-model="chatRecord.nextChatTime"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="沟通记录" :label-width="formLabelWidth">
            <el-input
              type="textarea"
              :rows="8"
              placeholder="请输入内容"
              v-model="chatRecord.context"
            >
            </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="recordVisible = false">取 消</el-button>
          <el-button type="primary" @click="updatedChatRecord">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- <el-button @click="test">ttt</el-button> -->
    <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 { newAddClew, addChatRecord, adminList } from "@/api/cso";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};
export default {
  name: "newClew",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      pageSize: null,
      pageNum: null,
      total: null,
      listLoading: true,
      multipleSelection: [],
      recordVisible: false,
      roles: "",
      chatRecord: {
        planStatus: "",
        nextChatTime: "",
        context: "",
        userId: ""
      },
      formLabelWidth: "120px",
      teacherList: []
    };
  },
  methods: {
    getNewAddClew() {
      newAddClew(this.listQuery).then(res => {
        // console.log(res, "getNewAddClew");
        this.listLoading = false;

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

        console.log(this.list, "this.list");
        this.$forceUpdate();
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleCurrentChange(val) {
      // 翻页
      this.listQuery.pageNum = val;
      this.getNewAddClew();
    },
    handleSizeChange(val) {
      //切换展示条数
      this.listQuery.pageSize = val;
      this.getNewAddClew();
    },
    handleStuDetail(id) {
      // 测试用的
      this.$router.push({
        path: "/pms/stuDetail",
        query: {
          id: id
        }
      });
      // console.log(123);
    },
    handleAddRecord(id) {
      this.chatRecord = {};
      // this.chatRecord.userId = "";
      this.recordVisible = true;
      // console.log(id);
      this.chatRecord.userId = id;
      // console.log(this.chatRecord.userId, "this.chatRecord.userId");
    },
    updatedChatRecord() {
      this.recordVisible = false;
      console.log(this.chatRecord);
      this.$confirm("是否提交", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        addChatRecord(this.chatRecord).then(response => {
          this.$message({
            type: "success",
            message: "提交成功",
            duration: 1000
          });
          // location.reload();
          this.getNewAddClew();
          this.$refs["addRecord"].resetFields();
          // console.log(this.$refs,'this.$refs');
        });
      });
    },
    reset() {
      // console.log(this.$refs["addRecord"], "this.$refs[formName]");
      // this.$refs["addRecord"]
      this.$refs["addRecord"].resetFields();
      // console.log("reset");
    },
    handleSearchList() {
      //搜索
      this.listQuery.pageNum = 1;

      this.getNewAddClew();
    },
    handleResetSearch(formName) {
      this.$refs[formName].resetFields();
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getNewAddClew();
      // 重置
    },
    getTeacherList() {
      adminList(1).then(res => {
        this.teacherList = res.data;
        console.log(this.teacherList, "getTeacherList");
      });
    },
    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.getNewAddClew();
      // console.log("changeTableSort");
    }
  },
  mounted() {
    this.roles = this.$store.state.user.roles;
    this.getNewAddClew();
    this.getTeacherList();

    console.log(this.roles, "this.roles");
    // console.log(this.$refs["newClewf"], "this.$refs.newClewf");
  }
};
</script>

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