<template>
  <el-container class="list_main_content fullheight">
    <el-aside
      v-if="!$route.meta.single"
      v-show="asideShow"
      class="aside-tree__wrap"
      width="200px"
    >
      <p class="flow_tree_top_title">待阅事项分类</p>
      <FlowTree
        ref="FlowTree"
        :isAsideShow="isAsideShow"
        :loadCount="loadCount"
        @currentTree="getCurrent"
        @switch-change="handleSwitch"
      ></FlowTree>
    </el-aside>
    <div
      v-if="!$route.meta.single"
      :class="{'navbar-collapse-right': !asideShow}"
      class="navbar-collapse"
      @click="asideShow = !asideShow"
    >
      <div class="navbar-collapse-bg">
        <i
          :class="{
            'el-icon-arrow-left': asideShow,
            'el-icon-arrow-right': !asideShow
          }"
          class="navbar-collapse-arrow"
        ></i>
      </div>
    </div>
    <el-container style="border-left:1px solid #E7EAEC">
      <h3 class="flow_table_title">待阅事项列表</h3>
      <div class="content_top_wrap">
        <el-row class="done_read_btn">
          <el-button size="small" type="primary" @click="noticeTurnDode()"
            >已阅</el-button
          >
        </el-row>
        <el-input
          v-model="tableData"
          clearable
          placeholder="流程事项/流程名称"
          prefix-icon="el-icon-search"
          size="small"
          style="width:200px;"
          @keyup.enter.native="searchEnterFun"
        ></el-input>
        <div class="date_wrap">
          <p>到达时间</p>
          <el-date-picker
            v-model="date"
            :default-time="['00:00:00', '23:59:59']"
            align="right"
            end-placeholder="结束日期"
            size="small"
            start-placeholder="开始日期"
            type="datetimerange"
          >
          </el-date-picker>
        </div>
        <el-row class="btn_wrap">
          <el-col :span="24">
            <el-button
              icon="el-icon-search"
              size="small"
              style="margin-left: 10px"
              type="primary"
              @click="search('find')"
              >查询
            </el-button>
            <el-button icon="el-icon-refresh" size="small" @click="reset"
              >重置</el-button
            >
          </el-col>
        </el-row>
      </div>
      <el-main>
        <!-- default-sort  排序 -->
        <el-table
          v-loading="fullscreenLoading"
          :data="rows"
          :default-sort="{prop: 'createTime', order: 'descending'}"
          header-cell-class-name="todo-header-row"
          row-class-name="todo-row"
          size="medium"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            align="center"
            type="selection"
            width="50"
          ></el-table-column>
          <el-table-column
            align="center"
            label="流程编号"
            prop="procInstId"
            show-overflow-tooltip
            width="160"
          ></el-table-column>
          <el-table-column label="标题" show-overflow-tooltip>
            <template slot-scope="scope">
              <span
                class="todo-subject"
                @click="handleRowClick(scope.row)"
                @contextmenu.prevent="handleRowClick(scope.row, true)"
                >{{ scope.row.subject }}</span
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="流程名称"
            show-overflow-tooltip
            width="150"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.procDefName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="任务名称"
            show-overflow-tooltip
            width="150"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="到达时间"
            prop="createTime"
            width="150"
          >
            <template slot-scope="scope">
              <span> {{ scope.row.createTime | formatDate }} </span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="传阅人"
            prop="ownerName"
            width="90"
          ></el-table-column>
          <el-table-column align="center" label="类型" width="90">
            <template slot-scope="{row}">
              <span class="blue-color">{{ todoReadStatus[row.status] }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer height="52px">
        <el-row justify="end" type="flex">
          <el-pagination
            :current-page="pagination.page"
            :page-size="pagination.pageSize"
            :page-sizes="[10, 20, 50, 100]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </el-row>
      </el-footer>
    </el-container>
  </el-container>
</template>

<script>
const TODO_READ_STATUS = {
  COPYTO: '待阅',
  COMMU: '沟通',
  FOLLOW: '跟踪'
}

import {mapState, mapActions} from 'vuex'
import FlowTree from '@/components/flow/flowTree.vue'
import utils from '@/utils.js'
import process from '@/api/process'
import {Message} from 'element-ui'

export default {
  name: 'todoRead',
  components: {
    FlowTree
  },
  props: {
    defKey: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      todoReadStatus: TODO_READ_STATUS,
      isAsideShow: false,
      asideShow: false,
      tableData: '',
      date: '',
      currentTree: '',
      fullscreenLoading: true,
      ids: '' //传阅任务id集合
    }
  },
  computed: {
    ...mapState('storeProcess', ['rows', 'pagination', 'total']),
    currentSwitchValue() {
      return (
        localStorage.todoReadAsideShow === 'true' ||
        localStorage.todoReadAsideShow === true
      )
    }
  },
  methods: {
    //将当前展开或者收起的值存在localStorage中
    handleSwitch(val) {
      localStorage.todoReadAsideShow = val
    },
    //批量勾选待阅流程执行已阅操作
    handleSelectionChange(val) {
      this.ids = ''
      if (val.length > 0) {
        for (let i = 0; i < val.length; i++) {
          this.ids = this.ids + val[i].id + ','
        }
        this.ids = this.ids.substring(0, this.ids.length - 1)
      }
    },
    noticeTurnDode() {
      if (this.ids != '') {
        this.$store
          .dispatch('storeProcess/noticeTurnDode', this.ids)
          .then(() => {
            this.search('find')
          })
      } else {
        Message.warning('请选择要阅读的任务')
      }
    },
    //Loading 加载中
    openFullScreen() {
      this.fullscreenLoading = true
    },
    handleSizeChange: function(size) {
      //每页下拉显示数据
      this.$store.dispatch('storeProcess/setPaginationSize', size)
      this.search()
    },
    handleCurrentChange: function(currentPage) {
      //点击第几页
      this.$store.dispatch('storeProcess/setPaginationPageNum', currentPage)
      this.search()
    },
    //回车查询
    searchEnterFun: function(e) {
      var keyCode = window.event ? e.keyCode : e.which
      if (keyCode == 13) {
        this.search('find')
      }
    },
    // 查询
    search(str) {
      const currentUser = this.$store.state.user.currentUserDetail.user
      this.openFullScreen()
      if (str == 'find') {
        this.$store.dispatch('storeProcess/setPaginationPageNum', 1)
      }
      let querys = [] //查询条件
      let queryFilter = {}
      let pageBean = {pageBean: this.pagination}
      let beginDate = '' //开始时间
      let endDate = '' //结束时间
      let group = 'main1' //判断条件
      if (this.tableData == '' && (this.date == '' || this.date == null)) {
        if (this.currentTree != '') {
          querys = [
            {
              property: 'bpm_pro_inst.type_id_',
              value: this.currentTree,
              group: 'typeId',
              operation: 'IN',
              relation: 'AND'
            }
          ]
        }
      } else {
        if (this.currentTree != '') {
          querys = [
            {
              property: 'bpm_pro_inst.type_id_',
              value: this.currentTree,
              group: 'typeId',
              operation: 'IN',
              relation: 'AND'
            },
            {
              property: 'bpm_pro_inst.subject_',
              value: this.tableData,
              group: 'main1',
              operation: 'LIKE',
              relation: 'OR'
            },
            {
              property: 'bpm_task_notice.proc_inst_id_',
              value: this.tableData,
              group: 'main1',
              operation: 'LIKE',
              relation: 'OR'
            },
            {
              property: 'bpm_pro_inst.proc_def_name_',
              value: this.tableData,
              group: 'main1',
              operation: 'LIKE',
              relation: 'OR'
            }
          ]
        } else {
          querys = [
            {
              property: 'bpm_pro_inst.subject_',
              value: this.tableData,
              group: 'main1',
              operation: 'LIKE',
              relation: 'OR'
            },
            {
              property: 'bpm_task_notice.proc_inst_id_',
              value: this.tableData,
              group: 'main1',
              operation: 'LIKE',
              relation: 'OR'
            },
            {
              property: 'bpm_pro_inst.proc_def_name_',
              value: this.tableData,
              group: 'main1',
              operation: 'LIKE',
              relation: 'OR'
            }
          ]
        }
        if (this.date != null && this.date != '') {
          beginDate = utils.formatDate(this.date[0].getTime())
          endDate = utils.formatDate(this.date[1].getTime())
          querys.push({
            property: 'bpm_task_notice.create_time_',
            value: [beginDate, endDate],
            group: 'time',
            operation: 'BETWEEN',
            relation: 'AND'
          })
        }
      }
      if (this.defKey) {
        querys.push({
          property: 'bpm_pro_inst.proc_def_key_',
          value: this.defKey,
          operation: 'EQUAL',
          relation: 'AND'
        })
      }
      queryFilter = {pageBean: this.pagination, querys}
      this.$store
        .dispatch('storeProcess/getNoticeTodoReadList', queryFilter)
        .then(() => {
          this.fullscreenLoading = false
        })
    },
    //重置
    reset() {
      this.tableData = ''
      this.date = ''
      this.search('find')
    },
    handleRowClick(row, newWin) {
      let url = ''
      //跟踪任务
      if (row.status == 'FOLLOW') {
        url = '/taskRead/' + row.id + '/FOLLOW'
        if (this.defKey) {
          url = '/sysModuleInstTaskRead/' + row.id + '/FOLLOW/' + this.defKey
        }
      } else if (row.status == 'COMMU') {
        url = '/task/' + row.id + '/0'
        if (this.defKey) {
          url = '/sysModuleInst/' + row.id + '/0/' + this.defKey + '/true'
        }
      } else {
        url = '/inst/' + row.procInstId + '/read/' + row.id
        if (this.defKey) {
          url =
            '/sysModuleInstRead/' +
            row.procInstId +
            '/read/' +
            row.id +
            '/' +
            this.defKey
        }
      }
      if (!newWin) {
        this.$router.push(url)
      } else {
        window.open(
          window.context.front +
            url +
            '?token=' +
            this.$store.state.login.currentUser.token
        )
      }
    },
    //子组件传值父组件(待办分类查询)
    getCurrent(data) {
      if (data.id == 6) {
        this.currentTree = ''
        this.openFullScreen()
        let pageBean = {pageBean: this.pagination}
        this.$store
          .dispatch('storeProcess/getNoticeTodoReadList', pageBean)
          .then(() => {
            this.fullscreenLoading = false
          })
      } else {
        let ids = []
        this.getFlowTrees(data, ids)
        this.currentTree = ids.join(',')
        this.search('find')
      }
    },
    getFlowTrees(data, ids) {
      ids.push(data.id)
      let arr = data.children
      for (var i = 0; i < arr.length; i++) {
        this.getFlowTrees(arr[i], ids)
      }
    },
    loadCount() {
      return process.getNoticeTodoReadCount({})
    }
  },
  created() {
    this.asideShow = this.currentSwitchValue
    this.isAsideShow = this.currentSwitchValue
    let querys = []
    if (this.defKey) {
      querys.push({
        property: 'bpm_pro_inst.proc_def_key_',
        value: this.defKey,
        operation: 'EQUAL',
        relation: 'AND'
      })
    }
    let pageBean = {
      pageBean: {page: '1', pageSize: '10', showTotal: 'true'},
      querys
    } //初次加载默认pageBean
    this.$store
      .dispatch('storeProcess/getNoticeTodoReadList', pageBean)
      .then(() => {
        //页面加载调用tudo.js里面的分页方法
        this.fullscreenLoading = false
      })
  }
}
</script>

<style lang="scss" scoped>
@import '@/assets/css/element-variables.scss';

.el-table ::v-deep th.todo-header-row {
  background-color: #fafafa;
  font-size: 13px;
}

.el-table ::v-deep tr.todo-row {
  font-size: 13px;
  color: $--color-text-regular;
}

.el-container {
  background-color: #fff;
}

.el-row {
  margin-bottom: 20px;
}

.el-header {
  border-bottom: 1.5px solid #ededed;
}

.todo-subject {
  cursor: pointer;
}

.todo-subject {
  color: $--theme-color;
}

.top-title {
  line-height: 58px;
  margin-left: 20px;
  font-size: 16px;
  color: #a2a2a2;
}

::v-deep .el-tree {
  position: relative;
  cursor: default;
  background: #fff;
  color: $--color-text-primary;
  font-size: 13px;
}

.content {
  padding-top: 0;
}

.content_top_wrap {
  display: flex;
  flex-direction: row;
  margin: 10px 0 0 20px;

  ::v-deep .el-input__prefix,
  ::v-deep .el-input__suffix {
    height: 32px;
  }

  ::v-deep .el-date-editor--datetimerange.el-input__inner {
    width: 220px;
  }

  .done_read_btn {
    margin-right: 10px;
  }

  .date_wrap {
    display: flex;

    p {
      width: 54px;
      margin: 8px;
    }
  }
}

@media (max-width: 1152px) {
  .content_top_wrap {
    flex-wrap: wrap;

    ::v-deep .el-row {
      margin-bottom: 0;
    }

    .btn_wrap {
      margin: 10px 0 0 -9px;
    }
  }
}
</style>
