<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="100" prop="deviceName" label="站点名称">
            <el-input
              v-model="queryForm.deviceName"
              size="small"
              placeholder="请输入站点名称"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="equipmentName" label="设备名称">
            <el-input
              v-model="queryForm.equipmentName"
              size="small"
              placeholder="请输入设备名称"
            ></el-input>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-button">
      <el-button size="small" @click="resetForm()">重置</el-button>
      <el-button type="primary" size="small" @click="search">查询</el-button>
      <el-button type="primary" size="small" @click="handleAdd">添加</el-button>
      <el-button type="primary" size="small" @click="exportData">
        导出
      </el-button>
    </div>
  </div>
  <el-table
    v-loading="listLoading"
    :data="tableData"
    stripe
    border
    style="width: 100%"
  >
    <el-table-column
      fixed
      prop="device.name"
      label="站点名称"
      width="150"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="name.dataValue"
      label="设备"
      width="100"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="brand.dataValue"
      label="品牌"
      width="150"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="supplier.dataValue"
      label="供应商"
      width="150"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="unit.dataValue"
      label="单位"
      width="100"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="model"
      label="型号"
      width="100"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="code"
      label="编号"
      width="100"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="power"
      label="功率"
      width="100"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="status.dataValue"
      label="状态"
      width="100"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="shelfLife"
      label="质保期"
      width="100"
      align="center"
    ></el-table-column>
    <el-table-column fixed="right" label="操作" align="center" width="150">
      <template #default="{ row }">
        <el-button type="text" @click="handleDelete(row)">删除</el-button>
        <el-button type="text" @click="handleEdit(row)">编辑</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>
