<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="repairPersonName" label="报修人">
            <el-input
              v-model="queryForm.repairPersonName"
              size="small"
              placeholder="请输入报修人"
            ></el-input>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="80px">
        <vab-query-form>
          <el-form-item width="100" prop="deviceType" label="站点类型">
            <el-select
              v-model="queryForm.deviceType"
              size="small"
              placeholder="站点类型"
            >
              <el-option
                v-for="item in deviceTypeData"
                :key="item.dataCode"
                :label="item.dataValue"
                :value="item.dataCode"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item width="100" prop="repairItem" label="报修项目">
            <el-select
              v-model="queryForm.repairItem"
              size="small"
              placeholder="报修项目"
            >
              <el-option
                v-for="item in repairItemData"
                :key="item.dataCode"
                :label="item.dataValue"
                :value="item.dataCode"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item width="100" prop="repairType" label="报修类型">
            <el-select
              v-model="queryForm.repairType"
              size="small"
              placeholder="报修类型"
            >
              <el-option
                v-for="item in repairTypeData"
                :key="item.dataCode"
                :label="item.dataValue"
                :value="item.dataCode"
              ></el-option>
            </el-select>
          </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
        v-if="isBtnPerm('/repair/add')"
        type="primary"
        size="small"
        @click="handleAdd()"
      >
        添加
      </el-button>
      <el-button
        v-if="isBtnPerm('/repair/export')"
        type="primary"
        size="small"
        @click="exportData"
      >
        导出
      </el-button>
    </div>
    <el-table id="table" :data="tableData" stripe border style="width: 100%">
      <el-table-column
        fixed
        prop="device.name"
        label="站点名称"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="deviceSpecifications"
        label="站点规格"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="repairPerson.name"
        label="报修人"
        width="110"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        label="报修时间"
        width="170"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="equipment.name"
        label="保修设备名称"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="repairType.dataValue"
        label="报修类型"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="deviceType"
        label="站点类型"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="remark"
        label="报修说明"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column prop="alarmFlag" label="状态" width="120" align="center">
        <template #default="{ row }">
          <el-tag
            v-if="row.status == 1"
            key="已派工"
            effect="dark"
            type="success"
            size="mini"
          >
            已派工
          </el-tag>
          <el-tag
            v-if="row.status == 0"
            key="未派工"
            effect="dark"
            type="danger"
            size="mini"
          >
            未派工
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="img" label="图片" width="140" align="center">
        <template #default="{ row }">
          <div v-for="(item, index) in row.fileList" :key="index">
            <el-image
              style="width: 100px; height: 100px"
              :src="row.url"
              :preview-src-list="item"
            ></el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template #default="{ row }">
          <el-button
            v-if="isBtnPerm('/repair/toTask')"
            type="text"
            @click="handleTask(row)"
          >
            转任务
          </el-button>
          <el-button
            v-if="isBtnPerm('/repair/update')"
            type="text"
            @click="handleEdit(row)"
          >
            编辑
          </el-button>
          <el-button
            v-if="isBtnPerm('/repair/remove')"
            type="text"
            @click="handleDelete(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>
    <repair-edit ref="edit"></repair-edit>
    <to-task ref="task"></to-task>
  </div>
</template>

<script>
import { doDelete, getList, getDictData, doExport } from '@/api/repair';
import ajax from '@/api/download';
import repairEdit from './repairEdit';
import toTask from './totask';
import { imgReplacePath } from '@/utils/img';
export default {
  components: {
    repairEdit,
    toTask,
  },
  data() {
    return {
      url: '',
      srcList: '',
      tableData: [],
      repairItemData: [],
      deviceTypeData: [],
      repairTypeData: [],
      dialogImageUrl: '',
      dialogVisible: false,
      dialogVisibleImg: false,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: false,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        deviceName: '',
        repairPersonName: '',
        deviceType: '',
        repairItem: '',
        repairType: '',
        projectId: '',
      },
    };
  },
  created() {
    this.queryForm.projectId = sessionStorage.getItem('projectId');
    this.getDictData();
    this.fetchData();
  },
  methods: {
    showData() {
      this.getDictData();
      this.fetchData();
    },
    search() {
      this.fetchData();
    },
    //导出
    async exportData() {
      const url = await doExport(this.queryForm);
      ajax.downloadFile(url, { fileName: '在线保修.xls' });
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      this.tableData = imgReplacePath(data.items, 'repairImg');
      console.log(this.tableData);
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    async getDictData() {
      const { data } = await getDictData();
      this.repairItemData = data.repairItem;
      this.deviceTypeData = data.deviceType;
      this.repairTypeData = data.repairType;
    },
    handleTask(row) {
      this.$refs['task'].showTask(row);
    },
    handleAdd() {
      this.$refs['edit'].showEdit();
    },
    handleEdit(row) {
      this.$refs['edit'].showEdit(row);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    deletes() {
      const ids = this.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;
      this.ids = [row.id];
      that.deletes();
    },
    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>
