<template>
  <div>
    <div class="table-top">
      <label>{{ name }}的任务</label>
      <span class="new-tab-open">
        <el-switch
          v-model="newTabOpen"
          active-text="新标签页跳转"
        />
      </span>
    </div>
    <el-row v-if="!showHeader" class="select-main" type="flex" align="center">
      <el-col :span="1" 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-line">|</el-col>
      <el-col :span="2" class="item-click" @click.native="addSechedule()"><img :src="imgUrl">添加排期</el-col>
      <el-col :span="2" class="item-click" @click.native="handlePlan('test')"><img :src="imgUrl">提测</el-col>
      <el-col :span="2" class="item-click" @click.native="handlePlan('allow')"><img :src="imgUrl">准出</el-col>
      <el-col :span="4" class="item-click" @click.native="handlePlan('daily')"><img :src="imgUrl">建立测试日报</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"
      :header-cell-style="{ color: 'rgb(74, 74, 74)', fontSize: '14px', fontWeight: '500'}"
      :row-style="{ fontSize: '14px' }"
      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 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="任务名称" min-width="250" align="left" show-overflow-tooltip>
        <template slot-scope="scope">
          <div class="task-main">
            <span class="task-id">TASK-{{ scope.row.id }}
              <span
                v-if="scope.row.tagNotification !== null"
                :class="{
                  'tagNotification': scope.row.tagType === 0,
                  'tagNotification1': scope.row.tagType === 1
                }"
              >
                {{ scope.row.tagNotification }}
              </span>
            </span>
            <span class="task-title" @click="link_task(scope.row.id)">{{ scope.row.name }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="150" align="center">
        <template slot-scope="scope">
          <el-select
            v-model="scope.row.status"
            :class="{
              'status0':scope.row.status===0,
              'status1':scope.row.status > 0 && scope.row.status <100,
              'status2':scope.row.status===100,
              'public_btn':scope.row.status!==10 || scope.row.status!==40 || scope.row.status!==20 || scope.row.status!==30,
              'public_btn1':scope.row.status===40,
              'public_btn2':scope.row.status===10,
              'public_btn3':scope.row.status===20 || scope.row.status===30
            }"
            size="mini"
            @change="changeStatus(scope.row)"
          >
            <el-option v-for="item in scope.row.availableStatusList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </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="交付日期" width="150" align="center" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.optionsObject ? scope.row.optionsObject.endTime : '' }}</template>
      </el-table-column>
      <el-table-column label="所属模块" width="200" align="center" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.moduleInfoName }}</template>
      </el-table-column>
      <el-table-column label="跟版客户端" width="150" align="center" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.involveAppString || '无' }}</template>
      </el-table-column>
      <el-table-column label="缺陷数量" width="100" align="center" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.bugCount }}</template>
      </el-table-column>
      <el-table-column label="任务进度" 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>
    <div align="right">
      <el-pagination
        :page-sizes="[15, 30, 45, total]"
        :current-page.sync="pages.curIndex"
        :page-size="pages.pageSize"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <TestReport v-if="dialogTestReport" ref="TestReport" />
    <DailyReport v-if="dialogDailyReport" ref="DailyReport" />
    <ReleaseReport v-if="dialogClientReport" ref="ClientReport" />
    <task-dialog v-if="showTaskDialog" :show.sync="showTaskDialog" :task-id="taskId.id" :status-name="taskId.statusString" @getList="getNew" />
    <!-- 批量排期 -->
    <modify-schedule
      v-if="visibleSchedule"
      :visible.sync="visibleSchedule"
      :select-task-list="selectTaskList"
      title="新建排期"
      @update="get_allTask()"
    />
  </div>
</template>
<script>
import imgUrl from '@/assets/建立档案@2x.png'
import '@/styles/PublicStyle/index.scss'
import TestReport from '@/views/reportManagement/components/TestingReport' // 提测
import DailyReport from '@/views/reportManagement/components/DailyReport' // 日报
import ReleaseReport from '@/views/reportManagement/components/ReleaseReport' // 准出
import { taskUpdate } from '@/api/projectViewDetails'
import { configShowTaskEnum, configShowTaskStatusEnum, taskList } from '@/api/taskIndex'
import modifySchedule from '@/views/projectManage/projectList/components/modifySchedule'
import taskDialog from '@/views/projectManage/taskList/dialog/taskDialog' // 任务状态修改（已上线/已提测/已准出）
import { dailyReportCheckStatus, reportreleaseCheckStatus, reportdelivertestCheckStatus } from '@/api/reportTemplate'
export default {
  components: {
    TestReport,
    DailyReport,
    ReleaseReport,
    taskDialog,
    modifySchedule
  },
  props: {
    idList: {
      type: Array,
      default: () => [],
      required: true
    },
    name: {
      type: String,
      default: '',
      required: false
    }
  },
  data() {
    return {
      newTabOpen: true, // 是否新的tab页打开
      imgUrl: imgUrl,
      tableList: [], // 排期bable验证
      showTaskDialog: false, // 状态弹窗
      all_task: [], // 任务列表
      allStatus: [], // 任务所有状态
      taskScheduleEvent: [], // 排期类型
      showHeader: true, // 任务列表的表头是否显示
      curcentList: [], // 当前已选择的列表
      curcentChecked: 0, // 当前已选择的数量
      planChecked: false,
      planHandleType: '', // 任务列表操作类型
      dialogTestReport: false, // 提测
      dialogDailyReport: false, // 日报
      dialogClientReport: false, // 准出
      changeStatusDate: null, // 状态改变时间
      nowChangeTask: null, // 当前正在改变的任务对象
      taskId: '', // 将要修改状态的任务id
      visibleSchedule: false, // 排期弹框
      selectTaskList: [], // 已选任务的id
      total: 0,
      status: null, // 列表状态
      pages: {
        pageSize: 15,
        curIndex: 1
      }
    }
  },
  watch: {
    idList: {
      handler(newV) {
        this.get_allTask()
      },
      deep: true
    },
    name: {
      handler(newV) {},
      immediate: true
    }
  },
  created() {
    this.getTaskStatus()
  },
  methods: {
    setStatus(val) {
      this.status = val
      this.pages.curIndex = 1
      this.get_allTask()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.get_allTask()
    },
    handleCurrentChange(val) {
      this.curIndex = val
      this.get_allTask()
    },
    async get_allTask() { // 获取全部任务
      if (this.idList.length === 0) {
        this.all_task = []
        return
      }
      const res = await taskList({ ids: this.idList, ...this.pages })
      if (res && res.code === 200) {
        this.all_task = res.data
        this.total = res.total
      }
    },
    async getTaskStatus() { // 获取任务状态列表
      const res = await configShowTaskEnum()
      if (res.code === 200) {
        this.taskScheduleEvent = res.data.taskScheduleEvent || []
      }
      const res1 = await configShowTaskStatusEnum(localStorage.getItem('bizId'))
      if (res1.code === 200) {
        this.allStatus = res1.data.taskStatus
      }
    },
    changeCheck(val) {
      if (val) {
        this.all_task.forEach(row => {
          this.$refs.planTable.toggleRowSelection(row, true)
        })
      } else {
        this.$refs.planTable.clearSelection()
      }
    },
    async changeStatus(e) { // 状态改变
      if (e.status === 70 || e.status === 90 || e.status === 100) {
        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: '修改成功', type: 'success', offset: 150 })
          // this.get_allTask()
          this.$emit('change', '任务')// 通知父组件，让父组件去执行子组件的数据更新
        }
      }
    },
    getNew() { // 状态改变成功回调
      this.$emit('change', '任务')// 通知父组件，让父组件去执行子组件的数据更新
    },
    handleSelectionChange(val) { // 任务列表删选操作
      this.tableList = []
      this.tableList = 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
      }
    },
    addSechedule() { // 添加排期
      const taskA = []
      this.tableList.map(item => {
        if (item.isScheduleLocked === 1) {
          taskA.push(item.taskIdSting)
        }
      })
      if (taskA.length !== 0) {
        this.$message({ message: '任务 ' + taskA + ' 的排期已锁定，请先解锁排期后再添加排期', type: 'warning', offset: 150 })
        return
      }
      this.visibleSchedule = true
      this.selectTaskList = this.curcentList
    },
    async filtrateTest() { // 提测筛选
      const data = this.curcentList.map(item => { return item.id })
      const res = await reportdelivertestCheckStatus(data)
      if (res.code === 200) {
        this.dialogTestReport = true
        this.$nextTick(() => {
          this.$refs.TestReport.init(7, this.curcentList.map(item => { return item.id }))
        })
      }
    },
    async filtrateAllow() { // 准出筛选
      const data = this.curcentList.map(item => { return item.id })
      const res = await reportreleaseCheckStatus(data)
      if (res.code === 200) {
        this.dialogClientReport = true
        this.$nextTick(() => {
          this.$refs.ClientReport.init(7, this.curcentList.map(item => { return item.id }))
        })
      }
    },
    async filtrateDaily() { // 建立日报
      const data = this.curcentList.map(item => { return item.id })
      const res = await dailyReportCheckStatus(data)
      if (res.code === 200) {
        this.dialogDailyReport = true
        this.$nextTick(() => {
          this.$refs.DailyReport.init(7, this.curcentList.map(item => { return item.id }))
        })
      }
    },
    link_task(id) { // 跳转到任务详情页
      if (this.newTabOpen) {
        const newTab = this.$router.resolve({ name: '任务详情', query: { id: id }})
        window.open(newTab.href, '_blank')
      } else {
        this.$router.push({ name: '任务详情', query: { id: id }})
      }
    }
  }
}
</script>

<style scoped>
.tagNotification {
  background: rgba(255,137,82,15%);
  color: #FF8952;
  padding: 0 5px;
  border-radius: 8px;
  margin-left: 10px;
}

.tagNotification1 {
  background: rgba(245,108,108,17%);
  color: #F56C6C;
  padding: 0 5px;
  border-radius: 8px;
  margin-left: 10px;
}
</style>
<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;
}
.table-top {
  color: #333333;
  font-size: 16px;
  width: 100%;
  padding: 10px 15px 0 15px;
  display: flex;
  justify-content: space-between;
}
.task-main {
  display: flex;
  flex-direction: column;
  .task-title {
    cursor: pointer;
    color: #666666;
    font-size: 14px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .task-id {
    color: #A7AEBC;
    font-size: 10px;
  }
}
.color-blue {
	color:#409EFF;
}
.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)
}
.status2 {
  @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 {
    width: auto;
    margin-right: 28px;
    color: #606266;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: left;
    cursor: pointer;
  }
  .item-click,.item-line{
    margin: 0;
    width: auto;
    color: #666666;
    font-size: 14px;
    display: flex;
    align-items: center;
    cursor: pointer;
    margin-right: 28px;
    img {
      height: 13.5px;
      widows: 13.5px;
      margin-right: 5px;
    }
  }
  .item-line {
    color:rgba(102,102,102,0.6);
  }
  .cancel {
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
  }
}
.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>
