<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="month" label="时间">
            <el-select
              v-model="queryForm.status"
              size="small"
              placeholder="请选择任务状态"
            >
              <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()">重置</el-button>
      <el-button type="primary" size="small" plain @click="handleAdd">
        新建
      </el-button>
      <el-button type="primary" size="small" @click="search">查询</el-button>
      <el-button type="primary" size="small" @click="exportData">
        导出
      </el-button>
      <el-button type="primary" size="small" @click="deletes">
        批量删除
      </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="站点名称"
      width="120"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="type"
      label="任务类型"
      width="180"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="taskContent"
      label="任务内容"
      width="250"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="handler.name"
      label="处理人"
      width="120"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="handler.contact"
      label="处理人联系方式"
      width="120"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="completeTime"
      label="完成时间"
      width="180"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="status"
      label="状态"
      width="120"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="feedbackText"
      label="反馈内容"
      width="250"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="feedbackImg"
      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 label="操作" align="center" width="180">
      <template #default="{ row }">
        <el-button type="text" @click="feedbackShow(row)">反馈</el-button>
        <el-button type="text" @click="handleEdit(row)">编辑</el-button>
        <el-button 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>
  <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: '全部',
          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>
