<template>
  <div class="box-center">
    <div style="
    width: calc(100% - 270px);
    position: fixed;    
    z-index: 999;
    margin-top: -16px;
    background: #ecedf0;
    box-shadow: 0px 0px 10px 0px #dadada;
    ">
      <div class="toptab" style="cursor: pointer" v-if="orgType != 2">
        <div @click="tapclickyab(0)" :class="{ activecllasscet: activeTotal == 0 }"
          :style="{ background: activeTotal == 0 ? '#2671E2' : '#ffffff' }">
          项目
        </div>
        <div v-if="orgType == 0" @click="tapclickyab(1)" :class="{ activecllasscet: activeTotal == 1 }"
          :style="{ background: activeTotal == 1 ? '#2671E2' : '#ffffff' }">
          代理商
        </div>
        <div v-if="orgType == 0 || orgType == 1" @click="tapclickyab(2)" :class="{ activecllasscet: activeTotal == 2 }"
          :style="{ background: activeTotal == 2 ? '#2671E2' : '#ffffff' }">
          公司
        </div>
      </div>
      <!-- 头 -->
      <div class="app-top">
        <div class="app-titel" style="margin-top: 5px">
          <div class="div-lab">
            <div class="label" style="margin-left: 15px">时间：</div>
            <div class="toptimeqhuan">
              <div :class="{ tophove: searchForm.dateType == 4 }" @click="tabtimetap(4)" style="cursor: pointer">
                近7天
              </div>
              <div :class="{ tophove: searchForm.dateType == 5 }" @click="tabtimetap(5)" style="cursor: pointer">
                近15天
              </div>
              <div :class="{ tophove: searchForm.dateType == 6 }" @click="tabtimetap(6)" style="cursor: pointer">
                近30天
              </div>
            </div>
            <div>
              <el-date-picker v-model="time" type="daterange" class="div-inp" range-separator="-"
                start-placeholder="开始日期" :default-time="['00:00:00', '23:59:59']" value-format="yyyy-MM-dd"
                end-placeholder="结束日期" @change="timeChange">
              </el-date-picker>
            </div>
          </div>

          <!--
        <div class="div-lab" v-if="orgType!=2">
          <el-input
            v-model="changeValue"
            clearable
            placeholder="请输入"
            maxlength="10"
            @change="selectChange"
            style="width: 363px"
          >
            <el-select
              v-model="selflag"
              slot="prepend"
              class="input-with-select"
              style="width: 115px"
              placeholder="请选择"
              @change="selChange"
            >
              <el-option label="公司名称" value="1"></el-option>
              <el-option v-if="orgType==0&&activeTotal != 2" label="代理商名称" value="2"></el-option>
              <el-option label="项目名称" value="3"></el-option>
            </el-select>
          </el-input>
        </div> -->

          <div class="div-lab" v-if="orgType == 0">
            <el-input v-model="changeValue" clearable placeholder="请输入" maxlength="10" @change="selectChange"
              style="width: 363px">
              <el-select v-model="selflag" slot="prepend" class="input-with-select" style="width: 115px"
                placeholder="请选择" @change="selChange">
                <el-option v-if="activeTotal != 1" label="公司名称" value="1"></el-option>
                <el-option v-if="activeTotal != 2" label="代理商名称" value="2"></el-option>
                <el-option v-if="activeTotal == 0" label="项目名称" value="3"></el-option>
              </el-select>
            </el-input>
          </div>

          <div class="div-lab" v-if="orgType == 1">
            <el-input v-model="changeValue" clearable placeholder="请输入" maxlength="10" @change="selectChange"
              style="width: 363px">
              <el-select v-model="selflag" slot="prepend" class="input-with-select" style="width: 115px"
                placeholder="请选择" @change="selChange">
                <el-option v-if="activeTotal != 1" label="公司名称" value="1"></el-option>
                <el-option v-if="activeTotal == 0" label="项目名称" value="3"></el-option>
              </el-select>
            </el-input>
          </div>
        </div>
        <div class="app-titel" style="margin-top: 5px">
          <div class="div-lab">
            <div class="label">地区：</div>
            <el-cascader :props="props" @change="locationsChange" placeholder="省/市" size="small" separator="/"
              v-model="provice" clearable></el-cascader>
          </div>
          <!-- <div class="div-lab" v-if="orgType == 0">
          <div class="label" style="min-width:70px;">系统运营：</div>
          <el-input  maxlength="10" clearable v-model="searchForm.linkman"></el-input>
        </div>
        <div class="div-lab" v-if="orgType == 1">
          <div class="label" style="min-width:70px;">售后运营：</div>
          <el-input  maxlength="10" clearable v-model="searchForm.linkman"></el-input>
        </div> -->
          <div v-if="orgType == 0 || orgType == 1" class="div-lab">
            <div class="label">运营人员：</div>
            <el-select v-model="searchForm.operationStaffId" placeholder="请选择" class="div-inp" filterable clearable>
              <el-option v-for="item in operaList" :key="item.accountId" :label="item.name" :value="item.accountId">
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="app-titel" style="margin-top: 15px">
          <div style="margin-left: 20px">
            <el-button type="primary" @click="screen">筛选</el-button>
          </div>
          <div style="margin-left: 20px">
            <el-button @click="clascreen" type="text">清空筛选条件</el-button>
          </div>
          <div style="margin-left: auto; margin-right: 10px" v-if="cus_build_count_download">
            <el-button @click="downLoad" icon="el-icon-download">导出</el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 表格 -->
    <div class="cen-tab" :style="{ marginTop: orgType != 2 ? '230px' : '170px' }">
      <el-table :header-cell-style="{ background: '#F5F7FA', color: '#333333' }" v-if="activeTotal == 0" :data="tableData"
        stripe style="width: 100%" height="640">
        <el-table-column prop="propertyName" label="项目名称" align="center">
        </el-table-column>
        <el-table-column prop="orgName" label="公司名称" align="center">
          <template slot-scope="{ row }">
            <el-tooltip class="item" effect="dark" :content="row.orgName" placement="top-start">
              <span
                style="display: block;width: 100%;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">{{ row.orgName }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="agentName" label="代理商" align="center">
        </el-table-column>
        <el-table-column prop="avgExacutiveRate" label="平均执行率" width="110" align="center" sortable>
          <template slot-scope="{ row }">
            {{ row.avgExacutiveRate || 0 }}%
          </template>
        </el-table-column>
        <!-- <el-table-column prop="axExacutiveRate" label="最高执行率" align="center" sortable width="110">
        </el-table-column>
        <el-table-column prop="noMarkAdviserCount" label="未标顾问" align="center" sortable width="110">
        </el-table-column> -->
        <el-table-column prop="equipmentNum" label="设备总数" align="center" sortable width="100">
        </el-table-column>

        <el-table-column prop="area" label="地区" align="center">
          <template slot-scope="{ row }">
            <el-tooltip class="item" effect="dark" :content="row.area" placement="top-start">
              <span
                style="display: block;width: 100%;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">{{ row.area }}</span>
            </el-tooltip>
          </template>
        </el-table-column>

        <el-table-column v-if="orgType != 2" prop="operationStaffName" label="运营人员" align="center">
          <template slot-scope="{ row }">
            <el-tooltip class="item" effect="dark" :content="row.operationStaffName" placement="top-start">
              <span
                style="display: block;width: 100%;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">{{ row.operationStaffName }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="onlineQuantity" label="在线设备" align="center" sortable width="100">
        </el-table-column>
        <el-table-column prop="noOnlineQuantity" label="离线设备" width="100" align="center" sortable>
        </el-table-column>
        <el-table-column prop="activeQuantity" label="活跃设备" align="center" sortable width="100">
        </el-table-column>
        <el-table-column prop="activeAccount" label="活跃顾问" align="center" sortable width="100">
        </el-table-column>
        <el-table-column prop="sumReception" label="接待量" align="center" sortable>
        </el-table-column>
        <!-- <el-table-column prop="sumActiveCustomer" label="有效接待" align="center" sortable width="110">
        </el-table-column>
        <el-table-column prop="noActiveCustomer" label="无效接待" align="center" sortable width="110">
        </el-table-column> -->
        <el-table-column prop="sumRecordTime" label="接待时长" align="center" sortable width="100">
        </el-table-column>

        <el-table-column prop="sumProhibitedQuantity" label="违禁次数" width="100" align="center" sortable>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="100">
          <template slot-scope="{ row }">
            <el-button type="text" v-if="cus_build_area_record" @click="goEquiment(row)">设备使用统计</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-table :header-cell-style="{ background: '#F5F7FA', color: '#333333' }" v-if="activeTotal == 2" :data="tableData"
        stripe style="width: 100%" height="640">
        <el-table-column prop="orgName" label="公司名称" align="center">
        </el-table-column>
        <el-table-column prop="houseQuantityOfEnough" label="服务期内项目" align="center" width="140">
        </el-table-column>
        <el-table-column prop="avgExacutiveRate" label="平均执行率" align="center" width="110" sortable>
          <template slot-scope="{ row }">
            {{ row.avgExacutiveRate || 0 }}%
          </template>
        </el-table-column>
        <!-- <el-table-column prop="axExacutiveRate" label="最高执行率" align="center" sortable width="110">
        </el-table-column>
        <el-table-column prop="noMarkAdviserCount" label="未标顾问" align="center" sortable width="110">
        </el-table-column> -->
        <el-table-column prop="onlineQuantity" label="在线设备" align="center" sortable width="110">
        </el-table-column>
        <el-table-column prop="area" label="地区" align="center">
        </el-table-column>
        <el-table-column prop="operationStaffName" label="运营人员" align="center">
        </el-table-column>
        <el-table-column prop="equipmentNum" label="设备总数" align="center" sortable width="110">
        </el-table-column>
        <el-table-column prop="noOnlineQuantity" label="离线设备" width="100" align="center" sortable>
        </el-table-column>
        <el-table-column prop="activeQuantity" label="活跃设备" align="center" sortable width="110">
        </el-table-column>
        <el-table-column prop="activeAccount" label="活跃顾问" align="center" sortable width="110">
        </el-table-column>
        <el-table-column prop="sumReception" label="接待量" align="center" sortable width="110">
        </el-table-column>
        <!-- <el-table-column prop="sumActiveCustomer" label="有效接待" align="center" sortable width="110">
        </el-table-column>
        <el-table-column prop="noActiveCustomer" label="无效接待" align="center" sortable width="110">
        </el-table-column> -->
        <el-table-column prop="sumRecordTime" label="接待时长" align="center" sortable width="110">
        </el-table-column>

        <el-table-column prop="sumProhibitedQuantity" label="违禁次数" align="center" sortable width="110">
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="100">
          <template slot-scope="{ row }">
            <el-button type="text" @click="goDetail(row, 2)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-table :header-cell-style="{ background: '#F5F7FA', color: '#333333' }" v-if="activeTotal == 1" :data="tableData"
        stripe style="width: 100%" height="640">
        <el-table-column prop="agentName" label="代理商名称" align="center">
        </el-table-column>
        <el-table-column prop="area" label="地区" align="center">
        </el-table-column>
        <el-table-column prop="avgExacutiveRate" label="平均执行率" align="center" width="140" sortable>
          <template slot-scope="{ row }">
            {{ row.avgExacutiveRate || 0 }}%
          </template>
        </el-table-column>
        <!-- <el-table-column prop="axExacutiveRate" label="最高执行率" align="center" sortable width="110">
        </el-table-column>
        <el-table-column prop="noMarkAdviserCount" label="未标顾问" align="center" sortable width="110">
        </el-table-column> -->
        <el-table-column prop="operationStaffName" label="运营人员" align="center">
        </el-table-column>
        <el-table-column prop="houseQuantityOfEnough" label="服务期内项目" align="center" width="150" sortable>
        </el-table-column>
        <el-table-column prop="sumReception" label="接待量" align="center" sortable width="110">
        </el-table-column>
        <!-- <el-table-column prop="sumActiveCustomer" label="有效接待" align="center" sortable width="110">
        </el-table-column>
        <el-table-column prop="noActiveCustomer" label="无效接待" align="center" sortable width="110">
        </el-table-column> -->
        <el-table-column prop="equipmentNum" label="设备总数" align="center" sortable width="110">
        </el-table-column>
        <el-table-column prop="sumRecordTime" label="接待时长" align="center" sortable width="110">
        </el-table-column>

        <el-table-column prop="sumProhibitedQuantity" label="违禁次数" align="center" width="110" sortable>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="100">
          <template slot-scope="{ row }">
            <el-button type="text" @click="goDetail(row, 1)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex; justify-content: flex-end; margin-top: 10px">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
          :page-sizes="[10, 30, 50]" :page-size="size" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getAreaList } from "@/api/modules/api";
export default {
  data() {
    return {
      props: {
        lazy: true,
        checkStrictly: true,
        expandTrigger: "hover",
        async lazyLoad(node, resolve) {
          const { level } = node;
          if (level == 0) {
            // console.log(23);
            const { data } = await getAreaList({ parentId: 0 }); //获取省接口
            var nodes = data.map((item) => {
              return {
                value: item.id,
                label: item.name,
                leaf: false,
              };
            });
            resolve(nodes);
          } else if (level == 1) {
            const { data } = await getAreaList({ parentId: node.data.value }); //获取市接口
            var nodes = data.map((item) => {
              return {
                value: item.id,
                label: item.name,
                leaf: true,
              };
            });
            resolve(nodes);
          } else {
            resolve();
          }
        },
      },
      activeTotal: "0",
      provice: [],
      value: "",
      input: "",
      tableData: [],
      multipleSelection: [],
      changeValue: "",
      dialogVisible: false,
      houseList: [],
      time: [],
      orgType: localStorage.getItem("orgType"),
      searchForm: {
        propertyName: "",
        provinceId: "",
        cityId: "",
        orgName: "",
        agentName: "",
        userName: "",
        dateType: "4",
        startDate: "",
        endDate: "",
        operationStaffId: "",
      },
      currentPage: 1,
      selflag: "1",
      size: 30,
      total: 10,
      operaList: [],
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  created() {
    this.cus_build_area_record = this.permissions["cus_build_area_record"];
    this.cus_build_count_download =
      this.permissions["cus_build_count_download"];
  },
  mounted() {
    if (this.orgType == 1) {
      this.selflag = "3";
    }
    // 获取项目列表
    this.tabtimetap(4);
    this.getOperaList();
    this.houseStatistics();
  },
  methods: {
    downLoad() {
      let obj = {
        ...this.searchForm,
        agentId: localStorage.getItem("agentId"),
        orgCode: localStorage.getItem("orgCode"),
        houseId: localStorage.getItem("houseId"),
        orgType: localStorage.getItem("orgType"),
      };
      if (this.activeTotal == 0) {
        if (this.orgType == 0) {
          obj.operationType = 1;
        } else if (this.orgType == 1) {
          obj.operationType = 2;
        } else {
          obj.operationType = null;
        }
        // 项目
        this.exportMethodPost(
          "/autoSR/cusStageStatistics/houseStatisticsExport",
          "项目数据",
          obj
        );
      } else if (this.activeTotal == 1) {
        obj.operationType = null;
        // 代理商
        this.exportMethodPost(
          "/autoSR/cusStageStatistics/agentStatisticsExport",
          "代理商",
          obj
        );
      } else {
        if (this.orgType == 0) {
          obj.operationType = 1;
        } else if (this.orgType == 1) {
          obj.operationType = 2;
        } else {
          obj.operationType = null;
        }
        this.exportMethodPost(
          "/autoSR/cusStageStatistics/companyStatisticsExport",
          "公司",
          obj
        );
      }
    },
    // 导出.Excel公用方法
    exportMethodPost(url, name, data = {}) {
      axios({
        method: "get",
        url: url,
        params: data,
        responseType: "blob",
      })
        .then((res) => {
          let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          let date = new Date();
          let time = date.toLocaleDateString();
          // console.log(time, "时间");
          if ("download" in document.createElement("a")) {
            const link = document.createElement("a");
            link.style.display = "none";
            link.href = URL.createObjectURL(blob);
            // link.download = res.headers['content-disposition'] //下载后文件名
            link.download = (name || "导出文件") + time + ".xlsx"; //下载的文件名
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
          } else {
            // console.log("--------------------jingla")
            let fileName = (name || "导出文件") + time + ".xlsx"; //下载的文件名
            navigator.msSaveBlob(blob, fileName);
          }
        })
        .catch((error) => {
          // Message.error({
          //     message: '网络连接错误'
          //  })
          console.log(error);
        });
    },
    getOperaList() {
      if (this.orgType == 1) {
        this.$api.http
          .getAllOperationsStaffByAgent({
            agentId: localStorage.getItem("agentId"),
          })
          .then((res) => {
            this.operaList = res.data;
          });
      } else {
        this.$api.http.getAllOperationsStaff().then((res) => {
          this.operaList = res.data;
        });
      }
    },
    selectChange(e) {
      this.searchForm.equipImei = "";
      this.searchForm.userName = "";
      if (!e) retun;
      if (this.selflag == 1) {
        this.searchForm.orgName = e;
      }
      if (this.selflag == 2) {
        this.searchForm.agentName = e;
      }
      if (this.selflag == 3) {
        this.searchForm.propertyName = e;
      }
    },
    goEquiment(row) {
      console.log(row);
      this.$router.push({
        path: "/Equipment/equipmentDetailList",
        query: {
          houseId: row.id,
          startDate: this.searchForm.startDate,
          endDate: this.searchForm.endDate,
          timeType: this.searchForm.dateType,
        },
      });
    },
    goDetail(row, i) {
      this.activeTotal = 0;
      console.log(row);
      if (i == 1) {
        // 代理商
        this.selflag = "2";
        this.changeValue = row.agentName;
        this.searchForm.agentName = row.agentName;
        this.houseStatistics();
      } else {
        // 公司
        this.selflag = "1";
        this.changeValue = row.orgName;
        this.searchForm.orgName = row.orgName;
        this.houseStatistics();
      }
    },
    tapclickyab(i) {
      this.activeTotal = i;
      if (this.orgType == 0) {
        if (i == 1) {
          this.selflag = "2";
        } else if (i == 2) {
          this.selflag = "1";
        }
      } else if (this.orgType == 1) {
        if (i == 2) {
          this.selflag = "1";
        }
      }
      this.clascreen();
      // this.houseStatistics();
    },
    timeChange(e) {
      this.searchForm.dateType = null;
      this.searchForm.startDate = e[0];
      this.searchForm.endDate = e[1];
      this.houseStatistics();
    },
    tabtimetap(idx) {
      this.searchForm.startDate = "";
      this.searchForm.endDate = "";
      this.searchForm.dateType = idx;
      this.time = [];
      this.houseStatistics();
    },
    locationsChange(e) {
      console.log(e);
      if (!e) return;
      this.searchForm.provinceId = e[0];
      this.searchForm.cityId = e[1];
    },
    selChange() {
      this.changeValue = "";
      this.searchForm.agentName = "";
      this.searchForm.userName = "";
      this.searchForm.orgName = "";
    },
    screen() {
      this.currentPage = 1;
      console.log(this.searchForm);
      this.houseStatistics();
    },
    clascreen() {
      this.time = [];
      this.provice = [];
      this.changeValue = "";
      this.searchForm = {
        propertyName: "",
        provinceId: "",
        cityId: "",
        orgName: "",
        agentName: "",
        userName: "",
        dateType: "4",
        startDate: "",
        endDate: "",
      };
      this.currentPage = 1;
      this.houseStatistics();
    },
    houseStatistics() {
      let obj = {
        current: this.currentPage,
        size: this.size,
        ...this.searchForm,
        agentId: localStorage.getItem("agentId"),
        orgCode: localStorage.getItem("orgCode"),
        houseId: localStorage.getItem("houseId"),
        orgType: localStorage.getItem("orgType"),
      };
      if (this.activeTotal == 0) {
        if (this.orgType == 0) {
          obj.operationType = 1;
        } else if (this.orgType == 1) {
          obj.operationType = 2;
        } else {
          obj.operationType = null;
        }
        // 项目
        this.$api.api.houseStatistics1(obj).then((res) => {
          // console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
        });
      } else if (this.activeTotal == 1) {
        obj.operationType = null;
        // 代理商
        this.$api.api.agentStatistics(obj).then((res) => {
          // console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
        });
      } else {
        if (this.orgType == 0) {
          obj.operationType = 1;
        } else if (this.orgType == 1) {
          obj.operationType = 2;
        } else {
          obj.operationType = null;
        }
        // 公司
        this.$api.api.companyStatistics(obj).then((res) => {
          // console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
        });
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.houseStatistics();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.houseStatistics();
    },
  },
};
</script>

<style scoped="scoped" lang="scss" >
.box-center {
  width: 100%;
  padding: 15px;
  min-width: 1000px;
  padding-bottom: 100px;
}

.toptab {
  width: 100%;
  display: flex;
  margin-bottom: 20px;
}

.toptab div {
  padding-left: 30px;
  padding-right: 30px;
  padding-top: 10px;
  padding-bottom: 10px;
  font-size: 14px;
}

.activecllasscet {
  color: #ffffff;
}

.cen-tab {
  width: 100%;
  padding: 15px;
  background: #ffffff;
  margin-top: 15px;
}

.tophove {
  color: #ffffff;
  background: #2671e2;
}

.app-top {
  width: 100%;
  background: #ffffff;
  box-shadow: 0px 0px 12px 0px rgba(0, 0, 0, 0.04);
  border-radius: 4px;
  padding-top: 15px;
  padding-bottom: 15px;

  .app-titel {
    width: 100%;
    display: flex;
    align-items: center;
    flex-wrap: wrap;

    .label {
      font-size: 14px;
      font-weight: 400;
      color: #32363d;
      line-height: 32px;
      margin-left: 15px;
      // min-width: 100px;
    }

    .toptimeqhuan {
      height: 30px;
      background: #ffffff;
      display: flex;
      align-items: center;
    }

    .toptimeqhuan div {
      padding-left: 20px;
      padding-right: 20px;
      text-align: center;
      line-height: 30px;
      font-size: 14px;
      margin-right: 15px;
      border-radius: 4px;
      border: 1px solid #e0e0e0;
    }
  }
}

.div-lab {
  display: flex;
  margin: 5px;
}

.div-inp {
  width: 250px;
}

/deep/ .el-table__header-wrapper {
  thead {
    tr {
      th {
        background: #F5F7FA;
        color: #333333;
      }
    }
  }
}

/deep/ .el-button--primary {
  background: #2671E2 !important;
  border: 1px solid #2671E2 !important;
}

/deep/ .el-button--text {
  color: #2671E2;
}
</style>
