<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="$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="repairPersonName"
            :label="$t('maintenance.bxr')"
          >
            <el-input
              v-model="queryForm.repairPersonName"
              size="small"
              :placeholder="$t('defalult.qsr')"
            ></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="$t('maintenance.zdlx')"
          >
            <el-select
              v-model="queryForm.deviceType"
              size="small"
              :placeholder="$t('maintenance.zdlx')"
            >
              <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="$t('maintenance.bxxm')"
          >
            <el-select
              v-model="queryForm.repairItem"
              size="small"
              :placeholder="$t('maintenance.bxxm')"
            >
              <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="$t('maintenance.bxlx')"
          >
            <el-select
              v-model="queryForm.repairType"
              size="small"
              :placeholder="$t('maintenance.bxlx')"
            >
              <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()">
        {{ $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>
    <el-table id="table" :data="tableData" stripe border style="width: 100%">
      <el-table-column
        fixed
        prop="device.name"
        :label="$t('dataEnquiry.zdmc')"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="deviceSpecifications"
        :label="$t('maintenance.zdgg')"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="repairPerson.name"
        :label="$t('maintenance.bxr')"
        width="110"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        :label="$t('maintenance.bxsj')"
        width="170"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="equipment.name"
        :label="$t('maintenance.bxsbsj')"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="repairType.dataValue"
        :label="$t('maintenance.bxlx')"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="deviceType"
        :label="$t('maintenance.zdlx')"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="remark"
        :label="$t('maintenance.bxsmd')"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="alarmFlag"
        :label="$t('dataEnquiry.zt')"
        width="120"
        align="center"
      >
        <template #default="{ row }">
          <el-tag
            v-if="row.status == 1"
            key="已派工"
            effect="dark"
            type="success"
            size="mini"
          >
            {{ $t('alarm.ypg') }}
          </el-tag>
          <el-tag
            v-if="row.status == 0"
            key="未派工"
            effect="dark"
            type="danger"
            size="mini"
          >
            {{ $t('alarm.qpg') }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="img"
        :label="$t('sensor.tp')"
        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="$t('defalult.cz')" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="handleTask(row)">
            {{ $t('maintenance.zrw') }}
          </el-button>
          <el-button type="text" @click="handleEdit(row)">
            {{ $t('defalult.bj') }}
          </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>
    <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>
