<template>
  <div style="padding: 0 20px">
    <div ref="lxHeader">
      <lx-header icon="md-apps" text="任务大纲" style="margin-bottom: 10px; border: 0px;">
        <slot>
          <div style="display: flex;justify-content: flex-start;flex-wrap: wrap;">
            <DatePicker style='width:200px;margin-right: 10px;' placeholder="年份" placement="bottom" format='yyyy'
              type="year" @on-change="changeYear"></DatePicker>
            <Input v-model="select.keyword" clearable style="width: 200px;margin-right: 10px;" placeholder="关键字搜索" />
            <Button type="primary" @click="getList">查询</Button>
            <Button type="primary" style="margin-left: 10px;" @click="editorOutline('','add')">添加</Button>
          </div>
        </slot>
      </lx-header>
    </div>

    <!-- @cell-click="cellClicks" -->
    <xy-table :list="mission_log" :table-item="missionLogTable" :showIndex='false' :isPage='false'
      :span-method="spanMethod">
      has_mission
      <template v-slot:has_mission>
        <el-table-column align='center' label="是否创建计划或专项任务" fixed="right" width="120" header-align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.mission_plans.length>0">
              已创建计划
            </div>
            <div v-if="scope.row.missions.length>0">
              已创建专项任务
            </div>
          </template>
        </el-table-column>
      </template>
      <template v-slot:btns>
        <el-table-column align='center' label="操作" fixed="right" width="120" header-align="center">
          <template slot-scope="scope">
            <div>
              <Button type="primary" style='margin-right:5px;margin-bottom:5px;' size="small"
                @click="editPlan(scope.row,'add')">年度计划</Button>
            </div>
            <div>
              <Button type="primary" style='margin-right:5px;margin-bottom:5px;' size="small"
                @click="editUnit(scope.row,4)">专题任务</Button>
            </div>
            <div>
              <Button type="primary" style='margin-right:5px;margin-bottom:5px;' size="small"
                @click="editorOutline(scope.row.id,'editor')">编辑大纲</Button>
            </div>
          </template>
        </el-table-column>
      </template>
    </xy-table>
    <addOutline ref="addOutline" @refresh="getList"></addOutline>
    <addPlan ref="addPlan"  @refresh="getList"></addPlan>
    <addUnit ref="addUnit" @toUnit="getList"  @refresh="getList"></addUnit>
  </div>
</template>

<script>
  import {
    index,
    destroy
  } from '@/api/task/newplan.js'
  import {
    getparameteritem
  } from "@/api/system/dictionary.js";
  import {
    getMergeCells,
    groupBy
  } from '@/utils/contactTable.js'
  import addOutline from '../list/components/addOutline.vue'
  import addPlan from '../list/components/addPlan.vue'
  import addUnit from '../list/components/addUnit.vue'
    import state from '@/store/modules/user.js'
  export default {
    components: {
      addOutline,
      addPlan,
      addUnit,
    },
    data() {
      return {
        select: {
          keyword: '',
          table_name: 'new_plans',
          page: 1,
          page_size: 99999,
          year: new Date().getFullYear(),
          sort_name: 'created_at',
          sort_type: 'ASC'
        },
        menuList: [],
        mission_log: [],
        missionLogTable: [{
          label: "工作项目",
          prop: 'menu',
          align: 'center',
          fixed: 'left',
          width: 120
        }, {
          label: "性质级别",
          prop: 'level',
          width: 120,
          align: 'center',
          fixed: 'left'
        }, {
          label: "主要内容",
          prop: 'content',
          align: 'left',
          width: 240
        }, {
          label: "工作周期",
          prop: 'period',
          width: 120
        }, {
          label: "工作要求",
          prop: 'require',
          width: 240
        }, {
          label: "完成时间",
          prop: 'end_time',
          width: 120,
          align: 'center'
        }, {
          label: "责任科室",
          prop: 'duty_dep_id',
          width: 120
        }, {
          label: "参与科室",
          prop: 'join_dep_id',
          width: 120,
        }, {
          label: "具体流程概述",
          prop: 'flow',
          width: 240,
          align: 'left'
        }, {
          label: "所需资源概述",
          prop: 'resource',
          width: 240,
          align: 'left'
        },{
          label: "是否已创建计划或专项任务",
          prop: 'has_mission',
          width: 240,
          align: 'left'
        }],
      }
    },
    computed: {
      // 获取所有单元格合并数据
      spanArr() {
        if (!this.missionLogTable.length) return []
        const mergeCols = ['menu', 'level'] // 需要合并的列（字段）
        const data = groupBy(this.mission_log, 'sort')
        return getMergeCells(data, this.missionLogTable, mergeCols)
      }
    },
    created() {
      // this.getMenuList()
       this.stateObj = state.state
      this.getList()
    },
    methods: {
      async getMenuList() {
        const res = await getparameteritem('main_level')
        this.menuList = res.detail
      },
      async getList() {
        const res = await index({

          ...this.select,
          'with_relations[0]':"missionPlans",
          'with_relations[1]':"missions",
          filter: [{
            key: 'year',
            op: 'eq',
            value: this.select.year
          }, {
            key: 'content',
            op: 'like',
            value: this.select.keyword
          }],
        })
        this.mission_log = res.data
      },
      changeYear(e) {
        if (e) {
          this.select.year = e
        }
      },
      spanMethod({
        row,
        colomn,
        rowIndex,
        columnIndex
      }) {

        return this.spanArr[rowIndex][columnIndex]
      },

      editorOutline(id, type) {
        if (id) {
          this.$refs.addOutline.id = id
        }
        this.$refs.addOutline.setMissionId(this.id)
        this.$refs.addOutline.setName(`${this.select.year}任务大纲`)
        this.$refs.addOutline.type = type
        // this.$refs.addOutline.menuList = this.menuList
        this.$refs.addOutline.isShow = true
      },
      editUnit(row,type){
        this.$refs.addUnit.type="add"
        this.$refs.addUnit.setoutLineId(row.id)
        this.$refs.addUnit.setType(type)
        this.$refs.addUnit.setNameContent(row.content,row.flow)
        this.$refs.addUnit.setMenuLevel(row.menu,row.level,row.menu_id,row.level_id)
        this.$refs.addUnit.setMainId(this.stateObj.department_id,this.stateObj.login_id)
        this.$refs.addUnit.isShow = true
      },
      editPlan(row,type){
        this.$refs.addPlan.type="add"
        this.$refs.addPlan.setMenuLevel(row)
        this.$refs.addPlan.setMainId(this.stateObj.department_id,this.stateObj.login_id)
        this.$refs.addPlan.isShow = true
      },
      toUrlPlan(){
        this.$router.push('/task/list/plan_1')
      },
      toUrlUnit(e){
        this.$router.push('/task/list/unit_4')
      },

    },
    watch: {}
  }
</script>

<style scoped>
  ::v-deep .el-table .cell.el-tooltip {
    white-space: normal !important;
  }
</style>
