<script setup lang="ts">
import type { MjFormProps, MjGridProps } from '#/components/mj/mj-table'

import { useUserStore } from '@vben/stores'

import { studentShareService } from '#/api/service/org/student-share'
import MjTable from '#/components/mj/mj-table'

import { useSearchSchool } from '../hooks/search'

const { userInfo } = storeToRefs(useUserStore())

const srcSchoolRender = useSearchSchool({
  change: () => gridRef.value.reload(),
})
const dstSchoolRender = useSearchSchool({
  change: () => gridRef.value.reload(),
})

// 搜索表单
const searchForm = {
  items: userInfo.value.isOnlyStaff
    ? []
    : [
        {
          field: 'srcSchoolId',
          title: '来源学校',
          span: 6,
          itemRender: srcSchoolRender,
        },
        {
          field: 'dstSchoolId',
          title: '目标学校',
          span: 6,
          itemRender: dstSchoolRender,
        },
      ],
} as MjFormProps

// 表格
const gridRef = ref()
const grid = {
  columns: [
    { type: 'seq' },
    {
      field: 'studentName',
      title: '学员姓名',
    },
    {
      field: 'studentAccount',
      title: '学员账号',
    },
    {
      field: 'operatorName',
      title: '操作员姓名',
    },
    {
      field: 'operatorAccount',
      title: '操作员账号',
    },
    {
      field: 'srcSchoolName',
      title: '来源学校',
    },
    {
      field: 'dstSchoolName',
      title: '目标学校',
    },
  ],
  beforeLoadData(params) {
    if (userInfo.value.isSpStaff) {
      // 如果是服务商人员
      params.providerId = userInfo.value.providerId
    } else if (userInfo.value.isStaff) {
      // 如果是学校人员
      params.schoolId = userInfo.value.schoolId
    }
    return params
  },
} as MjGridProps
</script>

<template>
  <div class="student-share g-container">
    <MjTable
      ref="gridRef"
      delete-button-text="取消共享"
      :search-form="searchForm"
      :grid="grid"
      :service="studentShareService"
      v-bind="$attrs"
    />
  </div>
</template>

<style lang="scss" scoped></style>
