<template>
  <div class="manage-container">
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="100px">
        <vab-query-form>
          <el-form-item
            width="100"
            prop="deviceName"
            :label="$t('dataEnquiry.zdmc')"
          >
            <el-input
              v-model="queryForm.deviceName"
              size="small"
              :placeholder="$t('dataEnquiry.qsrzdmc')"
            ></el-input>
          </el-form-item>
          <el-form-item
            width="100"
            prop="inspectorName"
            :label="$t('filling.xjr')"
          >
            <el-input
              v-model="queryForm.inspectorName"
              size="small"
              placeholder="请输入巡检人姓名"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="date" :label="$t('defalult.rq')">
            <el-date-picker
              v-model="queryForm.date"
              size="small"
              type="date"
              :placeholder="$t('defalult.xzrq')"
            ></el-date-picker>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-button">
      <el-button size="small" @click="resetForm()">
        {{ $t('defalult.chongz') }}
      </el-button>
      <el-button type="primary" size="small" @click="search">
        {{ $t('defalult.cx') }}
      </el-button>
      <el-button type="primary" size="small" @click="exportData">
        {{ $t('defalult.dc') }}
      </el-button>
      <el-button type="primary" size="small" @click="deletes">
        {{ $t('defalult.plsc') }}
      </el-button>
    </div>
    <el-table
      :data="tableData"
      stripe
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        fixed
        type="selection"
        width="60"
        align="center"
      ></el-table-column>
      <el-table-column
        fixed
        prop="deviceName"
        :label="$t('dataEnquiry.zdmc')"
        width="350"
        align="center"
      ></el-table-column>
      <el-table-column
        fixed
        prop="time"
        :label="$t('filling.xjsj')"
        width="250"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="inspector"
        :label="$t('filling.xjr')"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column :label="$t('defalult.cz')" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="showDetail(row)">
            {{ $t('defalult.xq') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('defalult.sc') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <filling-detail ref="detail"></filling-detail>
  </div>
</template>

<script>
import { getList, dict, doDelete, doExport } from '@/api/inspection';
import fillingDetail from './detail';
import ajax from '@/api/download';
export default {
  components: {
    fillingDetail,
  },
  data() {
    return {
      tableData: [],
      gridData: [],
      dictData: [],
      dialogTableVisible: true,
      dialogFormVisible: false,
      ids: [],
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: false,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        deviceName: '',
        inspectorName: '',
        date: '',
        projectId: '',
      },
    };
  },
  created() {
    this.queryForm.projectId = sessionStorage.getItem('projectId');
    this.fetchData();
  },
  methods: {
    //搜索
    search() {
      this.fetchData();
    },
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      this.tableData = data.items;
      this.total = data.total;
      this.getDictData();
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    //字典数据
    async getDictData() {
      const { data } = await dict();
      this.dictData = data;
    },
    showDetail(row) {
      this.$refs['detail'].showDetail(row);
    },
    //导出
    async exportData() {
      const url = await doExport(this.queryForm);
      ajax.downloadFile(url, { fileName: '巡检填报.xls' });
    },
    handleSelectionChange(val) {
      const ids = [];
      val.forEach(row => {
        ids.push(row.id);
      });
      this.ids = ids;
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    objectSpanMethod({ rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        if (rowIndex % 4 === 0) {
          return {
            rowspan: 4,
            colspan: 1,
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0,
          };
        }
      }
    },
    deletes() {
      const ids = this.ids;
      console.log(ids);
      if (ids.length == 0) {
        this.$message({
          type: 'info',
          message: '没有选中任何项',
        });
        return false;
      }
      this.deleteData(ids);
    },
    //删除
    deleteData(dis) {
      const that = this;
      this.$confirm('你确定要删除当前项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const { msg } = doDelete(dis);
          this.$message({
            type: 'success',
            message: msg == undefined ? '删除成功' : msg,
          });
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    handleDelete(row) {
      const that = this;
      that.deleteData([row.id]);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
  },
};
</script>

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

<style lang="scss">
.el-submenu__title:hover {
  background-color: rgba(#1890ff, 0.085) !important;
  color: hsla(208, 100%, 55%, 0.95) !important;
}
.el-dialog {
  .el-dialog__header {
    background-color: #1890ff !important;
    padding: 15px 20px;
    text-align: left !important;
    .el-dialog__title {
      color: #e8f4ff !important;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #e8f4ff !important;
    }
  }
  .el-dialog__body {
    padding: 20px !important;
    .el-form-item {
      margin-bottom: 10px !important;
      .el-input {
        width: 100% !important;
      }
      .el-select {
        width: 100% !important;
      }
    }
    .el-form-item:last-child {
      margin-bottom: 20px !important;
    }
  }
}
</style>
