<template>
  <div class="container">
    <el-card 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()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
          @keyup.enter.native="handleSearchList()"
        >
          <!-- <el-form-item label="姓名：">
            <el-input
              style="width: 203px"
              v-model="listQuery.customerName"
              placeholder=""
            ></el-input>
          </el-form-item> -->
          <el-form-item label="校园大使：">
            <!-- <el-input
              style="width: 203px"
              v-model="listQuery.campusAgent"
              placeholder=""
            ></el-input> -->
            <el-select clearable v-model="listQuery.campusAgent">
              <el-option
                v-for="item in ambList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="意向度：">
            <el-select
              clearable
              style="width: 203px"
              v-model="listQuery.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="缴费状态：">
            <el-select
              clearable
              v-model="listQuery.status"
              style="width: 203px"
            >
              <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="招生渠道：">
            <el-select
              clearable
              v-model="listQuery.channel"
              style="width: 203px"
            >
              <el-option label="主动推荐引流" :value="1"></el-option>
              <el-option label="客服自主添加" :value="2"></el-option>
              <el-option label="搜索引擎引流" :value="3"></el-option>
              <el-option label="外部资源推荐" :value="4"></el-option>
              <el-option label="自媒体引流" :value="5"></el-option>
              <el-option label="地推活动" :value="6"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="学校：">
            <el-input
              style="width: 203px"
              v-model="listQuery.school"
              placeholder=""
            ></el-input>
          </el-form-item> -->
          <el-form-item label="所在学校：" prop="school">
            <el-select clearable v-model="listQuery.school">
              <el-option
                v-for="item in schoolList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="年级：">
            <el-select clearable v-model="listQuery.grade" style="width: 203px">
              <el-option label="大学一年级" :value="1"></el-option>
              <el-option label="大学二年级" :value="2"></el-option>
              <el-option label="大学三年级" :value="3"></el-option>
              <el-option label="大学四年级" :value="4"></el-option>
              <el-option label="大学五年级" :value="5"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="专业：">
            <el-input
              style="width: 203px"
              v-model="listQuery.domian"
              placeholder=""
            ></el-input>
          </el-form-item> -->
          <!-- <el-form-item label="邮箱号：">
            <el-input
              style="width: 203px"
              v-model="listQuery.mail"
              placeholder=""
            ></el-input>
          </el-form-item> -->
          <!-- <el-form-item label="微信号：">
            <el-input
              style="width: 203px"
              v-model="listQuery.wechatNumber"
              placeholder=""
            ></el-input>
          </el-form-item> -->
          <el-form-item label="录入时间：">
            <!-- <el-date-picker
            v-model="listQuery.starInputTime"
            style="width: 203px"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker> -->
            <el-date-picker
              v-model="inputTime"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-form-item>
          <!-- <el-form-item label="">
            <el-input style="width: 203px" v-model="listQuery.productSn" placeholder=""></el-input>
            <div style="width: 250px"></div>
          </el-form-item> -->
          <!-- <el-form-item label="最近沟通时间：">
            <el-date-picker
              v-model="chatTime"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-form-item> -->
          <el-form-item label="缴费日期：">
            <el-date-picker
              v-model="paymentTime"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </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.status == 1
              ? "定金"
              : scope.row.status == 2
              ? "补全费用"
              : scope.row.status == 3
              ? "全款"
              : ""
          }}</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.campusAgent }}</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="缴费金额" align="center">
          <template slot-scope="scope">{{ scope.row.costMoney }}</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>{{ multipleSelection }}</div> -->

    <div>共计数据 {{ multipleSelection.length }} 条，共: {{ money }} 元</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 { kpiList, school } from "@/api/cso";
import { getCampusAgent } from "@/api/cso2";

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

export default {
  name: "kpi",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      pageSize: null,
      pageNum: null,
      total: null,
      listLoading: true,
      ambList: [],
      ambListQuery: {},
      multipleSelection: [],
      schoolList: [],
      money: 0,
      formLabelWidth: "120px",
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      inputTime: [],
      chatTime: [],
      paymentTime: []
    };
  },
  watch: {
    inputTime(newValue, oldValue) {
      if (this.inputTime) {
        [
          this.listQuery.starInputTime,
          this.listQuery.endInputTime
        ] = this.inputTime;
      } else {
        this.listQuery.starInputTime = undefined;
        this.listQuery.endInputTime = undefined;
      }
    },
    chatTime(newValue, oldValue) {
      if (this.chatTime) {
        [
          this.listQuery.starChatTime,
          this.listQuery.endChatTime
        ] = this.chatTime;
      } else {
        this.listQuery.starChatTime = undefined;
        this.listQuery.endChatTime = undefined;
      }
    },
    paymentTime(newValue, oldValue) {
      if (this.paymentTime) {
        [
          this.listQuery.starPaymentTime,
          this.listQuery.endPaymentTime
        ] = this.paymentTime;
        [
          this.ambListQuery.starTime,
          this.ambListQuery.endTime
        ] = this.paymentTime;
      } else {
        this.listQuery.starPaymentTime = undefined;
        this.listQuery.endPaymentTime = undefined;
        this.ambListQuery.starTime = undefined;
        this.ambListQuery.endTime = undefined;
      }

      this.getCampusAgentList();
    }
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    }
  },
  methods: {
    getNewStu() {
      kpiList(this.listQuery).then(res => {
        // console.log(res, "getNewStu");
        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");
        this.$forceUpdate();
      });
    },
    handleCurrentChange(val) {
      // 翻页
      this.listQuery.pageNum = val;
      this.getNewStu();
    },
    handleSizeChange(val) {
      //切换展示条数
      this.listQuery.pageSize = val;
      this.getNewStu();
    },
    getSchool() {
      school().then(res => {
        // this.schoolList = res.data;
        //     console.log(res, "school");
        res.data.forEach(item => {
          let obj = {};
          obj.value = item;
          obj.label = item;
          this.schoolList.push(obj);
        });
        //   console.log(this.schoolList, "this.schoolList");
      });
    },
    //校园大使下拉框
    getCampusAgentList() {
      getCampusAgent(this.ambListQuery).then(res => {
        this.ambList = [];
        res.data.forEach(item => {
          let obj = {};
          obj.value = item;
          obj.label = item;
          this.ambList.push(obj);
        });
        this.$forceUpdate();
        // console.log(this.ambList,'校园大使列表');
      });
    },

    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 == "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.getNewStu();
      // console.log("changeTableSort");
    },
    handleSearchList() {
      this.getNewStu();
    },
    handleResetSearch() {
      // 重置
      // this.selectProductCateValue = [];
      this.inputTime = undefined;
      this.chatTime = undefined;
      this.paymentTime = undefined;
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getNewStu();
    },
    // handleCurrentChange(val) {
    //   // 翻页 怎么有两个翻页
    //   this.listQuery.pageNum = val;
    //   this.getNewStu();
    // },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      let arr = [];
      this.multipleSelection.forEach(item => {
        arr.push(item.costMoney);
      });

      if (arr.length > 0) {
        this.money = arr.reduce((a, b) => {
          return a + b;
        });
      } else {
        this.money = 0;
      }
    }
  },
  mounted() {
    this.getNewStu();
    this.getSchool();
    this.getCampusAgentList();
  }
};
</script>

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