<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <div class="col-3">
          <q-input @keyup.enter.native="resetSearch" dense clearable filled v-model="msg.StuName" label="昵称"
            maxlength="20" />
        </div>
        <div class="col-3">
          <q-input @keyup.enter.native="resetSearch" dense clearable filled v-model="msg.StuTel" label="电话"
            maxlength="20" />
        </div>
        <div class="col-3">
          <q-input @keyup.enter.native="resetSearch" dense clearable filled v-model="msg.QQ" label="QQ"
            maxlength="20" />
        </div>
        <div class="col-3">
          <q-input @keyup.enter.native="resetSearch" dense clearable filled v-model="msg.WeChatNo" label="微信号"
            maxlength="20" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" filled v-model="msg.StuType" dense clearable :options="customTypeList"
            option-label="Name" option-value="Id" emit-value map-options label="客户类型" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" dense filled v-model="msg.StuStage" :options="customState" option-label="Name"
            option-value="Id" emit-value map-options label="客户进度" clearable />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" dense v-model="msg.CustomerId" :options="myCustomerList" filled use-input
            label="同行" option-label="CustomerName" option-value="CustomerId" ref="CustomerName" class="col-6" emit-value
            map-options @filter="cusfilterFn">
            <template v-slot:no-option>
              <q-item>
                <q-item-section class="text-grey">
                  未找到相关数据
                </q-item-section>
              </q-item>
            </template>
          </q-select>
        </div>
        <div class="col-3">
          <q-select @input="resetBelong" dense filled option-value="Id" option-label="Name" v-model="msg.BelongType"
            :options="BelongTypeList" emit-value map-options label="归属类型" />
        </div>
        <div class="col-3">
          <q-field filled dense>
            <template v-slot:control>
              <el-date-picker v-model="dateArray" size="mini" @change="resetSearch" value-format="yyyy-MM-dd"
                type="daterange" style="border:none;" range-separator="至" start-placeholder="开始时间"
                end-placeholder="结束时间" />
            </template>
          </q-field>
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" dense filled clearable use-input option-value="Id" option-label="Name"
            v-model="msg.ExistCourseConsultant" :options="consultantList" emit-value map-options label="是否推送程顾问" />
        </div>
        <div class="col-3" v-if="msg.BelongType == 4">
          <q-select @input="resetSearch" dense clearable @filter="filterSubord" use-input filled option-value="Id"
            option-label="EmployeeName" v-model="msg.CreateIds" multiple :options="SubordList" emit-value map-options
            label="我下属的" />
        </div>
        <div class="col-3">
          <q-field filled label="客户状态" dense stack-label>
            <template v-slot:control>
              <q-radio dense v-model="msg.QStudentStatus" @input="resetSearch" :val="1" label="有效" size="xs" />
              &nbsp;&nbsp;
              <q-radio dense v-model="msg.QStudentStatus" @input="resetSearch" :val="2" label="无效" size="xs" />
            </template>
          </q-field>
        </div>
        <div class="col-3">
          <q-select filled stack-label option-value="Id" @input="resetSearch" dense option-label="Name"
            v-model="msg.AdvisorStatus" ref="AdvisorStatus" :options="consultList" clearable label="当前状态"
            class="col-6 q-pb-lg" emit-value map-options />
        </div>
      </div>
    </div>
    <div class="page-content">
      <stulist :dataList="data" @reload="getStudent" @success="refreshPage" :loading="loading" ref="stuList"
        :isJudgeTrans="isJudgeTrans" :Count="Count">
      </stulist>
      <div class="row" style="justify-content: flex-end;padding: 5px 20px">
        <q-pagination v-model="msg.pageIndex" :max="pageCount" @input="changePage" class="full-width justify-end"
          color="primary" :input="true">
        </q-pagination>
      </div>
    </div>
  </div>
</template>
<script>
  import {
    getStudentPage,
    getSchoolDropdown,
    deleteStudent,
    createStudentAccount,
    queryStuStageList,
    GetStudentTypeList,
    GetPersonalDimension
  } from "../../api/school/index";
  import {
    queryEmployee
  } from "../../api/users/user";
  import {
    resetPassword
  } from "../../api/users/user.js";

  import stulist from "../../components/school/student/stulist";
  import {
    GetStudentAdvisorConfigList
  } from "../../api/customerstudent/customerstudent";

  import {
    getMyCustomerList,
    CreateTypeList,
    GetStuChannelList
  } from "../../api/sale/sale";
  import {
    mapGetters
  } from "vuex";
  export default {
    meta: {
      title: "我的学员"
    },
    components: {
      stulist
    },
    data() {
      return {
        Count:0,
        currentUrl: "",
        data: [],
        loading: true,
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          BelongType: 1,
          StuName: "",
          StuTel: "",
          Status: "-1",
          CreateBy: "",
          IsQueryMyStu: 1,
          StuStage: "", //客户阶段
          StartTime: "", //开始时间
          EndTime: "", //结束时间
          CustomerId: 0, //同行联系人编号
          CategoryId: "", //同行编号
          QQ: "",
          WeChatNo: "",
          StuType: "",
          CreateIds: [],
          ExistCourseConsultant: "", //是否存在课程顾问(1-存在，2=不存在)
          QStudentStatus: 1, //客户状态 1有效 2无效
          AdvisorStatus: '', //当前状态
          StuChannel: '',
        },
        dateArray: [], //日期数组
        pageCount: 0,
        //客户阶段状态列表
        customState: [],
        myCustomerList: [], //同行列表
        allCustomerList: [], //所有同行列表
        customTypeList: [], //客户类型
        //归属类型列表
        BelongTypeList: [{
            Id: 1,
            Name: "全部"
          },
          {
            Id: 2,
            Name: "我负责的"
          },
          {
            Id: 3,
            Name: "我协同的"
          },
          {
            Id: 4,
            Name: "我下属的"
          }
        ],
        //课程顾问
        consultantList: [{
            Id: 1,
            Name: "是"
          },
          {
            Id: 2,
            Name: "否"
          }
        ],
        isJudgeTrans: 1, //用于判断转交是否有条件
        SubordList: [], //我的下属数据
        AllSubordList: [],
        customFromList: [], //客户来源
        StuChannelList: [], //收客渠道
        allStuChannelList: [], //所有收客渠道
        consultList: [],
        CurrentUserInfo: {},
      };
    },
    created() {
      this.CurrentUserInfo = this.getLocalStorage();
      this.getSchool();
      this.getStuStageList();
      this.getCustomTypeList();
      this.GetSubordList();
    },
    mounted() {
      this.currentUrl = this.$route.path;
      if (this.$route.query.startTime) {
        this.dateArray.push(this.$route.query.startTime);
        this.msg.StartTime = this.$route.query.startTime;
      }
      if (this.$route.query.endTime) {
        this.dateArray.push(this.$route.query.endTime);
        this.msg.EndTime = this.$route.query.endTime;
      }
      if (this.$route.query.ChannelId) {
        this.msg.StuChannel = Number(this.$route.query.ChannelId);
      }
      if (this.$route.query.CategoryId) {
        this.msg.CategoryId = this.$route.query.CategoryId;
      }
      if (this.$route.query.createBy) {
        if (this.CurrentUserInfo.IsManager == 1 && this.CurrentUserInfo.IsMarket == 1) {
          this.msg.BelongType = 4;
          this.msg.CreateIds.push(Number(this.$route.query.createBy));
        }
      }
      this.getCustomerList();
      this.getCustomFrom();
      this.GetStuChannelList();
      this.getStatusList();
      this.getStudent();
    },
    computed: {
      ...mapGetters(["userInfo"])
    },

    methods: {
      //获取跟进状态下拉
      getStatusList() {
        let msg = {
          Name: ''
        }
        GetStudentAdvisorConfigList(msg).then(res => {
          if (res.Code == 1) {
            this.consultList = res.Data;
          }
        })
      },
      //获取收客渠道
      GetStuChannelList() {
        GetStuChannelList({}).then(res => {
          if (res.Code == 1) {
            this.StuChannelList = res.Data;
            this.allStuChannelList = res.Data;
          }
        });
      },
      //筛选渠道
      filterStuChannel(val, update) {
        update(() => {
          if (val === "") {
            this.StuChannelList = JSON.parse(
              JSON.stringify(this.allStuChannelList)
            );
          } else {
            const needle = val.toLowerCase();
            this.StuChannelList = this.allStuChannelList.filter(
              v => v.Name.toLowerCase().indexOf(needle) > -1
            );
          }
        });
      },
      //获取客户来源
      getCustomFrom() {
        CreateTypeList({}).then(res => {
          if (res.Code == 1) {
            this.customFromList = res.Data;
          }
        });
      },
      //获取我下属的下拉数据
      GetSubordList() {
        GetPersonalDimension().then(res => {
          if (res.Code == 1) {
            this.SubordList = res.Data;
            this.AllSubordList = res.Data;
          }
        });
      },
      //筛选转介人
      filterSubord(val, update, abort) {
        update(() => {
          this.SubordList = this.AllSubordList.filter(
            v => v.EmployeeName.indexOf(val) > -1
          );
        });
      },
      //获取客户阶段列表
      getStuStageList() {
        queryStuStageList().then(res => {
          this.customState = res.Data;
        });
      },
      //获取客户类型
      getCustomTypeList() {
        GetStudentTypeList().then(res => {
          if (res.Code == 1) {
            this.customTypeList = res.Data;
          }
        });
      },
      resetSearch() {
        this.msg.pageIndex = 1;
        this.getStudent();
      },
      resetBelong() {
        if (this.msg.BelongType != 4) {
          this.msg.pageIndex = 1;
          this.getStudent();
        }
      },
      changePage(val) {
        this.msg.pageIndex = val;
        this.getStudent();
      },
      getSchool() {
        getSchoolDropdown({}).then(res => {
          this.schoolList = res.Data;
        });
      },
      getStudent() {
        if (this.dateArray && this.dateArray.length > 0) {
          this.msg.StartTime = this.dateArray[0];
          this.msg.EndTime = this.dateArray[1];
        } else {
          this.msg.StartTime = "";
          this.msg.EndTime = "";
        }

        this.loading = true;
        getStudentPage(this.msg)
          .then(res => {
            this.loading = false;
            this.data = res.Data.PageData;
            this.pageCount = res.Data.PageCount;
            this.Count = res.Data.Count
            this.data.forEach(x => {
              if (x.CreateBy != this.userInfo.Id) {
                x.BelongType = 3;
              } else {
                x.BelongType = 1;
              }
            });
          })
          .catch(() => {
            this.loading = false;
          });
      },
      refreshPage() {
        this.getStudent();
      },
      getCustomerList() {
        getMyCustomerList({}).then(res => {
          this.myCustomerList = res.Data;
          this.allCustomerList = res.Data;
          this.myCustomerList.unshift({
            CustomerName: "不限",
            CustomerId: 0
          });
        });
      },
      cusfilterFn(val, update) {
        update(() => {
          if (val === "") {
            this.myCustomerList = JSON.parse(
              JSON.stringify(this.allCustomerList)
            );
          } else {
            const needle = val.toLowerCase();
            this.myCustomerList = this.allCustomerList.filter(
              v => v.CustomerName.toLowerCase().indexOf(needle) > -1
            );
          }
        });
      }
    }
  };
</script>
<style lang="sass">
  @import url('~assets/css/table.sass')
</style>
<style scoped>
  /deep/.el-input__inner,
  /deep/.el-range-input {
    background-color: transparent;
    border: none;
  }
</style>
