<template>
  <div class="flex direction-column full">
    <mark-header
      :exclude-operations="['remark', 'problem', 'example', 'delete', 'bookmark']"
      :paper-path="current?.url"
      @click="onOperationClick"
    ></mark-header>
    <div class="flex flex-1 overflow-hidden p-base mark-container">
      <div
        class="flex flex-1 direction-column radius-base fill-blank mark-content"
        :class="{ 'text-center': center }"
        :style="{ 'background-color': backgroundColor }"
      >
        <right-button class="next-button" @click="checkNext" />
        <div class="flex-1 p-base scroll-auto mark-content-paper">
          <img :src="dataUrl" alt="" class="paper-img" :style="{ 'background-color': frontColor }" />
        </div>
      </div>
      <div class="p-base radius-base fill-blank scroll-auto m-l-base table-view">
        <base-table
          ref="tableRef"
          border
          stripe
          size="small"
          :data="tableData"
          :columns="columns"
          highlight-current-row
          @current-change="onCurrentChange"
        ></base-table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts" name="MarkingViewSample">
/** 查看样卷 */
import { computed } from 'vue'
import { useSetImgBg } from '@/hooks/useSetImgBg'
import useFetch from '@/hooks/useFetch'
import useMarkHeader from '@/hooks/useMarkHeader'
import useTableCheck from '@/hooks/useTableCheck'
import MarkHeader from '@/components/shared/MarkHeader.vue'
import BaseTable from '@/components/element/BaseTable.vue'
import MarkHistoryList from '@/components/shared/MarkHistoryList.vue'
import RightButton from '@/components/shared/RightButton.vue'

import type { SetImgBgOption } from '@/hooks/useSetImgBg'
import type { ExtractApiResponse } from '@/api/api'
import type { MarkHeaderInstance, EpTableColumn } from 'global-type'

type RowType = ExtractArrayValue<ExtractApiResponse<'viewSamplePaper'>> & { index: number }

const {
  rotate,
  scale,
  center,
  frontColor,
  backgroundColor,
  onBack,
  onScaleChange,
  onCenter,
  onRotate,
  setBackgroundColor,
  setFrontColor,
  onViewStandard,
} = useMarkHeader()

/** 刷新 */
const onRefresh = () => {
  viewSamplePaper()
}

type OperationClick = MarkHeaderInstance['onClick']

type OperationType = Parameters<Exclude<OperationClick, undefined>>[0]['type']

const operationHandles: Partial<Record<OperationType, (...args: any) => void>> = {
  back: onBack,
  'scale-change': onScaleChange,
  center: onCenter,
  rotate: onRotate,
  'front-color': setFrontColor,
  'background-color': setBackgroundColor,
  refresh: onRefresh,
  standard: onViewStandard,
}

const onOperationClick: OperationClick = ({ type, value }) => {
  operationHandles[type]?.(value)
}

/** 样卷 */
const columns: EpTableColumn<RowType>[] = [
  { label: '密号', prop: 'secretNumber', width: 100, fixed: 'left' },
  { label: '题目', prop: 'mainTitle' },
  { label: '分数', prop: 'score', width: 50 },
  { label: '提交人', prop: 'markerName' },
  // { label: '提交时间', prop: 'markTime' },
  { label: '给分说明', prop: 'description' },
  { label: '提交时间', prop: 'markTime', width: 160 },
]
const { fetch: viewSamplePaper, result: samplePaperList } = useFetch('viewSamplePaper')

const { tableRef, tableData, current, next: checkNext, onCurrentChange } = useTableCheck(samplePaperList)

viewSamplePaper()

const imgOption = computed<SetImgBgOption>(() => {
  return {
    image: current?.value?.url,
    rotate: rotate.value,
    scale: scale.value,
  }
})

const { drawing, dataUrl } = useSetImgBg(imgOption, frontColor, setFrontColor)
</script>

<style scoped lang="scss">
.mark-container {
  .mark-content {
    position: relative;
    .preview {
      position: absolute;
      cursor: pointer;
      top: 10px;
      right: 20px;
      font-size: 24px;
    }
    .next-button {
      position: absolute;
      right: -20px;
      top: 300px;
    }
    .mark-content-paper {
      img {
        max-width: 100%;
      }
    }
  }
  .table-view {
    width: 580px;
  }
}
</style>
