<template>
  <div class="manage-container">
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="120px">
        <vab-query-form>
          <el-form-item 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 prop="equipmentName" :label="$t('maintenance.sbmc')">
            <el-input
              v-model="queryForm.equipmentName"
              size="small"
              :placeholder="$t('maintenance.qsrsbmc')"
            ></el-input>
          </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="handleAdd">
        {{ $t('defalult.tj') }}
      </el-button>
      <el-button type="primary" size="small" @click="exportData">
        {{ $t('defalult.dc') }}
      </el-button>
    </div>
  </div>
  <el-table
    v-loading="listLoading"
    :data="tableData"
    stripe
    border
    style="width: 100%"
  >
    <el-table-column
      fixed
      prop="device.name"
      :label="$t('dataEnquiry.zdmc')"
      width="150"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="name.dataValue"
      :label="$t('maintenance.sb')"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="brand.dataValue"
      :label="$t('maintenance.pp')"
      width="150"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="supplier.dataValue"
      :label="$t('maintenance.gys')"
      width="150"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="unit.dataValue"
      :label="$t('sensor.dw')"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="model"
      :label="$t('maintenance.xh')"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="code"
      :label="$t('maintenance.bh')"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="power"
      :label="$t('maintenance.gl')"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="status.dataValue"
      :label="$t('dataEnquiry.zt')"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="shelfLife"
      :label="$t('maintenance.bzq')"
      align="center"
    ></el-table-column>
    <el-table-column
      fixed="right"
      :label="$t('defalult.cz')"
      align="center"
      width="150"
    >
      <template #default="{ row }">
        <el-button type="text" @click="handleDelete(row)">
          {{ $t('defalult.sc') }}
        </el-button>
        <el-button type="text" @click="handleEdit(row)">
          {{ $t('defalult.bj') }}
        </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>
  <add ref="edit"></add>
</template>
<script>
import { getList, doExport, doDelete } from '@/api/maintenance';
import ajax from '@/api/download';
import add from './add';
export default {
  components: {
    add,
  },
  data() {
    return {
      tableData: [],
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: false,
      elementLoadingText: '正在加载...',
      levelData: [],
      categoryData: [],
      queryForm: {
        page: 1,
        size: 20,
        projectId: '',
        deviceName: '',
        equipmentName: '',
      },
    };
  },
  created() {
    this.queryForm.projectId = sessionStorage.getItem('projectId');
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      this.tableData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    showData() {
      this.fetchData();
    },
    handleAdd() {
      this.$refs['edit'].showEdit();
    },
    handleEdit(row) {
      this.$refs['edit'].showEdit(row);
    },
    //导出
    async exportData() {
      const url = await doExport(this.queryForm);
      ajax.downloadFile(url, { fileName: '设备维保.xls' });
    },
    //搜索
    search() {
      this.fetchData();
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    //删除
    deletes(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.deletes([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>
.manage-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;

  .el-input {
    width: 200px !important;
  }
  .el-select {
    width: 200px !important;
  }
}
</style>
