<script>
import TodoListService from '@/todo/todo-list-service'
import { Tooltip, Tag, Icon, Badge } from 'ant-design-vue'
import components from './_import-components/sd-flowcenter-mixins-import'
export default {
  name: 'SdFlowcenterMixins',

  components,
  data() {
    return {
      tableDataReady: false,
    }
  },
  created() {
    if (this.defaultFilter) this.expressions = { ...this.defaultFilter }
    this.getType()
  },
  methods: {
    pjobRender(text, record) {
      let overTimeFlag = ''
      if (record.taskStatus) {
        overTimeFlag = (
          <Tooltip>
            <template slot='title'>
              {record.taskStatus === 'timeout'
                ? `全流程超时后系统已自动处理，您本人未亲自处理`
                : `${record.taskStatusStepName}环节超时后系统已自动处理，您本人未亲自处理`}
            </template>
            <Icon type='sd-sync' class='sd-overtime-icon' />
          </Tooltip>
        )
      }
      return (
        <a class='part-time'>
          <div class='title-box'>
            {overTimeFlag}
            <span
              class={[
                'title-text',
                this.activeKey === 'inform' ? (record.opened === 0 ? 'unread' : '') : '',
              ]}
            >
              <Badge
                dot={this.activeKey === 'inform' ? record.opened === 0 : false}
                title='未查看'
              />
              {record.title}
            </span>
            <Tooltip>
              <template slot='title'>{record.groupPath}</template>
              <Tag vShow={record.srcTrustId} color='blue'>
                兼
              </Tag>
            </Tooltip>
          </div>
        </a>
      )
    },
    getType() {
      if (this.$route && this.$route.query.type) {
        this.activeKey = this.$route.query.type
      }
    },
    // 查看详情
    rowClick(record) {
      if (this.activeKey === 'todo' && !record.seen) {
        record.seen = true
      } else if (this.activeKey === 'inform' && record.opened === 0) {
        record.opened = 1
      }
      TodoListService.openTaskItem(record, this.activeKey).then((refreshFlag) => {
        if (refreshFlag) this.refresh()
      })
    },
    // 刷新当前页面数据
    refresh() {
      const treeName = this.activeKey + 'worktree'
      this.$refs[treeName].refresh()
      this.$refs[this.activeKey].refresh()
    },

    // 表格加载完数据判断是否要刷新左侧树
    tableDataLoaded(pagination) {
      // 搜索的時候不去对比左侧树和列表的数据量
      if (this.$refs[this.activeKey].searchText === '') {
        if (this.tableDataReady) {
          // 用选中节点文档数量判断是否要刷新
          const treeName = this.activeKey + 'worktree'
          if (this.$refs[treeName].selectNode) {
            if (parseInt(this.$refs[treeName].selectNode.props.count) !== pagination.total) {
              this.$refs[treeName].refresh()
            }
          }
        } else {
          this.tableDataReady = true
        }
      }
    },

    // 当工作树选择了业务类型时更新筛选条件
    treeSelectd(ywlxInfo, activeKey) {
      // 点击树时将tableDataReady改一下
      this.tableDataReady = false
      const filterInfo = this.getBusinesstypeInfo(ywlxInfo)

      this.treeId[activeKey] = filterInfo.treeNodeId

      var arrFilter = filterInfo.filter
      // 如果有默认条件，把默认条件加上
      if (this.defaultFilter !== undefined) {
        this.expressions[activeKey] = [...arrFilter, ...this.defaultFilter[activeKey]]
      } else {
        this.expressions[activeKey] = arrFilter
      }
    },

    // 根据传来的业务类型树相关对象获取业务类型id和名称,并根据业务类型id，生成列表要用的过滤条件filter
    getBusinesstypeInfo(item) {
      // 传业务类型树节点过来会有两种情况，在这里分别获取
      let treeNodeId = ''
      let businessTypeIds = ''
      if (item.id !== undefined) {
        treeNodeId = item.id
        businessTypeIds = item.props.businessTypeIds
      } else {
        treeNodeId = item.node.dataRef.id
        businessTypeIds = item.node.dataRef.props.businessTypeIds
      }

      // 这里分为4种情况，根据业务类型树id来判断
      var filter = []
      switch (treeNodeId) {
        case '0': {
          // 为0时说明是根节点，不传查询条件,filter继续为空
          break
        }

        case 'other': {
          // 为other时说明是其他业务类型，传固定的查询条件
          filter.push({
            dataType: 'str',
            name: 'appId',
            op: 'is_null',
          })

          break
        }

        case 'oaDocReceiptExchange': {
          // 为oaDocReceiptExchange时说明是待签收，传固定的查询条件
          filter.push({
            dataType: 'str',
            name: 'module',
            op: 'eq',
            stringValue: 'oaDocReceiptExchange',
          })

          break
        }

        default: {
          // 其他情况均为正常节点，传正常的业务类型id参数
          filter.push({
            dataType: 'str',
            name: 'appId',
            op: 'in',
            stringValue: businessTypeIds,
          })
        }
      }

      return {
        treeNodeId,
        filter,
      }
    },
    // 左侧树加载后重制表格
    foldFirstTime(dataType) {
      this.$refs[dataType].handleResize()
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
:global(.part-time) {
  :global(.title-box) {
    display: flex;
    align-items: center;
    :global(.title-text) {
      overflow: hidden;
      text-overflow: ellipsis;
    }
    :global(.unread) {
      :global(.ant-badge) {
        margin-right: 6px;
        vertical-align: baseline;
      }

      font-weight: 600;
    }
  }
  :global(.ant-tag) {
    margin-right: 0;
    margin-left: 5px;
  }
  :global(.sd-overtime-icon) {
    margin-right: 5px;
    color: $orange-6;
  }
}
</style>
