<template>
  <div class="content-container">
    <el-row class="tool-bar">
      <el-col :span="12" class="left">
        <div class="content-title">
          {{ title }}
        </div>
      </el-col>

      <el-col :span="12" class="right">
        <el-input v-model.trim="conditions.taskTitle" class="search-input m-right-15" placeholder="请输入任务名称">
          <el-button slot="append" icon="el-icon-search" @click="getData" />
        </el-input>
      </el-col>
    </el-row>

    <el-row class="content-body">
      <el-table v-loading="listLoading" class="page-table" border :data="dataList" height="calc(100vh - 210px)">
        <el-table-column type="index" label="序号" width="60" header-align="center" align="center" />

        <el-table-column prop="taskTitle" label="任务名称" header-align="left" align="left">
          <template v-slot="{row}">
            <span>{{ row.taskTitle }}</span>
          </template>
        </el-table-column>
        <el-table-column label="采空区">
          <template v-slot="{row}">
            <span>{{ NumConvertLM(row.goafOrebelt) }}/{{ row.goafOrebody }}/{{ row.goafOreheight }}/{{ row.goafName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="handleAccountName" label="执行人员" header-align="center" align="center" width="100">
          <template v-slot="{row}">
            <el-popover v-if="row.handleAccountName" trigger="hover" placement="top">
              <p>部门: {{ row.handleGroupName }}</p>
              <p>岗位: {{ row.handlePositionName }}</p>
              <div slot="reference" class="name-wrapper">
                <i class="el-icon-user" />
                {{ row.handleAccountName }}
              </div>
            </el-popover>
            <span v-else>-</span>
          </template>
        </el-table-column>

        <el-table-column prop="expectedEndDate" label="截止时间" header-align="center" align="center" width="180">
          <template v-slot="{row}">
            <span v-if="row.expectedEndDate" style="margin-left: 10px"><i class="el-icon-time" />{{ row.expectedEndDate }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>

        <el-table-column prop="status" label="状态" header-align="center" align="center" width="120">
          <template v-slot="{row}">
            <span><el-tag type="info" effect="plain" color="rgb(38 69 90)">{{ row.status | checkStatusFilter }}</el-tag></span>
          </template>
        </el-table-column>

        <el-table-column label="操作" header-align="center" align="center" width="280">
          <template v-slot="{row}">
            <el-button v-if="row.status!==1" size="mini" type="text" style="color:#1B81FF" @click="handleTask(row)">处理</el-button>
            <!-- <el-button size="mini" type="text" style="color:#1B81FF" @click="handleUpdate(row)">修改</el-button>
            <el-button size="mini" type="text" style="color:#E44E2D" @click="handleDelete(row)">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-wrap">
        <pagination v-show="total>0" :total="total" :page.sync="conditions.page" :limit.sync="conditions.limit" @pagination="getData" />
      </div>
    </el-row>
    <task-event ref="TaskEvent" title="任务动态" />
    <task-details ref="TaskDetails" title="任务详情" />
    <handling-view ref="HandlingView" title="处理任务" @success="getData" />
    <check-task ref="checkTask" @formSuccess="getData" />
  </div>
</template>

<script>
import CheckTask from './CheckTask.vue'
import TaskEvent from './TaskEvent.vue'
// import HandlingView from './check/Handle.vue'
import HandlingView from './ChecklistItem.vue'

import TaskDetails from './Details.vue'
import { getCheckTaskByPage, deleteCheckTaskById } from '@/api/goaf/task'
import { mapGetters } from 'vuex'
import Pagination from '@/components/Pagination'
import { checkType } from '@/utils'
import { NumConvertLM } from '@/utils'
export default {
  name: 'MyWaiting',
  filters: {
    checkTypeFilter(val) {
      return checkType(val)
    },
    checkStatusFilter(val) {
      if (val === 0) {
        return '待处理'
      } else if (val === 1) {
        return '已处理'
      } else if (val === 2) {
        return '已逾期'
      } else {
        return '--'
      }
    }
  },
  components: { Pagination, CheckTask, TaskEvent, HandlingView, TaskDetails },
  data() {
    return {
      title: '',
      dataList: [],
      total: 0,
      listLoading: false,
      conditions: {
        page: 1,
        limit: 10,
        taskTitle: '',
        status: 0
      },
      isGroup: false
    }
  },
  computed: {
    ...mapGetters([
      'userData'
    ])
  },
  mounted() {
    this.loadData(this.conditions.status)
  },
  methods: {
    NumConvertLM,
    // 加载数据
    loadData(status) {
      this.conditions.status = status
      switch (status) {
        case 0:
          this.title = '待处理'
          break
        case 1:
          this.title = '处理完成'
          break

        case 2:
          this.title = '逾期'
          break
      }
      this.getData()
    },

    // 加载数据
    getData() {
      this.listLoading = true
      getCheckTaskByPage(this.conditions).then((resp) => {
        this.listLoading = false
        const { code, data, total, msg } = resp
        if (code === 0) {
          this.total = total
          this.dataList = data
        } else {
          this.$message.error(msg)
        }
      }).catch((error) => {
        console.log(error)
      })
    },

    // 指令处理
    handlerCommand(command, data) {
      switch (command) {
        case 'editTask':
          this.handleUpdate(data)
          break

        case 'delTask':
          this.handleDelete(data)
          break
      }
    },
    handleUpdate(data) {
      this.$refs['checkTask'].showEditModel(JSON.parse(JSON.stringify(data)))
    },
    // 任务动态
    handleTaskEvent(data) {
      this.$refs['TaskEvent'].showTaskEvent(data)
    },

    // 处理
    handleTask(data) {
      this.$refs['HandlingView'].showView(data)
    },

    // 添加
    handleAdd() {
      this.$refs['checkTask'].showAddModel()
    },

    // 查看详情
    handleViewDetails(data) {
      const { taskId } = data
      this.$refs['TaskDetails'].showView(taskId)
    },

    // 删除
    handleDelete(data) {
      const { taskId } = data
      this.$confirm('删除任务？是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCheckTaskById(taskId).then((resp) => {
          const { code, msg } = resp
          if (code === 0) {
            this.getData()
            this.$message.success(msg)
          } else {
            this.$message.error(msg)
          }
        }).catch((error) => {
          console.log(error)
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },

    // 操作更新
    operationalUpdate() {
      this.$emit('operationalUpdate')
    }
  }
}
</script>

<style lang="scss">
</style>
<style lang="scss" scoped>
.content-container{
  height: calc(100%- 80px);
  margin: 10px 10px 10px 0;
}
</style>
