<template>
  <div v-if="visible" v-dialogResizeStandard="'can-resize-standard'" class="standard-dialog can-resize-standard">
    <div class="standard-head">
      <span>评分标准</span>
      <div class="head-btn-box flex justify-center items-center" @click="closeDialog">
        <el-icon><close /></el-icon>
      </div>
    </div>
    <div class="standard-body">
      <div id="my-iframe-mask"></div>
      <base-form class="p-t-base" size="small" :model="formModel" :items="formItems" :label-width="'78px'">
        <template #form-item-search>
          <el-button :loading="loading" type="primary" @click="onSearch">查询</el-button>
          <!-- <span v-if="mainStore.markerPausedLimit > 0" class="limit-time">{{ limitTime }}</span> -->
        </template>
      </base-form>
      <iframe
        v-if="showIframe"
        style="width: 100%; height: calc(100% - 60px); prevent-events: pointer"
        :src="iframeSrc"
      ></iframe>
    </div>
  </div>
</template>

<script setup lang="ts" name="StandardDialog">
import useVModel from '@/hooks/useVModel'
import { ref, computed, reactive, watch } from 'vue'
import { Close } from '@element-plus/icons-vue'
import { ElIcon, ElButton } from 'element-plus'
import useFetch from '@/hooks/useFetch'
import useMainStore from '@/store/main'
import useOptions from '@/hooks/useOptions'
import useForm from '@/hooks/useForm'
import BaseForm from '@/components/element/BaseForm.vue'

const mainStore = useMainStore()
const { subjectList, mainQuestionList, dataModel, changeModelValue, onOptionInit, isExpert, isLeader } = useOptions([
  'subject',
  'question',
])
const showIframe = ref(false)
const formModel = reactive<any>({
  mainNumber: dataModel.question,
  subjectCode: dataModel.subject,
})
watch(dataModel, () => {
  formModel.mainNumber = dataModel.question
  formModel.subjectCode = dataModel.subject
})
const { defineColumn, _ } = useForm()

const span10 = defineColumn(_, '', { span: 9 })

const formItems = computed<any[]>(() => [
  span10({
    rowKey: 'row-1',
    label: '科目',
    prop: 'subjectCode',
    slotType: 'select',
    labelWidth: '46px',
    slot: { options: subjectList.value, onChange: changeModelValue('subject'), disabled: !isExpert.value },
  }),
  span10({
    rowKey: 'row-1',
    label: '大题',
    prop: 'mainNumber',
    labelWidth: '46px',
    slotType: 'select',
    slot: {
      options: mainQuestionList.value,
      onChange: changeModelValue('question'),
      disabled: !isExpert.value && !isLeader.value,
    },
  }),
  { rowKey: 'row-1', slotName: 'search', labelWidth: '10px', colProp: { span: 6 } },
])

const props = defineProps<{
  modelValue: boolean
}>()
const visible = useVModel(props)
const closeDialog = () => {
  visible.value = false
}

const { fetch: fetchStandard, result: standardRes, loading } = useFetch('getMarkingStandard')
const iframeSrc = computed(() => {
  return standardRes.value?.url
    ? standardRes.value?.url + '#view=FitH&scrollbar=0&toolbar=0&statusbar=0&messages=0&navpanes=0'
    : ''
})
const onSearch = () => {
  showIframe.value = false
  // fetchStandard({ subjectCode: mainStore?.myUserInfo?.subjectCode, mainNumber: mainStore?.myUserInfo?.mainNumber })
  fetchStandard({ subjectCode: formModel.subjectCode, mainNumber: formModel.mainNumber }).then(() => {
    showIframe.value = true
  })
}
onOptionInit(onSearch)

const limitTime = computed(() => {
  return Math.ceil(mainStore.markerPausedLimit / 1000)
})
</script>
//
<style scoped lang="scss">
.standard-dialog {
  display: flex;
  flex-direction: column;
  position: fixed;
  z-index: 500;
  border-radius: 6px;
  box-shadow: 0px 12px 32px 4px rgba(0, 0, 0, 0.04), 0px 8px 20px rgba(0, 0, 0, 0.08);
  .standard-head {
    background-color: #f8f8f8;
    border-radius: 6px 6px 0 0;
    color: #333;
    font-size: 14px;
    height: 44px;
    line-height: 44px;
    padding: 0 10px;
    position: relative;
    .head-btn-box {
      position: absolute;
      right: 0;
      top: 0;
      width: 44px;
      height: 44px;
      z-index: 1;
      cursor: pointer;
      &:hover {
        :deep(i) {
          color: $color--primary;
        }
      }
    }
  }
  .standard-body {
    flex: 1;
    padding: 2px;
    position: relative;
    .limit-time {
      font-size: 16px;
      font-weight: bold;
      color: #000;
      margin-left: 10px;
    }
    #my-iframe-mask {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      top: 0;
      z-index: 10;
      display: none;
    }
  }
}
</style>
