import storeMixin from '@/common/store-mixin'
import LeaderscheduleService from './leaderschedule-service'
const leaderOfficeMixins = {
  mixins: [storeMixin],
  data() {
    return {
      leaderList: [], // 领导列表
      activeKey: '',
      arrMsId: [],
    }
  },
  created() {
    // 获取秘书领导映射列表
    this.getLeaderList()
    // 领导秘书关系一对多后获取秘书本人id
    this.getMsId()
  },
  methods: {
    getMsId() {
      LeaderscheduleService.getMsId().then((res) => {
        this.arrMsId = res
      })
    },

    // 获取秘书领导映射列表
    getLeaderList() {
      const params = {
        columns: 'id,leaderAccount,leaderName,secretaryName,secretaryAccount',
        expressions: [],
        formId: 'oaLeaderSecretary',
        maxResults: 10,
        orderBy: 'sortNumber asc',
        startPosition: 0,
      }
      LeaderscheduleService.getLeaders(params).then((res) => {
        if (res && res.data && res.data[0]) {
          this.leaderList = res.data
          if (this.$route.query?.type) {
            this.activeKey = this.$route.query.type
          } else {
            this.activeKey = res.data[0].leaderAccount
          }
        }
      })
    },
    // 处理请求参数
    processReq(req) {
      req.data.account = this.activeKey
      return req
    },
    openTaskItem(record, listType) {
      if (listType === 'todo' && !record.seen) {
        record.seen = true
      } else if (listType === 'informleader' && record.opened === 0) {
        record.opened = 1
      }
      // 生成打开文档地址
      const leaderSecretaryMapId = this.arrMsId.find(
        (item) => item.leaderAccount === this.activeKey
      ).secretaryId
      LeaderscheduleService.openTaskItem(
        record,
        listType,
        listType === 'todo' ? this.activeKey : undefined,
        leaderSecretaryMapId
      ).then((refreshFlag) => {
        if (refreshFlag) this.$refs[this.activeKey][0].refresh()
      })
    },
  },
}
export default leaderOfficeMixins
