<!-- 任务管理列表-->
<template>
  <el-main class="bg_white wrap_box lcpg lcrw">
    <!-- 搜索组 -->
    <lcSearch class="lcS" ref="theSearch" @searchhandle="searchhandle" @resethandel='resethandel' type="task">
    </lcSearch>
    <!-- 表格 -->
    <div class="task-index" style="width:  calc( 100%-32px ); padding:0 16px 16px 16px;position: relative; ">
      <!-- 按钮组 -->
      <LcButtons ref="lcButtons" :type="2" @btsEmit="btsEmit($event)" :deppgetPageInfo="getPageInfo" task="task"
        :ids="data.theids">
        <template #db>
          <el-checkbox v-model="data.checked" label="我的待办" @change="mychange"></el-checkbox>
        </template>
      </LcButtons>
      <vxe-table id='tasktable' @checkbox-change="selectionChange" @checkbox-all="selectionChange" max-height="550"
        :custom-config="{ storage: true }" border show-overflow :data="data.tasklsittable" keep-source
        :column-config="{ resizable: true }" style="margin-top: 15px;"
        :edit-config="{ trigger: 'click', mode: 'cell', showUpdateStatus: true, showInsertStatus: true }" class="ggdycss">
        <vxe-column type="checkbox" width="50" align="center"></vxe-column>
        <vxe-column field="sn" title="任务编号" width="100" align="center"></vxe-column>
        <vxe-column title="所属项目名称" width="240" align="left">
          <template #default="{ row }">
            <span style="color:#409eff;cursor: pointer;" @click="getRouteInfo(row, '项目')">{{ row.project_name }}</span>
          </template>
        </vxe-column>
        <vxe-column field="name" title="任务名称" width="180" align="left">
          <template #default="{ row }">
            <span style="color:#409eff;cursor: pointer;" @click="getRouteInfo(row, '任务')">{{ row.name }}</span>
          </template>
        </vxe-column>
        <vxe-column field="leader_user_name" title="任务负责人" width="120" align="left"> </vxe-column>
        <vxe-column field="reviewer_user_name" title="任务审核人" width="120" align="left"> </vxe-column>
        <vxe-column field="task_status_text" title="业务状态" width="120" align="left"></vxe-column>
        <vxe-column field="examine_status_text" title="审核状态" width="120" align="center"> </vxe-column>
        <vxe-column field="plan_start_time_text" title="预计开始日期" width="220" align="left"> </vxe-column>
        <vxe-column field="end_time_text" title="任务完成日期" align="left"> </vxe-column>
      </vxe-table>
    </div>
    <!-- 分页 -->
    <div class="block">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="data.page" :page-sizes="[50, 100, 150]" :page-size="data.size" class="rwfy"
        layout="total, sizes, prev, pager, next, jumper" :total="data.total">
      </el-pagination>
    </div>
    <!-- 共公单选弹窗 -->
    <LcSingleSelect :key="data.LcSingleIndex" v-model:isShowDialog="data.isShowDialog" v-model:type="data.dia_type"
      v-model:label="data.dia_label" v-model:title="data.dia_title" v-model:list="data.dia_list"
      @getDragDownData="getDragDownData" />
  </el-main>
</template>
<script>
import { defineComponent, onMounted, getCurrentInstance, reactive, ref, onUnmounted, provide } from 'vue';
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { taskMangeSearch_data } from '../../../utils/lc_data'
import { getComUser, changeObjKey, jointStrQuery, queryDataConversion, routeLinksConversion } from '../../../utils/lc_jx_utils/lc_jx.js'
import LcSingleSelect from '../../components/LcSingleSelect.vue/LcSingleSelect.vue';
import { tableDefaultVal, _data } from '../../../utils/lc_jx_utils/lc_task_json.js'
export default defineComponent({
  name: 'taskMange',
  components: { LcSingleSelect },
  setup (props, context) {
    // 获取实例
    const { proxy } = getCurrentInstance()
    let router = useRouter()
    let store = useStore()
    let formdata = reactive(JSON.parse(JSON.stringify(taskMangeSearch_data)))
    provide('project_id', '')
    // 变量
    let data = reactive({ // 搜索组
      selRow: [],
      defaultRow: [],
      tasklsittable: [],
      storeJointIds: '',
      // 分页
      page: 1,
      size: 50,
      total: 0,
      // 共公弹窗
      LcSingleIndex: 1,
      isShowDialog: false,
      checked: true,
      dia_type: null,
      dia_label: '',
      dia_title: '',
      dia_list: [],
      theids: []// 勾选的任务id集合
    })
    const theSearch = ref()
    const keyDown = (e) => {
      if (e.keyCode == 13 || e.keyCode == 100) {
        searchhandle(JSON.parse(JSON.stringify(theSearch.value.theformdata)))
      }
    }
    // 生命周期
    onMounted(async () => {
      if (window.sessionStorage.getItem("taskSearchdata")) {
        formdata = theSearch.value.theformdata
        data.checked = (formdata.my == '1' ? true : false)
      }
      // 初始化请求 - 数据
      getList()
      //绑定监听事件
      window.addEventListener('keydown', keyDown)
      let personFn = getComUser()
      data.defaultRow = tableDefaultVal
      await personFn.then(r => {
        data.dia_list = changeObjKey(r.data)
      })
    })
    onUnmounted(() => {
      //销毁事件
      window.removeEventListener('keydown', keyDown, false)
    })
    // 请求 - 列表
    function getList () {
      let sudata = JSON.parse(JSON.stringify(formdata))
      sudata.end_time = sudata.end_time && sudata.end_time.length > 0 ? sudata.end_time.join('-') : ''
      sudata.plan_start_time = sudata.plan_start_time && sudata.plan_start_time.length > 0 ? sudata.plan_start_time.join('-') : ''
      sudata.my = sudata.my
      sudata.task_status = sudata.task_status == '' ? '-1' : sudata.task_status
      sudata.examine_status = sudata.examine_status == '' ? '-1' : sudata.examine_status
      sudata.page = data.page
      sudata.size = data.size
      proxy.$http.showLoading(false).get('project/task/list', sudata).then(res => {
        if (res.errno == 200) {
          data.defaultRow = queryDataConversion(res.data.data)
          data.tasklsittable = queryDataConversion(res.data.data)
          data.total = res.data.total
          data.selRow = []
        } else {
          proxy.$message.error(res.errmsg)
        }
      })
    }
    function mychange (val) {
      formdata = JSON.parse(JSON.stringify(theSearch.value.theformdata))
      formdata.page = 1
      theSearch.value.theformdata.my = val ? '1' : '0'
      formdata.my = val ? '1' : '0'
      getList()
    }
    function resethandel (value) {
      formdata = JSON.parse(JSON.stringify(taskMangeSearch_data))
      data.checked = true
      formdata.my = 1
      data.page = 1
      getList()
    }
    // 搜索
    function searchhandle (value) {
      formdata = value
      data.page = 1
      getList()
    }
    // 分页
    const handleSizeChange = (size) => {
      data.page = 1
      data.size = size
      getList()
    }
    const handleCurrentChange = (page) => {
      data.page = page
      getList()
    }
    function btsEmit (value) {
      //按钮操作--排期等
      getClickBtnType(value, data.selRow, data.checked)
    }
    // 表格操作按钮 - 回调 value --> checkbox
    const getClickBtnType = (type, selRow, value) => {
      // 通过 - 提交 - 驳回 - 暂停 - 判断是否勾选行
      let arr = ['pass', 'tj', 'cx', 'zt', 'pq', 'zp', 'rybg']
      if (arr.includes(type)) {
        if (selRow.length == 0) {
          proxy.$message.error('请选择需要操作的行!')
          return
        }
      }
      let queryIds = jointStrQuery(selRow)
      if (type === 'pass') {
        comOperaRequest('project/task/pass', queryIds)
      }
      if (type === 'tj') {
        comOperaRequest('project/task/submit', queryIds, 10, type)
      }
      if (type === 'cx') {
        comOperaRequest('project/task/submit', queryIds, 0, type)
      }
      if (type === 'pq') {
        data.LcSingleIndex = data.LcSingleIndex + 1
        data.isShowDialog = true
        data.dia_type = 3
        data.dia_label = '预计开始日期'
        data.dia_title = "任务排期"
        data.storeJointIds = queryIds
      }
      if (type === 'zp') {
        data.LcSingleIndex = data.LcSingleIndex + 1
        data.isShowDialog = true
        data.dia_type = 2
        data.dia_label = '任务负责人'
        data.dia_title = "人员变更"
        data.storeJointIds = queryIds
      }
      if (type === 'rybg') {
        data.LcSingleIndex = data.LcSingleIndex + 1
        data.isShowDialog = true
        data.dia_type = 6
        data.dia_label = '任务审核人'
        data.dia_title = "审核人"
        data.storeJointIds = queryIds
      }
      if (type === 'zt') {
        comOperaRequest('project/task/ch_status', queryIds, 20)
      }
    }
    // 获取路由跳转
    const getRouteInfo = (rowInfo, type) => {
      if (type == '任务') {
        store.state.lcJxStore.isClick = false
        let item = JSON.parse(JSON.stringify(rowInfo))
        router.push({ path: '/lcProject/taskMange/' + routeLinksConversion(item.basetask_id), query: { id: item.project_id, prBasetaskId: item.id } })
      }
      if (type == '项目') {
        router.push({ path: '/lcProject/projectMange/edit', query: { id: rowInfo.project_id } })
      }
    }
    // 公共弹窗
    const getDragDownData = (val, type) => {
      let reqQuery = null
      type == 3 ? reqQuery = { type: 0, ids: data.storeJointIds, plan_start_time: val } : reqQuery = { type: 'leader_user', ids: data.storeJointIds, user_id: val }
      if (type == 3) {
        proxy.$http.showLoading(false).get('project/task/ch_status', reqQuery).then(res => {
          if (res.errno == 200) {
            data.storeJointIds = ''
            formdata.page = 1
            data.defaultRow = []
            getList()
            proxy.$message.success(res.errmsg)
          } else {
            proxy.$message.error(res.errmsg)
          }
        })
      }
      if (type == 2) {
        comPersonRequest(reqQuery)
      }
      if (type == 6) {
        //人员变更
        reqQuery.type = 'reviewer_user'
        comPersonRequest(reqQuery)
      }
    }
    // 公共人员请求
    const comPersonRequest = (reqQuery) => {
      proxy.$http.showLoading(false).get('project/task/ch_user', reqQuery).then(res => {
        if (res.errno == 200) {
          data.storeJointIds = ''
          formdata.page = 1
          getList()
        } else {
          proxy.$message.error(res.errmsg)
        }
      })
    }
    // 公共请求 - mode --> Get
    const comOperaRequest = (url, queryId, val = undefined, type) => {
      let reqQuery = null
      val == undefined ? reqQuery = { ids: queryId } : reqQuery = { ids: queryId, type: val }
      if (type == 'tj' || type == 'cx') {
        reqQuery = { ids: queryId, examine_status: val }
      }
      proxy.$http.showLoading(false).get(url, reqQuery).then(res => {
        if (res.errno == 200) {
          data.storeJointIds = ''
          formdata.page = 1
          proxy.$message.success(res.errmsg)
          getList()
        } else {
          proxy.$message.error(res.errmsg)
        }
      })
    }
    function selectionChange (val) {
      data.selRow = val.records
      data.theids = jointStrQuery(data.selRow)
    }
    return {
      data,
      formdata,
      theSearch,
      resethandel,
      keyDown,
      // 表格
      getClickBtnType,
      getList,
      comOperaRequest,
      comPersonRequest,
      getRouteInfo,
      mychange,
      // 分页
      handleSizeChange,
      handleCurrentChange,
      // 弹窗
      getDragDownData,
      // 搜索
      searchhandle,
      btsEmit,
      selectionChange
    }
  }
})
</script>
<style src="../../../assets/style/lc_jx_style/lc_jx_com.scss" scoped lang="scss"></style>
<style scoped lang="scss">
@import '../../../assets/style/lc.scss';
</style>
