<template>
  <div class="table-container">
    <div class="table-list" :style="tableData && tableData.length?'height:calc(100% - 50px);':'height:100%;'">
      <el-table :data="tableData" style="width: 100%;" height="100%" v-loading="loading" :header-cell-style="headerStyle">
        <el-table-column prop="folderName" label="所属模型文件夹" show-overflow-tooltip min-width="80"></el-table-column>
        <el-table-column prop="fileName" label="模型文件名" show-overflow-tooltip min-width="80"></el-table-column>
        <el-table-column :label="`${inSpaceType}本地名称`" show-overflow-tooltip min-width="100">
          <template slot-scope="scope">
            <div>
              {{scope.row.localName}}
              <el-badge v-if="scope.row.component&&scope.row.component.length?true:false"
                        :value="scope.row.component&&scope.row.component.length?scope.row.component.length:0"
                        class="item" type="warning">
              </el-badge>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="localId" :label="`${inSpaceType}本地编码`" show-overflow-tooltip min-width="100"></el-table-column>
        <el-table-column prop="equipCategory.name" :label="`${inSpaceType}类型`" show-overflow-tooltip min-width="80"></el-table-column>
        <el-table-column prop="bimId" label="BIM ID" show-overflow-tooltip min-width="220"></el-table-column>
        <el-table-column prop="taskState" label="任务执行情况" show-overflow-tooltip min-width="60"></el-table-column>
        <el-table-column prop="action" label="操作" min-width="100">
          <template slot-scope="scope">
            <i v-if="scope.row.taskState == '待验证' || scope.row.taskState == '未找到'" title="查看详情" class="iconfont icon-xiangqing table-button" @click="handleDetail(scope.$index, scope.row)"></i>
            <i v-if="scope.row.taskState == '未找到'" title="重新生成任务" class="iconfont icon-Update table-button" @click="handleRegenerate(scope.$index, scope.row)"></i>
            <i v-if="scope.row.taskState == '待验证'" title="删除任务" class="iconfont icon-delete table-button" @click="handleDelete(scope.$index, scope.row)"></i>
            <i v-if="scope.row.taskState == '未找到'" title="认可此任务执行情况" class="iconfont icon-lijiqueren table-button" @click="handleConfirm(scope.$index, scope.row)"></i>
          </template>
        </el-table-column>
        <template slot="empty">
          <div style="height: 60%;transform: translateY(50%);">
            <i class="icon-wushuju iconfont"></i>
            暂无数据
          </div>
        </template>
      </el-table>
    </div>
    <el-pagination class="right" v-show="tableData && tableData.length" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page.pageNumber"
     :page-sizes="page.pageSizes" :page-size="page.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="page.total">
    </el-pagination>
    <el-drawer title="任务详情" :visible.sync="drawer" direction="rtl" size="600px">
      <device-detail :detail="detailModel" ref="detailModel"></device-detail>
    </el-drawer>
  </div>
</template>

<script>
import { createModelTask, deleteModelTask, queryModelTask, updateModelTask } from "@/api/data_admin/buildTaskApi"
import tools from "@/utils/tools"
import { mapGetters } from "vuex"
import deviceDetail from '../detail/deviceDetail'

export default {
  components: {
    deviceDetail
  },
  computed: {
    ...mapGetters("layout", ["projectId", "userInfo"])
  },
  props: {
    paramsData: Object,
  },
  data() {
    return {
      inSpaceType: '设备',
      loading: false, // loading
      drawer: false, // 详情侧弹窗
      detailModel: {},
      tableData: [], //列表数据
      taskStateMap: {
        '-1': '未找到',
        '0': '已完成',
        '1': '待验证'
      },
      page: {
        pageSize: 50,
        pageSizes: [10, 20, 50, 100],
        pageNumber: 1,
        total: 0
      },
      headerStyle: {
        backgroundColor: '#e1e4e5',
        color: '#2b2b2b',
        lineHeight: '30px'
      }, // 列表样式
    };
  },
  created() {
    // this.getTableData()
  },
  methods: {
    // 获取列表数据
    getTableData() {
      this.loading = true
      let params = {
        filters: `projectId='${this.projectId}'`,
        cascade: [
          {
            name: 'component'
          }, {
            name: 'scanScheme'
          }, {
            name: 'equipCategory'
          }
        ],
        orders: "createTime desc, id asc",
        pageNumber: this.page.pageNumber,
        pageSize: this.page.pageSize
      }
      if(this.paramsData.taskState !== ''){
        params.filters += `;taskState=${this.paramsData.taskState}`
      }
      if(this.paramsData.modelFile){
        params.filters += `;fileId='${this.paramsData.modelFile}'`
      }
      if(this.paramsData.modelCategory){
        params.filters += `;category='${this.paramsData.modelCategory}'`
      }
      queryModelTask(params, res => {
        this.loading = false
        this.page.total = res.total;
        this.tableData = res.content.map(item => {
          if (item.component && item.component.length) {
            item.ComponentCount = []
            item.component.map(parts => {
              if (parts.equipCategory && parts.equipCategory.code && parts.equipCategory.name) {
                let index = item.ComponentCount.findIndex(c => {
                  return c.code == parts.equipCategory.code
                })
                if (index != -1) {
                  item.ComponentCount[index].count++
                } else {
                  item.ComponentCount.push({
                    name: parts.equipCategory.name,
                    code: parts.equipCategory.code,
                    count: 1
                  })
                }
              }
            })
          }
          item.TaskState = this.taskStateMap[item.TaskState]
          return item
        })
      })
    },
    // 删除任务
    handleDelete(index, row) {
      this.$confirm("确认删除该任务？", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = [{TaskId: row.TaskId}]
        deleteModelTask(params, res => {
          this.$message.success('删除成功')
          this.getTableData()
        })
      }).catch(() => {
        this.$message("取消删除")
      })
    },
    //重新生成任务
    handleRegenerate(index, row){
      let params = {
        Content: [{
          EquipId: row.EquipId,
          FileId: row.FileId,
          FileName: row.FileName,
          FolderId: row.FolderId,
          FolderName: row.FolderName,
          SchemeId: row.SchemeId,
          TaskState: -1
        }]
      }
      createModelTask(params, res => {
        this.$message.success('重新生成任务成功！')
        this.getTableData()
      })
    },
    //认可此任务执行情况
    handleConfirm(index, row){
      let params = {
        Content: [{
          ConfirmingPersonId: this.userInfo.userId,
          ConfirmingPersonName: this.userInfo.userName,
          TaskId: row.TaskId,
          TaskState: 0
        }]
      }
      updateModelTask(params, res => {
        this.$message.success('更新成功！')
        this.getTableData()
      })
    },
    //查看任务详情
    handleDetail(index, row) {
      this.drawer = true
      this.$nextTick(() => {
        this.detailModel = row
        this.$refs.detailModel.handleModelContent(row)
      })
    },
    // 改变pagesize
    handleSizeChange(pageSize) {
      this.page.pageNumber = 1
      this.page.pageSize = pageSize;
      this.getTableData()
    },
    // 改变pageno
    handleCurrentChange(pageNo) {
      this.page.pageNumber = pageNo;
      this.getTableData()
    }
  },
  watch: {
    paramsData: {
      handler(newName, oldName) {
        if(!tools.isSimilarly(newName, oldName)){
          this.page.pageNumber = 1
          this.getTableData()
        }
      },
      immediate: true,
      deep: true
    },
    projectId() {
      this.page.pageNumber = 1
      this.getTableData()
    }
  }
};
</script>
<style lang="less" scoped>
/deep/ .el-drawer__body {
  height: calc(100% - 80px);
  border-top: 1px solid #c9c9c9;
}
/deep/ .el-drawer__header {
  margin-bottom: 12px;
}
/deep/ .el-badge__content{
  width: 20px;
}
.table-container {
  height: 100%;
  background: #fff;
  .table-button{
    cursor: pointer;
    margin-right: 15px;
  }
}
</style>
