<template>
  <div id="detail">
    <el-row>
      <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
        <el-col :span="24" class="one">
          <cSearch :is_title="false" :is_back="true" @toBack="toBack"></cSearch>
        </el-col>
        <el-col :span="24" class="two">
          <cForm :span="24" :fields="fields" :form="form" :rules="rules" @save="toSave" label-width="auto">
            <template #information>
              <cEditor v-model="form.information" url="/files/ball/match/upload"></cEditor>
            </template>
            <template #status>
              <el-option v-for="i in statusList" :key="i.value" :label="i.label" :value="i.value"></el-option>
            </template>
          </cForm>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
// 基础
import type { Ref } from 'vue';
import { ref, reactive, onMounted } from 'vue';
import type { FormRules } from 'element-plus';
import { ElMessage } from 'element-plus';
import { useRoute } from 'vue-router';
// 接口
import { MatchStore } from '@/stores/match/match';
import { DictDataStore } from '@/stores/dict/dictData'; // 字典表
import type { IQueryResult } from '@/util/types.util';
const matchAxios = MatchStore();
const dictAxios = DictDataStore();
const route = useRoute();
// 加载中
const loading: Ref<any> = ref(false);
// 表单
let form: Ref<any> = ref({});
const rules = reactive<FormRules>({});
let fields: Ref<any[]> = ref([
  { label: '比赛名称', model: 'name' },
  { label: '开始时间', model: 'start_time', type: 'datetime' },
  { label: '结束时间', model: 'end_time', type: 'datetime' },
  { label: '比赛地点', model: 'address' },
  { label: '报名截止时间', model: 'sign_deadline', type: 'datetime' },
  { label: '比赛信息', model: 'information', custom: true },
  { label: '状态', model: 'status', type: 'select' }
]);
// 字典表
const statusList: Ref<any> = ref([]);
// 请求
onMounted(async () => {
  loading.value = true;
  await searchOther();
  await search();
  loading.value = false;
});
const search = async () => {
  let id = route.query.id;
  if (id) {
    let res: IQueryResult = await matchAxios.fetch(id);
    if (res.errcode == '0') {
      let info: any = res.data as {};
      form.value = info;
    }
  }
};
// 保存
const toSave = async (data) => {
  let res: IQueryResult;
  if (data._id) res = await matchAxios.update(data);
  else res = await matchAxios.create(data);
  if (res.errcode == 0) {
    ElMessage({ type: `success`, message: `维护信息成功` });
    toBack();
  }
};
// 查询其他信息
const searchOther = async () => {
  let res: IQueryResult;
  // 状态
  res = await dictAxios.query({ type: 'match_status' });
  if (res.errcode == '0') statusList.value = res.data;
};
// 返回上一页
const toBack = () => {
  window.history.go(-1);
};
</script>
<style scoped lang="scss"></style>
