<template>
  <div class="manage-container">
    <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"
            :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"
            placeholder="请输入报修人"
          ></el-input>
        </el-form-item>
        <el-form-item
          width="100"
          prop="deviceType"
          :label="$t('maintenance.zdlx')"
        >
          <el-select v-model="queryForm.deviceType" 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="$t('maintenance.bxxm')"
        >
          <el-select v-model="queryForm.repairItem" 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="$t('maintenance.bxlx')"
        >
          <el-select v-model="queryForm.repairType" placeholder="报修类型">
            <el-option
              v-for="item in repairTypeData"
              :key="item.dataCode"
              :label="item.dataValue"
              :value="item.dataCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item width="100">
          <el-button @click="resetForm()">
            {{ $t('defalult.chongz') }}
          </el-button>
          <el-button type="primary" size="medium" @click="search">
            {{ $t('defalult.cx') }}
          </el-button>
          <el-button type="primary" @click="handleAdd()">
            {{ $t('defalult.tj') }}
          </el-button>
          <el-button type="primary" size="medium" @click="exportData">
            {{ $t('defalult.dc') }}
          </el-button>
        </el-form-item>
      </vab-query-form>
    </el-form>
    <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="device.name"
        label="站点设备"
        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 fixed="right" :label="$t('defalult.cz')" align="center">
        <template #default="{ row }">
          <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-column prop="upload" label="拍照上传" align="center">-->
      <!--        <template #default="{ row }">-->
      <!--          <el-upload-->
      <!--            action="http://101.133.141.167:8081/file/uploadImg"-->
      <!--            list-type="picture-card"-->
      <!--            limit="1"-->
      <!--            :on-preview="handlePictureCardPreview(row.id)"-->
      <!--            :on-remove="handleRemove"-->
      <!--          >-->
      <!--            <i class="el-icon-plus"></i>-->
      <!--          </el-upload>-->
      <!--          <el-dialog v-model="dialogVisibleImg">-->
      <!--            <img width="100%" :src="dialogImageUrl" alt="" />-->
      <!--          </el-dialog>-->
      <!--        </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>
  </div>
</template>

<script>
import { doDelete, getList, getDictData, doExport } from '@/api/repair';
import ajax from '@/api/download';
import repairEdit from './components/repairEdit';
export default {
  components: {
    repairEdit,
  },
  data() {
    return {
      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: {
    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 = data.items;
      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;
    },
    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">
.el-submenu__title:hover {
  background-color: rgba(#1890ff, 0.085) !important;
  color: hsla(208, 100%, 55%, 0.95) !important;
}
</style>
