<template>
  <custom-layout v-loading="loading" :is_menu="false">
    <div class="main w_1300">
      <div class="info">
        <el-descriptions title="赛事介绍" size="large" :column="3" border>
          <el-descriptions-item label="赛事名称">
            {{ info.name }}
          </el-descriptions-item>
          <el-descriptions-item label="赛事类别">
            {{ getDict(info.form, 'form') || '暂无赛事类别' }}
          </el-descriptions-item>
          <el-descriptions-item label="赛事状态">
            {{ getDict(info.match_status, 'status') || '暂无赛事状态' }}
          </el-descriptions-item>
          <el-descriptions-item label="赛事流程状态">
            {{ getDict(info.ext_status, 'ext_status') || '暂无赛事状态' }}
          </el-descriptions-item>
          <el-descriptions-item label="组织单位">
            {{ info.work || '暂无组织单位' }}
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <div class="user" v-if="user && user.id">
        <div class="status">
          <el-steps :active="parseInt(info.ext_status)" align-center finish-status="success">
            <el-step v-for="(item, index) in extList" :key="index" :title="item.label" />
          </el-steps>
        </div>
        <div class="content">
          <one v-if="info.ext_status == '0'" :info="info" @toExamtion="toExamtion" @toExport="toExport"></one>
          <two v-if="info.ext_status == '1' || info.ext_status == '2' || info.ext_status == '3' || info.ext_status == '4'" :info="info" @toStep2="toStep2" @stepFill="stepFill" @toStep3="toStep3" @toStep4="toStep4" @toExport="toExport" @step4Score="step4Score" @toStep5="toStep5" @toFinals="toFinals"></two>
          <thr v-if="info.ext_status == '5' || info.ext_status == '6' || info.ext_status == '7' || info.ext_status == '8'" :info="info" @toExport="toExport" @step5Time="step5Time" @toMessage="toMessage" @toStep6="toStep6" @toPerson="toPerson" @toStep7="toStep7" @toStep8="toStep8"></thr>
        </div>
      </div>
    </div>
  </custom-layout>
</template>

<script setup>
import { get } from 'lodash-es'
const $checkRes = inject('$checkRes')

import { UserStore } from '@/store/user'
const userStore = UserStore()
const user = computed(() => userStore.user)
// 接口
import { DictDataStore } from '@/store/api/system/dictData'
import { MatchStore } from '@/store/api/platform/match'
import { MatchExtStore } from '@/store/api/platform/matchExt'
import { MatchRegStore } from '@/store/api/platform/matchReg'
const matchRegStore = MatchRegStore()
const matchExtStore = MatchExtStore()
const dictDataStore = DictDataStore()
const store = MatchStore()
// 组件
import one from '../match/one.vue'
import two from '../match/two.vue'
import thr from '../match/thr.vue'
// 加载中
const loading = ref(false)
// 路由
const route = useRoute()
// 赛事详情
const info = ref({})

// 字典表
const statusList = ref([])
const formList = ref([])
const extList = ref([])
const isUseList = ref([])
// 请求
onMounted(async () => {
  loading.value = true
  await searchOther()
  await search()
  loading.value = false
})
const searchOther = async () => {
  let result
  // 赛事状态
  result = await dictDataStore.query({ code: 'matchStatus', is_use: '0' })
  if ($checkRes(result)) statusList.value = result.data
  // 赛事状态
  result = await dictDataStore.query({ code: 'matchForm', is_use: '0' })
  if ($checkRes(result)) formList.value = result.data
  // 流程状态
  result = await dictDataStore.query({ code: 'extStatus', is_use: '0' })
  if ($checkRes(result)) extList.value = result.data
  // 是否使用
  result = await dictDataStore.query({ code: 'isUse', is_use: '0' })
  if ($checkRes(result)) isUseList.value = result.data
}
const search = async () => {
  let id = route.query.id
  if (id) {
    let res = await store.fetch(id)
    if (res.errcode == '0') {
      info.value = res.data
    }
  }
}
// 字典数据转换
const getDict = (data, model) => {
  if (data) {
    let res
    if (model == 'form') res = formList.value.find((f) => f.value == data)
    else if (model == 'status') res = statusList.value.find((f) => f.value == data)
    else if (model == 'ext_status') res = extList.value.find((f) => f.value == data)
    return get(res, 'label')
  }
}
// 结束报名
const toExamtion = () => {
  ElMessageBox.confirm('是否确定结束该赛事的报名？', '报名信息', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await matchExtStore.step1({ match_id: route.query.id })
      if ($checkRes(res, true)) {
        search()
      }
    })
    .catch(() => {})
}
// 组织初审
const toStep2 = () => {
  ElMessageBox.confirm('是否确定进入组织初审阶段？', '组织初审', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await matchExtStore.step2({ match_id: route.query.id })
      if ($checkRes(res, true)) {
        search()
      }
    })
    .catch(() => {})
}
// 补充初审时间
const stepFill = (data) => {
  ElMessageBox.confirm('是否确定补充选择用户的初审时间？', '组织初审', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await matchExtStore.stepFill(data)
      if ($checkRes(res, true)) {
        search()
      }
    })
    .catch(() => {})
}
// 公示名单
const toStep3 = () => {
  ElMessageBox.confirm('是否确定进入初审阶段公示名单？', '公示名单', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await matchExtStore.step3({ match_id: route.query.id })
      if ($checkRes(res, true)) {
        search()
      }
    })
    .catch(() => {})
}
// 初审阶段-赛事进行
const toStep4 = () => {
  ElMessageBox.confirm('是否确定进入初审阶段赛事进行？', '赛事进行', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await matchExtStore.step4({ match_id: route.query.id })
      if ($checkRes(res, true)) {
        search()
      }
    })
    .catch(() => {})
}
// 导出
const toExport = async () => {
  const res = await matchRegStore.exportReg(route.query.id)
  if ($checkRes(res, true)) {
    window.open(res.data)
  }
}
// 上传初审分数
const step4Score = async (data) => {
  const res = await matchExtStore.step4Score(data)
  if ($checkRes(res, true)) {
    search()
  }
}
// 进入决赛的名单
const toFinals = async (data) => {
  ElMessageBox.confirm('是否确定选择改报名信息进入决赛名单？', '决赛名单', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await matchExtStore.step4To5(data)
      if ($checkRes(res, true)) {
        search()
      }
    })
    .catch(() => {})
}
// 决赛组织阶段
const toStep5 = async () => {
  ElMessageBox.confirm('是否确定进入决赛组织阶段？', '赛事进行', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await matchExtStore.step5({ match_id: route.query.id })
      if ($checkRes(res, true)) {
        search()
      }
    })
    .catch(() => {})
}
// 补充决赛时间
const step5Time = (data) => {
  ElMessageBox.confirm('是否确定补充选择用户的决赛时间？', '决赛时间', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await matchExtStore.step5Time(data)
      if ($checkRes(res, true)) {
        search()
      }
    })
    .catch(() => {})
}
// 发送消息提示
const toMessage = () => {
  ElMessageBox.confirm('是否确定对已经设置决赛时间的报名用户发送消息提示？', '发送消息提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      let id = route.query.id
      const res = await matchExtStore.step5Sms(id)
      if ($checkRes(res, true)) {
        search()
      }
    })
    .catch(() => {})
}
// 进入决赛名单公示阶段
const toStep6 = () => {
  ElMessageBox.confirm('是否确定进入决赛名单公示阶段？', '赛事进行', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await matchExtStore.step6({ match_id: route.query.id })
      if ($checkRes(res, true)) {
        search()
      }
    })
    .catch(() => {})
}
// 补充决赛人员
const toPerson = (data) => {
  ElMessageBox.confirm('是否确定补充该人员参加决赛？', '赛事进行', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await matchExtStore.step5Sup({ id: route.query.id, user_id: data.user_id })
      if ($checkRes(res, true)) {
        search()
      }
    })
    .catch(() => {})
}
// 进入决赛赛事进行阶段
const toStep7 = () => {
  ElMessageBox.confirm('是否确定进入决赛赛事进行阶段？', '赛事进行', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await matchExtStore.step7({ match_id: route.query.id })
      if ($checkRes(res, true)) {
        search()
      }
    })
    .catch(() => {})
}
// 进入决赛赛事结束阶段
const toStep8 = () => {
  ElMessageBox.confirm('是否确定进入决赛赛事结束阶段？', '赛事进行', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      const res = await matchExtStore.step8({ match_id: route.query.id })
      if ($checkRes(res, true)) {
        search()
      }
    })
    .catch(() => {})
}
provide('extList', extList)
provide('isUseList', isUseList)
</script>
<style scoped lang="scss">
.main {
  margin: 10px auto;
  .info {
    margin: 20px 0;
  }
  .user {
    .status {
      margin: 50px 0;
    }
    .content {
    }
  }
}
</style>
