<template>
  <div class="manage-container">
    <div class="manage-input">
      <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"
              size="small"
              :placeholder="$t('dataEnquiry.qsrzdmc')"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="month" :label="$t('defalult.sj')">
            <el-select
              v-model="queryForm.status"
              size="small"
              :placeholder="$t('task.qxzrwzt')"
            >
              <el-option
                v-for="item in statusData"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></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" plain @click="handleAdd">
        {{ $t('defalult.xj') }}
      </el-button>
      <el-button type="primary" size="small" @click="search">
        {{ $t('defalult.cx') }}
      </el-button>
      <el-button type="primary" size="small" @click="exportData">
        {{ $t('defalult.dc') }}
      </el-button>
      <el-button type="primary" size="small" @click="deletes">
        {{ $t('defalult.plsc') }}
      </el-button>
    </div>
  </div>
  <el-table
    :data="tableData"
    stripe
    border
    style="width: 100%"
    @selection-change="handleSelectionChange"
  >
    <el-table-column
      fixed
      type="selection"
      width="40"
      align="center"
    ></el-table-column>
    <el-table-column
      fixed
      prop="device.name"
      :label="$t('dataEnquiry.zdmc')"
      width="120"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="type"
      :label="$t('task.rwlx')"
      width="180"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="taskContent"
      :label="$t('task.rwnr')"
      width="250"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="handler.name"
      :label="$t('task.clr')"
      width="120"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="handler.contact"
      :label="$t('task.clrlxfs')"
      width="120"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="completeTime"
      :label="$t('task.wcsj')"
      width="180"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="status"
      :label="$t('dataEnquiry.zt')"
      width="120"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="feedbackText"
      :label="$t('task.fknr')"
      width="250"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="feedbackImg"
      :label="$t('task.fltp')"
      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 :label="$t('defalult.cz')" align="center" width="180">
      <template #default="{ row }">
        <el-button type="text" @click="feedbackShow(row)">
          {{ $t('task.fk') }}
        </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>
  <alarm-add ref="add"></alarm-add>
  <feedback ref="feedback"></feedback>
</template>

<script>
import { getList, doDelete, taskExport } from '@/api/task';
import ajax from '@/api/download';
import alarmAdd from './alarmAdd';
import feedback from './feedback';
import { imgReplacePath } from '@/utils/img';
export default {
  components: {
    alarmAdd,
    feedback,
  },
  data() {
    return {
      tableData: [],
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: false,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        status: 3,
        projectId: '',
        type: 2,
      },
      statusData: [
        {
          label: 'all',
          value: 3,
        },
        {
          label: '未处理',
          value: 0,
        },
        {
          label: '已处理',
          value: 1,
        },
      ],
      ids: [],
    };
  },
  created() {
    this.queryForm.projectId = sessionStorage.getItem('projectId');
  },
  methods: {
    otherIndex() {
      this.fetchData();
    },
    async fetchData() {
      const { data } = await getList(this.queryForm);
      this.tableData = imgReplacePath(data.items, 'feedbackImg');
      this.total = data.total;
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    handleAdd() {
      this.$refs['add'].showEdit();
    },
    handleEdit(row) {
      this.$refs['add'].showEdit(row);
    },
    feedbackShow(row) {
      this.$refs['feedback'].feedbackIndex(row);
    },
    //搜索
    search() {
      this.fetchData();
    },
    //导出
    async exportData() {
      const url = await taskExport(this.queryForm);
      ajax.downloadFile(url, { fileName: '任务.xls' });
    },
    handleSelectionChange(val) {
      const ids = [];
      val.forEach(row => {
        ids.push(row.id);
      });
      this.ids = ids;
    },
    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;
      that.deleteData([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>
