<template>
  <div class="page">

    <el-form size="small" :inline="true"  class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
      <el-form-item label="标题" prop="title">
        <el-input v-model="searchForm.title"></el-input>
      </el-form-item>
      <el-form-item label="流程名称" prop="taskName">
        <el-input v-model="searchForm.taskName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary" @click="refreshList()" size="small" icon="el-icon-search">查询</el-button>
        <el-button @click="resetSearch()" size="small" icon="el-icon-refresh-right">重置</el-button>
      </el-form-item>
    </el-form>

      <div class="top bg-white">
        <vxe-toolbar :refresh="{query: refreshList}" custom>
          <template #buttons>
            <el-button type="primary" size="small" @click="readAll()">全部标记已读</el-button>
            <el-button :disabled="$refs.todoTable && $refs.todoTable.getCheckboxRecords().length === 0" type="warning" size="small" @click="portionRead()">标记为已读</el-button>
          </template>
        </vxe-toolbar>

        <el-tabs v-model="type" type="card" @tab-click="handleClick">
          <el-tab-pane :key="index" v-for="(item,index) in typeList" :label="item.value" :name="item.value" :value="item.id"></el-tab-pane>
        </el-tabs>

        <div style="height: calc(100% - 80px);">
            <vxe-table
              border="inner"
              auto-resize
              resizable
              height="auto"
              :loading="loading"
              size="small"
              ref="todoTable"
              show-header-overflow
              show-overflow
              highlight-hover-row
              :menu-config="{}"
              :print-config="{}"
              :import-config="{}"
              :export-config="{}"
              :data="dataList"
              :checkbox-config="{}">
              <vxe-column type="seq" width="60" title="序号"></vxe-column>
              <vxe-column type="checkbox"  width="40px"></vxe-column>
              <vxe-column  title="实例标题" field="title" >
                <template slot-scope="scope">
                  <el-link  type="primary" :underline="false" @click="todo(scope.row)">{{scope.row.title}}</el-link>
                </template>
              </vxe-column>
              <vxe-column  title="流程名称" field="taskName" > </vxe-column>
              <vxe-column  title="流程发起人" field="createUser" ></vxe-column>
              <vxe-column
                field="createTime"
                title="创建时间">
              </vxe-column>
              <vxe-column  title="读取状态" field="type" ></vxe-column>
            </vxe-table>
            <vxe-pager
              background
              size="small"
              :current-page="tablePage.currentPage"
              :page-size="tablePage.pageSize"
              :total="tablePage.total"
              :page-sizes="[10, 20, 100, 1000, {label: '全量数据', value: 1000000}]"
              :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
              @page-change="currentChangeHandle">
            </vxe-pager>
        </div>
      </div>
       <el-dialog
        title="查看进度"
        :close-on-click-modal="true"
        :visible.sync="visible"
        v-dialogDrag
        width="70%"
        height="600px">
          <flow-chart ref="preview" :processInstanceId="processInstanceId"></flow-chart>
        </el-dialog>
    <WareHouseHistoryPopup ref="wareHouseHistoryPopup"></WareHouseHistoryPopup>
  </div>
</template>

<script>
  import WareHouseHistoryPopup from '@/views/modules/materialManagement/wareHouseSummary/WareHouseHistoryPopup'
  import TaskService from '@/api/flowable/TaskService'
  import NoticeService from '@/api/flowable/NoticeService'
  import SelectUserTree from '@/views/modules/utils/treeUserSelect'
  import pick from 'lodash.pick'
  export default {
    data () {
      return {
        dataList: [],
        typeList: [],
        searchForm: {
          title: '',
          taskName: ''
        },
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        loading: false,
        visible: false,
        currentTask: null,
        processInstanceId: ''
      }
    },
    taskService: null,
    noticeService: null,
    created () {
      this.noticeService = new NoticeService()
      this.taskService = new TaskService()
      this.typeList = [
        {id: '0', value: '未读'},
        {id: '1', value: '已读'}
      ]
    },
    activated () {
      this.refreshList()
    },
    components: {
      WareHouseHistoryPopup,
      SelectUserTree
    },
    methods: {
      // 获取数据列表
      refreshList () {
        this.loading = true
        if (this.commonJS.isEmpty(this.searchForm.type)) {
          this.searchForm.type = '0'
        }
        this.noticeService.list({
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          ...this.searchForm
        }).then(({data}) => {
          this.dataList = data.records
          this.tablePage.total = data.total
          this.loading = false
        })
      },
      // 当前页
      currentChangeHandle ({ currentPage, pageSize }) {
        this.tablePage.currentPage = currentPage
        this.tablePage.pageSize = pageSize
        this.refreshList()
      },
      todo (row) {
        // console.log('row', row)
        if (row.taskName === '库存提醒') {
          // console.log('测试进来了')
          this.$refs.wareHouseHistoryPopup.init(row.defId)
          this.inputForm = {
            id: row.id
          }
          this.noticeService.stockUpdate(this.inputForm)
          this.resetSearch()
        } else {
          this.inputForm = {
            taskId: '',
            noticeId: ''
          }
          this.taskService.getTaskDef({
            procInsId: row.taskId,
            procDefId: row.defId
          }).then(({data}) => {
            this.inputForm.taskId = row.taskId
            this.inputForm.noticeId = row.noticeName
            this.noticeService.update(this.inputForm)
            this.$router.push({
              path: '/flowable/task/TaskForm',
              query: {
                isShow: false,
                formReadOnly: true,
                formTitle: `${row.title}`,
                num: 2,
                title: `审批【${row.taskName || ''}】`,
                ...pick(data, 'formType', 'formUrl', 'procDefKey', 'taskDefKey', 'procInsId', 'procDefId', 'taskId', 'status', 'title', 'businessId')
              }
            })
          })
        }
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      },
      handleClick (tab) {
        const val = tab.$attrs.value
        this.searchForm.type = val
        this.refreshList()
      },

      // 全部已读
      readAll () {
        this.$confirm(`确定将全部信息标记为已读吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.noticeService.readAll().then(({data}) => {
            this.loading = false
            this.$message({
              message: data,
              type: 'success',
              duration: 1500
            })
            this.refreshList()
          })
        })
      },

      // 部分已读
      portionRead (id) {
        let ids = id || this.$refs.todoTable.getCheckboxRecords().map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定将选定信息标记为已读吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.noticeService.portionRead(ids).then(({data}) => {
            this.loading = false
            this.$message({
              message: data,
              type: 'success',
              duration: 1500
            })
            this.refreshList()
          })
        })
      }
    }
  }
</script>
