<template>
  <div>
    <el-row v-if="!showHeader" class="select-main" type="flex" align="center">
      <el-col :span="2" class="flex-align-center">
        <el-checkbox v-model="planChecked" class="plan-checked" @change="changeCheck" />
      </el-col>
      <el-col :span="3" class="item-checked">已选择<span style="color: #409EFF">{{ curcentChecked }}</span>个</el-col>
      <el-col :span="1" class="item-click">|</el-col>
      <el-col :span="2" class="item-click click-blue" @click.native="handlePlan('test')">提测</el-col>
      <el-col :span="2" class="item-click click-blue" @click.native="handlePlan('allow')">准出</el-col>
      <el-col :span="4" class="item-click click-blue" @click.native="handlePlan('daily')">建立测试日报</el-col>
      <el-col :span="4" class="item-click" @click.native="handlePlan('cancel')">取消选择</el-col>
    </el-row>
    <el-table
      ref="planTable"
      :data="all_task"
      style="width: 100%;"
      size="mini"
      row-key="id"
      :expand-row-keys="expandArr"
      :header-cell-style="{ color: 'rgb(74, 74, 74)', fontSize: '14px', fontWeight: '500', textAlign: 'center' }"
      show-overflow-tooltip="true"
      :show-header="showHeader"
      :header-row-style="{height: '50px'}"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand" width="40">
        <template slot="header">
          <div class="expand"><i v-show="!allChange" class="el-icon-plus" @click="expandAll(true)" /></div>
          <div class="expand"><i v-show="allChange" class="el-icon-minus" @click="expandAll(false)" /></div>
        </template>
        <template slot-scope="props">
          <schedule-list :id="props.row.id" :type-list="taskScheduleEvent" />
        </template>
      </el-table-column>
      <el-table-column label="优先级" prop="priority" width="90" sortable align="center">
        <template slot-scope="scope" style="text-align: center;">
          <span class="div_priority" :class="scope.row.priorityString">
            {{ scope.row.priorityString }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="任务名称" width="200" align="center" show-overflow-tooltip>
        <template slot-scope="scope"><span class="task-title" @click="link_task(scope.row.id)">{{ scope.row.name }}</span></template>
      </el-table-column>
      <el-table-column label="所属模块" width="150" align="center" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.moduleInfoName }}</template>
      </el-table-column>
      <el-table-column label="状态" width="105" align="center">
        <template slot-scope="scope">
          <el-select
            v-model="scope.row.status"
            :class="{
              'status_color': scope.row.status === 0,
              'status_color1': scope.row.status === 1,
              'status_color4': scope.row.status === 3,
              'status_color6': scope.row.status === 2,
              'status_color7': scope.row.status === 4,
              'status_color2': scope.row.status === 5
            }"
            class="btns"
            size="mini"
            @change="changeStatus(scope.row)"
          >
            <el-option v-for="item in allStatus" :key="item.code" :label="item.msg" :value="item.code" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="所属需求" width="200" align="center" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.requireName }}</template>
      </el-table-column>
      <el-table-column label="跟版客户端" width="120" align="center" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.app }}</template>
      </el-table-column>
      <el-table-column label="开发负责人" width="100" align="center" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.rdObject ? scope.row.rdObject.name : '' }}</template>
      </el-table-column>
      <el-table-column label="测试负责人" width="100" align="center" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.qaObject ? scope.row.qaObject.name : '' }}</template>
      </el-table-column>
      <el-table-column label="任务进度" min-width="150" align="center">
        <template slot-scope="scope">
          <el-progress :percentage="Number(scope.row.rate && scope.row.rate.substring(0,4))" color="#409eff" />
        </template>
      </el-table-column>
    </el-table>
    <TestReport v-if="dialogTestReport" ref="TestReport" />
    <DailyReport v-if="dialogDailyReport" ref="DailyReport" />
    <ClientReport v-if="dialogClientReport" ref="ClientReport" />
    <taskDialog v-if="showTaskDialog" :show.sync="showTaskDialog" :task-id="taskId.id" :status-name="taskId.statusString" @getList="get_allTask" />
    <normal-dialog :show-dialog.sync="statusDialog" :title="'状态变更：已上线'" :width="'50%'" @confirm="confirmStatus()">
      <div class="dialog-change-status">
        <span>实际上线时间：</span>
        <el-date-picker v-model="changeStatusDate" type="date" style="width:100%;" placeholder="选择日期" format="yyyy-MM-dd HH:mm:ss" />
      </div>
    </normal-dialog>
  </div>
</template>
<script>
import '@/styles/PublicStyle/index.scss'
import TestReport from '@/views/Platform/presentation/Templates/TestReport' // 提测
import DailyReport from '@/views/Platform/presentation/Templates/DailyReport' // 日报
import ClientReport from '@/views/Platform/presentation/Templates/ClientReport' // 准出
import { taskList } from '@/api/projectIndex'
import { taskUpdate } from '@/api/projectViewDetails'
import { configShowTaskEnum } from '@/api/taskIndex'
import scheduleList from './scheduleList'
import normalDialog from '@/components/dialog/normalDialog'
import taskDialog from '@/views/projectManage/taskList/dialog/taskDialog' // 任务状态修改（已上线/已提测/已准出）
export default {
  components: {
    normalDialog,
    TestReport,
    DailyReport,
    ClientReport,
    scheduleList,
    taskDialog
  },
  data() {
    return {
      changeData: new Map(),
      allChange: false, // 是否全展开
      expandArr: [], // 展开行数组
      showTaskDialog: false, // 状态弹窗
      inputValue: '',
      all_task: [], // 任务列表
      allStatus: [], // 任务所有状态
      taskScheduleEvent: [], // 排期类型
      showHeader: true, // 任务列表的表头是否显示
      curcentList: [], // 当前已选择的列表
      curcentChecked: 0, // 当前已选择的数量
      planChecked: false,
      planHandleType: '', // 任务列表操作类型
      dialogTestReport: false, // 提测
      dialogDailyReport: false, // 日报
      dialogClientReport: false, // 准出
      statusDialog: false, // 修改状态弹框
      changeStatusDate: null, // 状态改变时间
      nowChangeTask: null, // 当前正在改变的任务对象
      taskId: '' // 将要修改状态的任务id
    }
  },
  watch: {
    value: {
      handler(newV, oldV) {
        this.inputValue = newV
      },
      immediate: true
    }
  },
  created() {
    this.getTaskStatus()
    this.get_allTask()
  },
  methods: {
    async get_allTask() { // 获取全部任务
      const res = await taskList({
        projectId: this.$route.query.id
      })
      if (res.code === 200) {
        this.all_task = res.data
        // for (const [key, value] of Object.entries(res.data[0])) {
        //   console.log(`${key}: ${value}`)
        // }
      }
    },
    async getTaskStatus() { // 获取任务状态列表
      const res = await configShowTaskEnum()
      if (res.code === 200) {
        this.allStatus = res.data.taskStatus
        this.taskScheduleEvent = res.data.taskScheduleEvent || []
      }
    },
    changeCheck(val) {
      if (val) {
        this.all_task.forEach(row => {
          this.$refs.planTable.toggleRowSelection(row, true)
        })
      } else {
        this.$refs.planTable.clearSelection()
      }
    },
    expandAll(isEx) { // 全部展开
      this.allChange = isEx
      isEx ? this.expandArr = this.all_task.map(item => item.id) : this.expandArr = []
    },
    async changeStatus(e) { // 状态改变
      alert(e)
      if (e.status === 2 || e.status === 4 || e.status === 5) {
        this.taskId = e
        this.allStatus.map(item => {
          item.code === e.status ? this.taskId.statusString = item.msg : ''
        })
        this.showTaskDialog = true
        this.nowChangeTask = e
        return
      } else {
        const user = {
          name: localStorage.getItem('username'),
          ename: localStorage.getItem('realname'),
          id: ''
        }
        const taskInfoDO = e
        const resTask = await taskUpdate({ taskInfoDO, user })
        if (resTask.code === 200) {
          this.$message({ message: resTask.msg, type: 'success', offset: 150 })
        }
      }
    },
    // async confirmStatus() { // 确认更改状态
    //   const user = { name: localStorage.getItem('username'), ename: localStorage.getItem('realname'), id: '' }
    //   const taskInfoDO = this.nowChangeTask
    //   taskInfoDO.onlineRealTime = this.changeStatusDate
    //   const resTask = await taskUpdate({ taskInfoDO, user })
    //   if (resTask.code === 200) {
    //     this.$message({ message: resTask.msg, type: 'success', offset: 150 })
    //   }
    // },
    handleSelectionChange(val) { // 任务列表删选操作
      val.length > 0 ? this.showHeader = false : this.showHeader = true
      this.curcentChecked = val.length
      this.curcentList = val
      if (val.length === this.all_task.length) {
        this.planChecked = true
      }
    },
    handlePlan(type) { // 任务列表操作
      this.planHandleType = type
      switch (type) {
        case 'test':
          this.filtrateTest()
          break
        case 'allow':
          this.filtrateAllow()
          break
        case 'daily':
          this.filtrateDaily()
          break
        case 'cancel':
          this.$refs.planTable.clearSelection()
          break
      }
    },
    filtrateTest() { // 提测筛选
      this.dialogTestReport = true
      this.$nextTick(() => {
        this.$refs.TestReport.init(7, this.curcentList.map(item => { return item.id }))
      })
    },
    filtrateAllow() { // 准出筛选
      this.dialogClientReport = true
      this.$nextTick(() => {
        this.$refs.ClientReport.init(7, this.curcentList.map(item => { return item.id }))
      })
    },
    filtrateDaily() { // 建立日报
      this.dialogDailyReport = true
      this.$nextTick(() => {
        this.$refs.DailyReport.init(7, this.curcentList.map(item => { return item.id }))
      })
    },
    link_task(id) { // 跳转到任务详情页
      this.$router.push({ name: '任务详情', query: { id: id }})
    }
  }
}
</script>
<style lang="scss" scoped>
@mixin setStatus($color) {
  input {
    color:$color;
    border: 1px solid $color;
  }
  >>> .el-select__caret{
    color:$color;
  }
  >>> .el-input__inner{
    color:$color;
    border-color: $color;
  }
  >>> .el-input__inner:focus {
    border-color: $color;
  }
}
>>>.el-row .el-col {
  margin: 10px 0;
}
.task-title {
  cursor: pointer;
}
.P0 {
  background-color: #F56C6C;
}
.P1 {
  background-color: #FF8952;
}
.P2 {
  background-color: #F5E300;
}
.P3 {
  background-color: #7ED321;
}
.P4 {
  background-color: #61D3B8;
}
.P5 {
  background-color: #69B3FF;
}
.P6 {
  background-color: #BDBDBD;
}
.status0 {
  @include setStatus(#409EFF)
}
.status1 {
  @include setStatus(#FF8952)
}
.status3 {
  @include setStatus(#13C2C2)
}
.status5 {
  @include setStatus(#7ED321)
}
.expand i {
  border:1px solid #DCDFE6;
}
>>>.el-table__expand-icon{
  font-size: 14px;
  .el-icon{
    margin: 0;
    transform: translate(-50%, -50%);
    border:1px solid #DCDFE6;
  }
}
>>>.el-table__expand-icon .el-icon-arrow-right::before{
  content: "\E6D9";
}
>>>.el-table__expand-icon--expanded .el-icon-arrow-right::before{
  content: "\E6D8";
}
>>>.el-table__expand-icon--expanded {
  transform: rotate(180deg);
}
>>>.el-table__expanded-cell {
  background-color: #FFFFFF;
  padding: 0;
}
>>>.el-table__expanded-cell:hover {
  background-color: #FFFFFF !important;
}
.div_priority {
  text-align: center;
  color: #ffffff;
  padding: inherit;
  border-radius: 4px;
  width: 40px;
  display: inline-block;
}
.plan-checked {
  padding-left: 21px;
}
.select-main {
  height: 50px;
  border-bottom: 1px solid #DCDFE6;
  .flex-align-center {
    display: flex;
    align-items: center;
  }
  .item-checked {
    color: #606266;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: left;
    cursor: pointer;
  }
  .item-click{
    color: #606266;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
  }
  .click-blue {
    color: #409EFF;
  }
}
.descr {
  display: flex;
  justify-content: flex-start;
}
.planList >>> .el-table th>.cell {
  padding-left: 14px;
  padding-right: 14px;
}
.dialog-change-status {
  margin: 2% 3%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  white-space:nowrap;
}
</style>
<style lang="scss">
.btns .el-input--suffix .el-input__inner {
  padding-right: 10px;
  padding-left: 10px;
  width: 73px;
}
.item{
  /deep/ input {
    color: rgb(126, 211, 33);
    border: 1px solid rgb(126, 211, 33);
     border-color: rgb(126, 211, 33) !important;
         font-weight: 900;
  }
  /deep/ .el-input__suffix {
    color: rgb(126, 211, 33) !important;
    right: 1px;
  }
  /deep/ .el-select__caret {
    color: rgb(126, 211, 33) !important;
  }
}
.item1 {
  /deep/ input {
    color: rgb(255, 204, 102);
     border: 1px solid rgb(255, 204, 102);
     border-color: rgb(255, 204, 102) !important;
         font-weight: 900;
  }
  /deep/ .el-input__suffix {
    color: rgb(255, 204, 102) !important;
    right: 1px;
  }
  /deep/ .el-select__caret {
    color: rgb(255, 204, 102) !important;
  }
}
.item2 {
  /deep/ input {
    color: rgb(245, 108, 108);
    border: 1px solid rgb(245, 108, 108);
    border-color: rgb(245, 108, 108) !important;
        font-weight: 900;
  }
   /deep/ .el-input__suffix {
    color: rgb(245, 108, 108) !important;
    right: 1px;
  }
  /deep/ .el-select__caret {
   color: rgb(245, 108, 108) !important;
  }
}
.item3 {
  /deep/ input {
    color: #D675F0;
    border: 1px solid #D675F0;
    border-color: #D675F0 !important;
        font-weight: 900;
  }
    /deep/ .el-input__suffix {
    color: #D675F0 !important;
    right: 1px;
  }
   /deep/ .el-select__caret {
   color: #D675F0 !important;
  }
}
.item-color {
  /deep/ input {
    color: rgb(106, 180, 255);
    border: 1px solid rgb(106, 180, 255);
    border-color: rgb(106, 180, 255) !important;
        font-weight: 900;
  }
    /deep/ .el-input__suffix {
    color: rgb(106, 180, 255) !important;
    right: 1px;
  }
   /deep/ .el-select__caret {
   color: rgb(106, 180, 255) !important;
  }
}
</style>
