<template>
  <a-card>
    <a-tabs v-if="leaderList.length > 0" v-model="activeKey">
      <a-tab-pane v-for="leader in leaderList" :key="leader.leaderAccount" :tab="leader.leaderName">
        <sd-data-table
          :ref="leader.leaderAccount"
          data-url="api/framework/v1/office-leader-opinion/leaderopinions/list"
          :columns="columns"
          :actions="actions"
          :row-key="'instId'"
          :search-fields="['title']"
          :filter-expressions="expressions"
          :process-req="processReq"
        >
          <a
            slot="bt"
            slot-scope="text, record"
            target="_blank"
            rel="opener"
            @click="
              openUrl(
                `/sd-webflow/${makePath(
                  record
                )}?openAuthType=leaderDone&openAuthParameter=${encodeURIComponent(activeKey)}`,
                leader.id
              )
            "
          >
            {{ text }}
          </a>
        </sd-data-table>
      </a-tab-pane>
    </a-tabs>
    <a-empty v-else />
  </a-card>
</template>

<script>
import storeMixin from '@/common/store-mixin'
import TableColumnTypes from '@/common/services/table-column-types'
import TodoListService from '@/todo/todo-list-service'
import openAsTrustId from '@/common/services/open-as-trust-id'
import components from './_import-components/sd-leadermanagerlist-import'
import LeaderscheduleService from './leaderschedule-service'

export default {
  name: 'SdLeadermanagerlist',
  metaInfo: {
    title: '领导批示管理',
  },
  components,
  mixins: [storeMixin],
  data() {
    return {
      leaderList: [], // 领导列表
      activeKey: '',
      columns: [
        {
          title: '业务类型',
          dataIndex: 'businessTypeName',
        },
        {
          title: '标题',
          dataIndex: 'title',
          scopedSlots: { customRender: 'bt' },
        },
        {
          title: '批示内容',
          dataIndex: 'approveOpinion',
        },
        {
          title: '批示时间',
          sdRender: TableColumnTypes.dateTime, // 特殊处理的数据类型，时间戳会自动调用sdDateFormat处理
          defaultSortOrder: 'descend', // 没有点击任何排序列时，默认的排序列
          dataIndex: 'approveTime',
          width: '20%',
          sorter: true,
        },
      ],
      actions: [],
      expressions: [
        {
          dataType: 'str',
          name: 'approverAccount',
          op: 'eq',
          stringValue: '',
        },
      ],
      arrMsId: [],
    }
  },
  created() {
    // 获取秘书领导映射配置
    this.getLeaderList()
    // 领导秘书关系一对多后获取秘书本人id
    this.getMsId()
  },
  methods: {
    // 获取秘书领导映射配置
    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
          this.activeKey = res.data[0].leaderAccount
        }
      })
    },
    // 处理请求参数
    processReq(req) {
      req.data.formId = 'oaOfficeLeaderOpinion'
      this.expressions.forEach((item) => {
        // 更新查询参数：领导账户
        if (item.name === 'approverAccount') {
          item.stringValue = this.activeKey
        }
      })
      return req
    },
    makePath(task) {
      // 生成打开文档地址
      return TodoListService.makePath(task, 'todo')
    },
    getMsId() {
      LeaderscheduleService.getMsId().then((res) => {
        this.arrMsId = res
      })
    },
    openUrl(url, id) {
      const leaderSecretaryMapId = this.arrMsId.find(
        (item) => item.leaderAccount === this.activeKey
      ).secretaryId
      openAsTrustId(url, 'MS' + leaderSecretaryMapId)
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
