<template>
  <div class="manage-container">
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="80px">
        <vab-query-form>
          <el-form-item width="330" prop="type" label="站点">
            <el-cascader
              v-model="queryForm.device"
              :options="deviceData"
              :props="{
                value: 'id',
                label: 'name',
                children: 'deviceList',
              }"
              @change="changeDevice"
              clearable
              filterable
            ></el-cascader>
          </el-form-item>
          <el-form-item width="100" prop="time" label="日期">
            <el-date-picker
              v-model="queryForm.time"
              size="small"
              type="daterange"
              align="right"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              @change="changeTime"
            ></el-date-picker>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-button">
      <el-form-item width="100">
        <el-button size="small" @click="resetForm()">重置</el-button>
        <el-button type="primary" size="small" @click="search">查询</el-button>
      </el-form-item>
    </div>
    <el-table
      id="table"
      v-loading="listLoading"
      :data="tableData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="count"
        label="故障次数"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="fault"
        label="故障"
        align="center"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="details(row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getDate, getPastTime } from '@/common/times';
import { getData, getProjectDeviceTree } from '@/api/operation';
export default {
  data() {
    return {
      tableData: [],
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: false,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        deviceId: '',
        start: '',
        end: '',
        time: '',
        projectId: '',
      },
      deviceData: [],
      form: {
        id: '',
        location: '',
      },
    };
  },
  created() {
    this.fetchData();
    this.getProjectDeviceTree();
  },
  methods: {
    changeTime() {
      this.queryForm.start = getDate(this.queryForm.time[0]);
      this.queryForm.end = getDate(this.queryForm.time[1]);
    },
    // 层级显示站点
    async getProjectDeviceTree() {
      const { data } = await getProjectDeviceTree();
      this.deviceData = data;
    },
    // 选择站点
    changeDevice(e) {
      this.queryForm.projectId = e[0];
      this.queryForm.deviceId = e[1];
    },
    // 轨迹
    details(row) {
      this.$router.push({
        name: '故障详情',
        path: '/operationData',
        query: {
          deviceId: this.queryForm.deviceId,
          projectId: this.queryForm.projectId,
          start: this.queryForm.start,
          end: this.queryForm.end,
        },
      });
      // this.$refs['details'].loadScript(this.queryForm);
    },
    // 获取数据
    async fetchData() {
      const { data } = await getData(this.queryForm);
      this.tableData = data;
    },
    //搜索
    search() {
      this.fetchData(this.queryForm);
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px !important;
}
.el-select {
  width: 200px !important;
}
.manage-button {
  padding-left: 12px;
}
</style>

<style lang="scss">
.el-submenu__title:hover {
  background-color: rgba(#1890ff, 0.085) !important;
  color: hsla(208, 100%, 55%, 0.95) !important;
}
</style>
