<style>
  .makeTable {
    text-align: center;
    overflow: scroll;
  }

  .makeTable th,
  .makeTable td {
    border: 1px solid #d1d1d1;
    height: 40px;
    min-width: 150px;
    background-color: #fff;
  }

  .makeTable tr th {
    height: 40px;
    background-color: rgb(238, 238, 239);
    min-width: 150px;
  }

  .makeTable th:first-child {
    /* td第一个粘性定位 */
    position: sticky;
    left: 0;
  }

  .makeTable td:first-child {
    /* td第一个粘性定位 */
    position: sticky;
    left: 0;
    background: #fff;
  }

  .makeTable th:last-child {
    /* td第一个粘性定位 */
    position: sticky;
    right: 0;
  }

  .makeTable td:last-child {
    /* td第一个粘性定位 */
    position: sticky;
    right: 0;
    background: #fff;
  }

  .makeTable th:nth-last-child(2) {
    /* td第一个粘性定位 */
    position: sticky;
    right: 150px;
  }

  .makeTable td:nth-last-child(2) {
    /* td第一个粘性定位 */
    position: sticky;
    right: 150px;
    background: #fff;
  }

  .makeOutDiv {
    position: relative;
    width: 100%;
    overflow-y: auto;
  }

  .makeTable th:nth-last-child(3) {
    /* td第一个粘性定位 */
    position: sticky;
    right: 300px;
  }

  .makeTable td:nth-last-child(3) {
    /* td第一个粘性定位 */
    position: sticky;
    right: 300px;
    background: #fff;
    border-left: 1px solid #d1d1d1;
    border-right: 1px solid #d1d1d1;
  }

  .makeOutDiv::-webkit-scrollbar {
    width: 3px;
    height: 8px;
    background-color: #F5F5F5;
  }

  /*!*定义滚动条轨道 内阴影+圆角*!*/
  .makeOutDiv::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
    border-radius: 10px;
    background-color: #F5F5F5;
  }

  /*!*定义滑块 内阴影+圆角*!*/
  .makeOutDiv::-webkit-scrollbar-thumb {
    border-radius: 10px;
    -webkit-box-shadow: -webkit-gradient(linear, 0 0, 0 100%, color-stop(.5, rgba(255, 255, 255, .2)), color-stop(.5, transparent), to(transparent));
    background-color: #0ae;
  }

  .Sysuser_Date .el-input__inner {
    background: transparent !important;
    border: 0 !important;
  }

  .Sysuser_Date .el-range-editor .el-range-input {
    background: transparent !important;
  }

  .makeOutDiv .el-table th {
    background: #f5f6f7;
  }
</style>
<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-4 Sysuser_Date">
          <q-field filled>
            <template v-slot:control>
              <el-date-picker v-model="dateList" type="daterange" style="width:100%" :picker-options="pickerOptions0"
                align="right" value-format="yyyy-MM-dd" @change="getData" unlink-panels range-separator="至"
                start-placeholder="开始日期" end-placeholder="结束日期">
              </el-date-picker>
            </template>
          </q-field>
        </div>
        <div class="col-3 Sysuser_Date" v-if="IsShowEmpQuery">
          <q-field filled>
            <template v-slot:control>
              <el-select class="topselect" v-model="msg.empList" @change="getData" style="width:100%;" collapse-tags
                multiple filterable placeholder="请选择">
                <el-option v-for="item in EmployeeList" :key="item.Id" :label="item.EmployeeName" :value="item.Id">
                </el-option>
              </el-select>
            </template>
          </q-field>
        </div>
        <div class="col-3">
          <q-btn color="accent" class="q-mr-md" size="sm" style="margin-top:15px;" icon="download" label="下载"
            @click="downloadMarketChannelStudentStatic" />
        </div>
      </div>
    </div>
    <div class="makeOutDiv">
      <el-table :data="tableData" style="width: 100%" ref="table" :fit='true'
        :default-sort="{prop: 'date', order: 'descending'}" v-loading="loading">
        <template v-for="(item, index) in tableHeader">
          <template v-if="item.label=='员工'">
            <el-table-column :prop="item.prop" :label="item.label" fixed="left" width="70" :key="index">
            </el-table-column>
          </template>
          <template v-if="item.label=='合计回单'||item.label=='合计到访'||item.label=='合计转化率'">
            <el-table-column :prop="item.prop" :label="item.label" fixed="right" sortable width="115" :key="index">
            </el-table-column>
          </template>
          <template v-if="item.SubList&&item.SubList.length>0">
            <template v-if="item.label!='合计'">
              <el-table-column :label="item.label" width="70" :key="index">
                <template v-for="(subItem,subIndex) in item.SubList">
                  <el-table-column :prop="subItem.prop" :label="subItem.label" sortable width="90" :key="subIndex">
                  </el-table-column>
                </template>
              </el-table-column>
            </template>
            <template v-else>
              <el-table-column :label="item.label" :key="index" fixed="right">
                <template v-for="(subItem,subIndex) in item.SubList">
                  <el-table-column :prop="subItem.prop" :label="subItem.label" fixed="right" :key="subIndex">
                  </el-table-column>
                </template>
              </el-table-column>
            </template>
          </template>
        </template>
      </el-table>
    </div>
  </div>
</template>
<script>
  import {
    MarketChannelStudentStatic
  } from '../../api/users/user'
  import {
    queryEmployee
  } from "../../api/users/user";
  import {
    EduDownLoad
  } from "../../api/common/common";
  export default {
    data() {
      return {
        msg: {
          startTime: "",
          endTime: "",
          empList: []
        },
        dateList: [],
        dataList: [],
        EmployeeList: [],
        tableHeader: [],
        tableData: [],
        pickerOptions0: {
          disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6
          }
        },
        IsQueryAuth: false, //是否有查询市场部和课程顾问部人员权限
        IsShowEmpQuery: false, //是否显示员工下拉框
        tableHeight: 50,
        loading:false,
      };
    },
    created() {
      this.CurrentUserInfo = this.getLocalStorage();
      if (this.CurrentUserInfo && this.CurrentUserInfo.ActionMenuList && this.CurrentUserInfo.ActionMenuList.length >
        0) {
        this.CurrentUserInfo.ActionMenuList.forEach((x) => {
          if (x.FunctionCode == "Query_MarketConsultant") {
            this.IsQueryAuth = true;
          }
        });
      }
      if (this.IsQueryAuth || (this.CurrentUserInfo.IsManager == 1 && this.CurrentUserInfo.IsCourseConsultant == 1) || (
          this.CurrentUserInfo.IsManager == 1 && this.CurrentUserInfo.IsMarket == 1)) {
        this.IsShowEmpQuery = true;
      } else {
        this.msg.empList.push(this.CurrentUserInfo.Id);
      }
    },
    mounted() {
      var now = new Date();
      var strLink = "-";
      var year = now.getFullYear();
      var month = now.getMonth() + 1;
      var day = now.getDate();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (day >= 1 && day <= 9) {
        day = "0" + day;
      }
      this.msg.startTime = year + strLink + month + strLink + '01';
      this.msg.endTime = year + strLink + month + strLink + day;
      this.dateList.push(year + strLink + month + strLink + '01');
      this.dateList.push(year + strLink + month + strLink + day);
      this.getEmployeeList();
      this.getData();
      setTimeout(() => {
        this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 60;
      }, 200)
    },
    methods: {
      downloadMarketChannelStudentStatic() {
        var msg = JSON.parse(JSON.stringify(this.msg));
        this.loading=true;
        EduDownLoad(
          "/UserCenter/DownLoadMarketChannelStudentStatic",
          msg,
          "渠道总表.xls",res=>{
            this.loading=false;
          }
        );
      },
      getFixed(index) {
        if (index == 0) {
          return true
        }
      },
      //获取数据
      getData() {
        if (this.dateList && this.dateList.length > 0) {
          this.msg.startTime = this.dateList[0];
          this.msg.endTime = this.dateList[1];
        } else {
          this.msg.startTime = "";
          this.msg.endTime = "";
        }
        this.tableData = [];
        this.tableHeader = [{
          prop: "EmployeeName",
          label: "员工"
        }];
        this.loading=true;
        MarketChannelStudentStatic(this.msg).then(res => {
          this.loading=false;
          if (res.Code == 1) {
            var tempArray = res.Data;
            if (tempArray && tempArray.length > 0) {
              tempArray.forEach((item, index) => {
                if (index == 0) {
                  if (item.subList && item.subList.length > 0) {
                    item.subList.forEach((subItem, subIndex) => {
                      if (subItem.ChannelName == '合计') {
                        this.tableHeader.push({
                          prop: 'ClueCount' + subIndex,
                          label: subItem.ChannelName + "回单"
                        })
                        this.tableHeader.push({
                          prop: 'VisitCount' + subIndex,
                          label: subItem.ChannelName + "到访"
                        })
                        this.tableHeader.push({
                          prop: 'Rates' + subIndex,
                          label: subItem.ChannelName + "转化率"
                        })

                      } else {
                        var subData = {
                          label: subItem.ChannelName,
                          prop: "",
                          SubList: [{
                              prop: 'ClueCount' + subIndex,
                              label: "回单"
                            },
                            {
                              prop: 'VisitCount' + subIndex,
                              label: "到访"
                            },
                            {
                              prop: 'Rates' + subIndex,
                              label: "转化率"
                            },
                          ]
                        };
                        this.tableHeader.push(subData)
                      }
                    })
                  }
                }
                this.tableData.push(item.dicList)
              });
            }
          }
        });
      },
      //获取统计
      getStatical(item) {
        let num = parseFloat(item.VisitCount);
        let total = parseFloat(item.ClueCount);
        if (isNaN(num) || isNaN(total)) {
          return "-";
        }
        return total <= 0 ? "0" : (Math.round(num / total * 10000) / 100.00);
      },
      //获取员工列表
      getEmployeeList() {
        var qMsg = {
          IsLeave: 1,
          Dept_Id: 4,
          UserRole: 1,
        };
        queryEmployee(qMsg).then(res => {
          this.EmployeeList = res.Data;
        });
      },
    }
  }
</script>