<template>
  <div class="container">
    <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="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.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.grade }}</template>
        </el-table-column>
        <el-table-column label="报课数量" align="center">
          <template slot-scope="scope">{{ scope.row.courseNum }}</template>
        </el-table-column>

        <el-table-column
          label="缴费时间"
          :sortable="'custom'"
          align="center"
          prop="addTime"
        >
          <template slot-scope="scope">{{
            scope.row.addTime | formatDateTime
          }}</template>
        </el-table-column>
        <el-table-column
          label="分配时间"
          :sortable="'custom'"
          align="center"
          prop="allocateTime"
        >
          <template slot-scope="scope">{{
            scope.row.allocateTime | formatDateTime
          }}</template>
        </el-table-column>

        <el-table-column label="是否主动添加微信" align="center">
          <template slot-scope="scope">
            <!-- {{ scope.row.isVoluntaryAddWechat }} -->
            <el-switch
              v-model="scope.row.isVoluntaryAddWechat"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="
                changeWxState(scope.row.userId, scope.row.isVoluntaryAddWechat)
              "
            >
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column label="已通过微信" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleWeChatState(scope.row.userId)"
              >确认</el-button
            >
          </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 {
  newAllocate,
  weChatState,
  adminList,
  addWechatStatus
} from "@/api/cso";
import { formatDate } from "@/utils/date";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};

export default {
  name: "newAllocate",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      pageSize: null,
      pageNum: null,
      total: null,
      listLoading: true,
      multipleSelection: [],
      formLabelWidth: "120px",
      teacherList: [],
      roles: "",
      value: false
    };
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      let date = new Date(time);
      // return formatDate(date, "yyyy-MM-dd hh:mm:ss");
      return formatDate(date, "yyyy-MM-dd");
    }
  },
  methods: {
    getData() {
      newAllocate(this.listQuery).then(res => {
        console.log(res.data, "getData");
        this.listLoading = false;

        // res.data.list.forEach(item => {
        //   if (item.isVoluntaryAddWechat == 0) {
        //     item.isVoluntaryAddWechat = true;
        //   }
        //   if (item.isVoluntaryAddWechat == 1) {
        //     item.isVoluntaryAddWechat = false;
        //   }

        // });
        this.list = JSON.parse(JSON.stringify(res.data.list));
        this.list.forEach(item => {
          if (item.isVoluntaryAddWechat === 0) {
            // console.log(item.isVoluntaryAddWechat);
            item.isVoluntaryAddWechat = true;
          }
          if (item.isVoluntaryAddWechat === 1) {
            item.isVoluntaryAddWechat = false;
          }
        });
        this.pageNum = res.data.pageNum;
        this.pageSize = res.data.pageSize;
        this.total = res.data.total;
        this.$store.commit("set_newAllocateNum", res.data.total);
        console.log(this.list, "getData");
        this.$forceUpdate();
      });
    },
    changeWxState(userId, isVoluntaryAddWechat) {
      if (isVoluntaryAddWechat) {
        isVoluntaryAddWechat = 0;
      } else {
        isVoluntaryAddWechat = 1;
      }
      addWechatStatus({
        isVoluntaryAddWechat,
        userId
      });
    },
    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;
    },
    handleWeChatState(id) {
      // console.log(id, "handleWeChatState");
      weChatState(id).then(res => {
        this.$message({
          type: "success",
          message: "确认成功"
        });
        this.getData();
        this.$forceUpdate();
      });
    },
    handleSearchList() {
      //搜索
      this.listQuery.pageNum = 1;

      this.getData();
    },
    handleResetSearch(formName) {
      this.$refs[formName].resetFields();
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getData();
      // 重置
    },
    getTeacherList() {
      adminList(2).then(res => {
        this.teacherList = res.data;
        console.log(this.teacherList, "getTeacherList");
      });
    }
  },
  mounted() {
    this.getData();
    this.getTeacherList();
    this.roles = this.$store.state.user.roles;
  }
};
</script>

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