<template>
  <div class="STBbase-wrapper height100">
    <div class="search-container">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-select v-model="form.orgId" placeholder="请选择所属单位">
            <el-option
              v-for="item in orgOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.status" placeholder="请选择机顶盒状态">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-cascader
            placeholder="请选择区域"
            v-model="form.areaId"
            :options="areaOptions"
            :props="defaultProps"
            change-on-select
          ></el-cascader>
        </el-form-item>
        <el-form-item>
          <div class="btn-group">
            <el-button type="primary" @click="onSearch">查询</el-button>
            <el-button @click="handleReset">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
      <div class="page-tip">
        <span class="page-tip-title">页面说明:</span>
        <span class="page-tips"
          >可按照机顶盒状态及所属单位进行信息筛选。可查看当前系统所有机顶盒运维状态信息。</span
        >
      </div>
    </div>
    <div class="table-content">
        <div class="btn-group">
        <el-button class="export" type="primary" @click="handleExport"
          >导出</el-button
        >
      </div>
      <div class="party-table">
        <el-table
          border
          style="width: 100%; height: 100%"
          height="100%"
          :data="tableData"
        >
          <el-table-column type="index" width="120" label="序号" align="center">
            <template slot-scope="scope">
              <span>{{ (page._index - 1) * 10 + scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="MAC地址"
            prop="mac"
          ></el-table-column>
          <el-table-column
            align="center"
            label="所属单位"
            prop="organName"
          ></el-table-column>
          <el-table-column
            align="center"
            label="单位管理员"
            prop="userName"
          ></el-table-column>
          <el-table-column
            align="center"
            label="所属区域"
            prop="areaName"
          ></el-table-column>
          <el-table-column align="center" label="机顶盒状态" prop="status">
            <template slot-scope="scope">
              <span>{{ statusOptions[scope.row.status - 1].label }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <party-pagination :page="page" @changePage="handleCurrentChange" />
    </div>
  </div>
</template>
<script>
import { partyPagination } from "@/components/index";
import { getAreas } from "@/config/area.js";
import { getOrgListWithOutPage } from "@/config/organ.js";
export default {
  components: { partyPagination },
  data() {
    return {
      page: {
        _index: 1,
        _size: 10,
        total: 0,
      },
      orgOptions: [], // 单位信息
      areaOptions: [], // 区域信息
      defaultProps: {
        label: "name",
        value: "id",
        checkStrictly: true,
      },
      statusOptions: [
        // 状态信息
        {
          value: "1",
          label: "待激活",
        },
        {
          value: "2",
          label: "已激活",
        },
        {
          value: "3",
          label: "故障",
        },
      ],
      form: {
        orgId: "",
        status: "",
        areaId: [],
      },
      tableData: [], // 表格信息
    };
  },
  mounted() {
    this.getAreas();
    this.getOrgList();
    this.onSearch();
  },
  methods: {
    // 获取区域信息
    async getAreas() {
      this.areaOptions = await getAreas();
    },
    // 获取单位信息
    async getOrgList() {
      this.orgOptions = await getOrgListWithOutPage();
    },
    // 查询
    onSearch() {
      this.page._index = 1;
      this.requestForm = JSON.parse(JSON.stringify(this.form))
      this.getTableData();
    },
    // 获得数据接口
    getTableData() {
      let vm = this;
      let param = {
        _index: this.page._index,
        _size: this.page._size,
        areaId: this.requestForm.areaId.length
          ? this.requestForm.areaId[this.requestForm.areaId.length - 1]
          : "",
        organId: this.requestForm.orgId,
        status: this.requestForm.status,
      };
      vm.$https(
        {
          url: "boxOperation/getPageList",
          method: "post",
          authType: this.backToken,
        },
        vm.$qs.stringify(param)
      )
        .then((res) => {
          if (res.data.resultCode === "200") {
            let data = res.data.data;
            vm.page.total = data.total;
            vm.tableData = data.records;
          } else {
            this.$message.error(res.data.message);
          }
        })
        .catch(function (err) {
          console.log(err);
        });
    },
    // 重置
    handleReset() {
      this.form = {
        orgId: "",
        status: "",
        areaId: [],
      };
      this.onSearch();
    },
    // 分页
    handleCurrentChange(val) {
      this.page._index = val;
      this.getTableData();
    },
     // 导出
    handleExport() {
      let dates = {};
      if (this.form.date) {
        dates.startDate = this.requestForm.date[0];
        dates.endDate = this.requestForm.date[1];
      }
      let param = {
        areaId: this.requestForm.areaId.length
          ? this.requestForm.areaId[this.requestForm.areaId.length - 1]
          : "",
        organId: this.requestForm.orgId,
        status: this.requestForm.status,
      };
      this.$https(
        {
          url: "/boxOperation/excel/getPageList",
          method: "post",
          authType: this.backToken,
          responseType: "blob",
        },
        this.$qs.stringify(param)
      ).then((res) => {
        let blobUrl = window.URL.createObjectURL(res.data);
        const aElement = document.createElement("a");
        aElement.href = blobUrl;
        aElement.download = "机顶盒运维信息.xls";
        aElement.click();
        window.URL.revokeObjectURL(blobUrl);
      });
    },
  },
};
</script>
<style lang="less">
@import "~@/style/table.less";
</style>