<template>
  <div class="index">
    <el-row>
      <el-col :span="24" class="main" v-loading="loading">
        <el-col :span="24" class="one">
          <div class="one_1">
            <el-radio-group size="large" v-model="searchForm.form" @change="search">
              <el-radio-button v-for="(item, index) in formList" :key="index" :label="item.label" :value="item.value" />
            </el-radio-group>
          </div>
          <div class="one_2">
            <div class="one_left" @click="toAdd">发布赛事</div>
            <div class="one_right">
              <el-input v-model="searchForm.name" style="width: 250px" size="large" placeholder="搜索" @change="search" :suffix-icon="Search" />
            </div>
          </div>
        </el-col>
        <el-col :span="24" class="two">
          <el-table :data="list" style="width: 100%" size="large" :header-cell-style="{ backgroundColor: '#edf3ff' }">
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
            <el-table-column prop="name" align="center" label="赛事名称" />
            <el-table-column prop="start_time" align="center" label="开始时间" width="180" />
            <el-table-column prop="end_time" align="center" label="结束时间" width="180" />
            <el-table-column prop="status" align="center" label="状态" width="180">
              <template #default="scope">
                <div>{{ getDict(scope.row.status, 'status') }}</div>
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="250">
              <template #default="{ row }">
                <!-- <el-link v-if="row.form == '4'" :underline="false" type="primary" @click="toPreliminary(row)" style="margin-right: 10px">报名名单</el-link> -->
                <el-link v-if="row.status == '-2'" :underline="false" type="warning" @click="toExam(row)" style="margin-right: 10px">提交审核</el-link>
                <el-link v-if="row.match_status == '2' && row.form != '4' && row.form != '3'" :underline="false" type="primary" @click="toScore(row)" style="margin-right: 10px"> 分数 </el-link>
                <el-link v-if="row.form != '4'" :underline="false" type="warning" @click="toSign(row)" style="margin-right: 10px">报名</el-link>
                <el-link :underline="false" type="primary" @click="toEdit(row)" style="margin-right: 10px">修改</el-link>
                <el-link v-if="row.status != '1'" :underline="false" type="danger" style="margin-right: 10px" @click="toDelete(row)"> 删除 </el-link>
                <el-link :underline="false" type="primary" @click="toMatch(row)" style="margin-right: 10px">赛事状态修改</el-link>
                <el-link v-if="row.form == '4'" :underline="false" type="primary" @click="toExtOne(row)">赛事设置</el-link>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
        <el-col :span="24" class="thr">
          <el-pagination background layout="prev, pager, next" :total="total" :page-size="limit" v-model:current-page="currentPage" @current-change="changePage" @size-change="sizeChange" />
        </el-col>
      </el-col>
    </el-row>
    <el-dialog v-model="dialog.show" :title="dialog.title" :destroy-on-close="false" @close="toClose">
      <el-row>
        <el-col :span="24" v-if="dialog.type == '1'">
          <el-form ref="formRef" :model="form" :rules="rules" label-width="auto">
            <el-tabs v-model="activeName" type="card">
              <el-tab-pane label="基本信息" name="first">
                <el-form-item label="封面" prop="file">
                  <custom-upload model="file" :list="form.file" :limit="1" listType="picture-card" url="/files/web/cxyy_match/upload" @change="onUpload"></custom-upload>
                </el-form-item>
                <el-form-item label="赛事名称" prop="name">
                  <el-input size="large" v-model="form.name" placeholder="请输入赛事名称" />
                </el-form-item>
                <el-form-item label="类别" prop="form">
                  <el-select v-model="form.form" placeholder="请选择类别" size="large" style="width: 100%">
                    <el-option v-for="item in formList" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </el-form-item>
                <el-form-item label="标签" prop="tags">
                  <el-select size="large" v-model="form.tags" multiple filterable allow-create default-first-option :reserve-keyword="false" placeholder="请选择标签" style="width: 100%">
                    <el-option v-for="item in tagsList" :key="item.id" :label="item.title" :value="item.title" />
                  </el-select>
                </el-form-item>
                <el-form-item label="类型" prop="type">
                  <el-select v-model="form.type" placeholder="请选择类型" size="large" style="width: 100%">
                    <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </el-form-item>
                <el-form-item label="赛事规模" prop="scale">
                  <el-radio-group v-model="form.scale">
                    <el-radio v-for="item in scaleList" :key="item.value" :value="item.value" size="large">{{ item.label }}</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="赛事类型" prop="match_type">
                  <el-radio-group v-model="form.match_type">
                    <el-radio v-for="item in matchTypeList" :key="item.value" :value="item.value" size="large">{{ item.label }}</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="路由" prop="href" v-if="form.match_type == '1'">
                  <el-input size="large" v-model="form.href" placeholder="请输入路由" />
                </el-form-item>
                <el-form-item label="组织单位" prop="work">
                  <el-input size="large" v-model="form.work" placeholder="请输入组织单位" />
                </el-form-item>
                <el-form-item label="行业" prop="industry">
                  <el-select v-model="form.industry" placeholder="请选择行业" size="large" style="width: 100%">
                    <el-option v-for="item in industryList" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </el-form-item>
                <el-form-item label="奖金(万元)" prop="money">
                  <el-input size="large" v-model="form.money" placeholder="请输入奖金(万元)" />
                </el-form-item>
                <el-form-item label="有效期" prop="time">
                  <el-date-picker v-model="form.time" type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"> </el-date-picker>
                </el-form-item>
                <el-form-item label="是否启用" prop="is_use">
                  <el-radio-group v-model="form.is_use">
                    <el-radio v-for="item in isUseList" :key="item.value" :value="item.value" size="large">{{ item.label }}</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="是否公开路演人信息" prop="is_show" v-if="form.form == '3'">
                  <el-radio-group v-model="form.is_show">
                    <el-radio v-for="item in isUseList" :key="item.value" :value="item.value" size="large">{{ item.label }}</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="视频" prop="vide">
                  <custom-upload model="video" :list="form.video" :limit="1" url="/files/web/cxyy_match/upload" @change="onUpload"></custom-upload>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                  <el-input size="large" v-model="form.address" type="textarea" placeholder="请输入地址" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                  <el-input size="large" v-model="form.remark" type="textarea" placeholder="请输入备注" />
                </el-form-item>
              </el-tab-pane>
              <el-tab-pane label="拓展信息" name="second">
                <el-form-item label="" prop="file">
                  <div class="rules">
                    <custom-form v-model="form.rules" :fields="rulesFields" :useSave="false">
                      <template #rules1>
                        <WangEditor v-model="form.rules.rules1" />
                      </template>
                      <template #rules2>
                        <WangEditor v-model="form.rules.rules2" />
                      </template>
                      <template #rules3>
                        <WangEditor v-model="form.rules.rules3" />
                      </template>
                      <template #rules4>
                        <WangEditor v-model="form.rules.rules4" />
                      </template>
                      <template #rules5>
                        <WangEditor v-model="form.rules.rules5" />
                      </template>
                      <template #rules6>
                        <WangEditor v-model="form.rules.rules6" />
                      </template>
                      <template #rules7>
                        <WangEditor v-model="form.rules.rules7" />
                      </template>
                      <template #rules8>
                        <WangEditor v-model="form.rules.rules8" />
                      </template>
                      <template #rules9>
                        <WangEditor v-model="form.rules.rules9" />
                      </template>
                      <template #rules10>
                        <WangEditor v-model="form.rules.rules10" />
                      </template>
                      <template #rules11>
                        <WangEditor v-model="form.rules.rules11" />
                      </template>
                      <template #rules12>
                        <WangEditor v-model="form.rules.rules12" />
                      </template>
                    </custom-form>
                  </div>
                </el-form-item>
                <el-form-item label="简介" prop="brief">
                  <WangEditor v-model="form.brief" />
                </el-form-item>
              </el-tab-pane>
              <el-tab-pane label="流程信息" name="third">
                <el-form-item label="" prop="process">
                  <el-col :span="24" class="tables">
                    <el-col :span="24" class="tables_1">
                      <el-button type="primary" @click="addProcess()">添加</el-button>
                    </el-col>
                    <el-col :span="24">
                      <el-table :data="processList" border>
                        <el-table-column type="index" label="序号" width="80" align="center"> </el-table-column>
                        <el-table-column prop="name" label="流程名称" align="center">
                          <template #default="scope">
                            <el-input v-model="scope.row.name" placeholder="请输入流程名称" />
                          </template>
                        </el-table-column>
                        <el-table-column prop="order_num" label="排序" align="center">
                          <template #default="scope">
                            <el-input v-model="scope.row.order_num" type="number" placeholder="请输入排序" />
                          </template>
                        </el-table-column>
                        <el-table-column prop="is_use" label="是否启用" align="center">
                          <template #default="scope">
                            <el-select v-model="scope.row.is_use" placeholder="请选择是否启用">
                              <el-option v-for="item in isUseList" :key="item.value" :label="item.label" :value="item.value" />
                            </el-select>
                          </template>
                        </el-table-column>
                        <el-table-column label="操作" align="center" width="100">
                          <template #default="scope">
                            <el-button type="danger" @click="delProcess(scope.row)">删除</el-button>
                          </template>
                        </el-table-column>
                      </el-table>
                    </el-col>
                  </el-col>
                </el-form-item>
              </el-tab-pane>
              <el-tab-pane label="报名信息设置" name="fourth" v-if="form.form == '4'">
                <el-form-item label="" prop="ext_info">
                  <el-col :span="24" class="add">
                    <el-button type="primary" @click="addInvest()">添加</el-button>
                  </el-col>
                  <el-table :data="investigateList" border>
                    <el-table-column type="index" label="序号" width="80" align="center"> </el-table-column>
                    <el-table-column prop="problem" label="字段名称" align="center"> </el-table-column>
                    <el-table-column label="操作" align="center" width="200">
                      <template #default="scope">
                        <el-button type="primary" @click="updateInvest(scope.row)">修改</el-button>
                        <el-button size="mini" type="danger" @click="delInvest(scope.row)">删除</el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-form-item>
              </el-tab-pane>
              <el-tab-pane label="分数纬度设置" name="fifth" v-if="form.form == '4'">
                <el-form-item label="" prop="ext_finals">
                  <el-col :span="24" class="tables">
                    <el-col :span="24" class="tables_1">
                      <el-button type="primary" @click="addFinals()">添加</el-button>
                    </el-col>
                    <el-col :span="24">
                      <el-table :data="finalsList" border>
                        <el-table-column type="index" label="序号" width="80" align="center"> </el-table-column>
                        <el-table-column prop="name" label="纬度名称" align="center">
                          <template #default="scope">
                            <el-input v-model="scope.row.name" placeholder="请输入纬度名称" />
                          </template>
                        </el-table-column>
                        <el-table-column label="操作" align="center" width="100">
                          <template #default="scope">
                            <el-button type="danger" @click="delFinals(scope.row)">删除</el-button>
                          </template>
                        </el-table-column>
                      </el-table>
                    </el-col>
                  </el-col>
                </el-form-item>
              </el-tab-pane>
            </el-tabs>
            <el-col :span="24" class="button">
              <el-button type="warning" size="large" @click="toDraftSave(formRef)">保存草稿</el-button>
              <el-button type="primary" size="large" @click="toSave(formRef)">保存并提交审核</el-button>
            </el-col>
          </el-form>
        </el-col>
        <el-col :span="24" v-if="dialog.type == '2'">
          <sign :matchForm="form"></sign>
        </el-col>
        <el-col :span="24" v-if="dialog.type == '3'">
          <custom-form v-model="form" :fields="matchFields" :rules="matchRules" @save="toMatchSave" submitText="保存" :DraftSave="false">
            <template #match_status>
              <el-option v-for="i in matchList" :key="i.id" :label="i.label" :value="i.value"></el-option>
            </template>
          </custom-form>
        </el-col>
        <el-col :span="24" v-if="dialog.type == '4'">
          <score :matchForm="form"></score>
        </el-col>
        <el-col :span="24" v-if="dialog.type == '5'">
          <preliminary :matchForm="form"></preliminary>
        </el-col>
        <el-col :span="24" v-if="dialog.type == '6'">
          <extOne :matchForm="form" :type="1" @toClose="toClose"></extOne>
        </el-col>
        <el-col :span="24" v-if="dialog.type == '7'">
          <extOne :matchForm="form" :type="2" @toClose="toClose"></extOne>
        </el-col>
        <el-col :span="24" v-if="dialog.type == '8'">
          <extOne :matchForm="form" :type="3" @toClose="toClose"></extOne>
        </el-col>
        <el-col :span="24" v-if="dialog.type == '9'">
          <extOne :matchForm="form" :type="4" @toClose="toClose"></extOne>
        </el-col>
      </el-row>
    </el-dialog>
    <el-dialog v-model="dialogVisible" title="报名信息填写" width="800" :destroy-on-close="false" @close="toInvestClose">
      <custom-form v-model="investigateForm" :fields="fourformFields" :rules="fourRules" @save="toInvestigateSave" submitText="保存" :DraftSave="false">
        <template #type>
          <el-option v-for="i in proTypeList" :key="i.value" :label="i.label" :value="i.value"></el-option>
        </template>
        <template #answer>
          <div class="answer" v-if="investigateForm.type == '0' || investigateForm.type == '1' || investigateForm.type == '2' || investigateForm.type == '7'">
            <el-col :span="24" class="add">
              <el-button type="primary" @click="addAnswer()">添加</el-button>
            </el-col>
            <el-table :data="answerList" border>
              <el-table-column type="index" label="序号" width="80" align="center"> </el-table-column>
              <el-table-column prop="text" label="答案" align="center" width="500">
                <template #default="scope">
                  <el-input v-model="scope.row.text" placeholder="请输入答案" />
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" width="100">
                <template #default="scope">
                  <el-button type="danger" @click="delAnswer(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="answer" v-if="investigateForm.type == '8'">
            <custom-upload model="file" :list="answerFile" accept="*" :limit="10" url="/files/web/cxyy_match/upload" @change="onaUpload"></custom-upload>
          </div>
        </template>
        <template #is_must>
          <el-radio v-for="i in isUseList" :key="i.id" :label="i.value">{{ i.label }}</el-radio>
        </template>
      </custom-form>
    </el-dialog>
  </div>
</template>

<script setup>
// 组件
import moment from 'moment'
import sign from './parts/sign.vue'
import score from './parts/score.vue'
import preliminary from './parts/preliminaryMatch.vue'
import extOne from './parts/extOne.vue'
import { Search } from '@element-plus/icons-vue'
import { cloneDeep, get } from 'lodash-es'
const $checkRes = inject('$checkRes')
import { UserStore } from '@/store/user'
const userStore = UserStore()
const user = computed(() => userStore.user)
// 接口
import { MatchStore } from '@/store/api/platform/match'
import { ProcessStore } from '@/store/api/platform/process'
import { DictDataStore } from '@/store/api/system/dictData'
import { TagsStore } from '@/store/api/system/tags'
const store = MatchStore()
const dictDataStore = DictDataStore()
const tagsStore = TagsStore()
const processStore = ProcessStore()
// 加载中
const loading = ref(false)
const match_id = ref('')
const searchForm = ref({})
// 列表
const list = ref([])
let skip = 0
let limit = inject('limit')
const total = ref(0)
const currentPage = ref(1)
// 字典表
const isUseList = ref([])
const statusList = ref([])
const typeList = ref([])
const matchList = ref([])
const formList = ref([])
const tagsList = ref([])
const industryList = ref([])
const matchTypeList = ref([])
const scaleList = ref([])

// 流程列表
const processList = ref([])
// 表单
const formRef = ref()
const form = ref({ video: [], time: [], rules: {} })
const dialog = ref({ type: '1', show: false, title: '发布赛事' })
const activeName = ref('first')
// 分数纬度列表
const finalsList = ref([])
const matchFields = ref([{ label: '赛事状态', model: 'match_status', type: 'select' }])
const matchRules = reactive({ match_status: [{ required: true, message: '请选择赛事状态', trigger: 'blur' }] })
const rules = reactive({
  name: [{ required: true, message: '请输入赛事名称', trigger: 'blur' }],
  form: [{ required: true, message: '请选择类别', trigger: 'blur' }],
  scale: [{ required: true, message: '请选择赛事规模', trigger: 'blur' }],
  match_type: [{ required: true, message: '请选择赛事类型', trigger: 'blur' }],
  work: [{ required: true, message: '请输入组织单位', trigger: 'blur' }]
})
// 赛事规则
const rulesFields = ref([
  { label: '大赛背景', model: 'rules1', custom: true },
  { label: '大赛主题和目标', model: 'rules2', custom: true },
  { label: '大赛基本情况介绍', model: 'rules3', custom: true },
  { label: '赛题任务', model: 'rules4', custom: true },
  { label: '赛程安排', model: 'rules5', custom: true },
  { label: '赛制阶段', model: 'rules6', custom: true },
  { label: '参赛资格', model: 'rules7', custom: true },
  { label: '参赛报名', model: 'rules8', custom: true },
  { label: '奖项设置与奖励办法', model: 'rules9', custom: true },
  { label: '组织单位', model: 'rules10', custom: true },
  { label: '赛事联络', model: 'rules11', custom: true },
  { label: '赛事交流', model: 'rules12', custom: true }
])
// 调查问卷
const investigateList = ref([])
const investigateForm = ref({})
const fourformFields = ref([
  { label: '字段名称', model: 'problem' },
  { label: '类型', model: 'type', type: 'select' },
  {
    label: '答案',
    model: 'answer',
    custom: true,
    display: () => investigateForm.value.type == '0' || investigateForm.value.type == '1' || investigateForm.value.type == '2' || investigateForm.value.type == '7' || investigateForm.value.type == '8'
  },
  { label: '是否必填', model: 'is_must', type: 'radio', display: () => investigateForm.value.type != '7' },
  { label: '备注', model: 'remark', type: 'textarea' }
])
const fourRules = reactive({
  problem: [{ required: true, message: '请输入字段名称', trigger: 'blur' }]
})
const dialogVisible = ref(false)
const proTypeList = ref([
  { value: '0', label: '单选' },
  { value: '1', label: '多选' },
  { value: '2', label: '下拉' },
  { value: '3', label: '数值类型' },
  { value: '4', label: '单行文本' },
  { value: '5', label: '多行文本' },
  { value: '6', label: '图片/文件' },
  { value: '7', label: '数组' },
  { value: '8', label: '附件模板' }
])
// 答案
const answerList = ref([])
const answerFile = ref([])
// 请求
onMounted(async () => {
  loading.value = true
  await searchOther()
  await search()
  loading.value = false
})
const search = async (query = { skip, limit }) => {
  skip = query.skip
  limit = query.limit
  const info = {
    skip: query.skip,
    limit: query.limit,
    user: user.value.id,
    ...searchForm.value
  }
  const res = await store.query(info)
  if (res.errcode == '0') {
    list.value = res.data
    total.value = res.total
  }
}
const searchOther = async () => {
  let result
  // 是否使用
  result = await dictDataStore.query({ code: 'isUse', is_use: '0' })
  if ($checkRes(result)) isUseList.value = result.data
  // 类型
  result = await dictDataStore.query({ code: 'matchType', is_use: '0' })
  if ($checkRes(result)) typeList.value = result.data
  // 赛事类型
  result = await dictDataStore.query({ code: 'activeType', is_use: '0' })
  if ($checkRes(result)) matchTypeList.value = result.data
  // 类别
  result = await dictDataStore.query({ code: 'matchForm', is_use: '0' })
  if ($checkRes(result)) formList.value = result.data
  // 状态
  result = await dictDataStore.query({ code: 'examStatus', is_use: '0' })
  if ($checkRes(result)) statusList.value = result.data
  // 赛事状态
  result = await dictDataStore.query({ code: 'matchStatus', is_use: '0' })
  if ($checkRes(result)) matchList.value = result.data
  // 赛事规模
  result = await dictDataStore.query({ code: 'matchScale', is_use: '0' })
  if ($checkRes(result)) scaleList.value = result.data
  // 标签
  result = await tagsStore.query({ is_use: '0' })
  if ($checkRes(result)) tagsList.value = result.data
  // 行业
  result = await dictDataStore.query({ code: 'matchIndustry', is_use: '0' })
  if ($checkRes(result)) industryList.value = result.data
}
const searchProcess = async () => {
  const info = { match: form.value.id }
  const res = await processStore.query(info)
  if (res.errcode == '0') processList.value = res.data
}
// 字典数据转换
const getDict = (data, model) => {
  if (data) {
    let res
    if (model == 'status') res = statusList.value.find((f) => f.value == data)
    return get(res, 'label')
  }
}
// 添加
const toAdd = () => {
  dialog.value = { type: '1', show: true, title: '发布赛事' }
}
// 修改
const toEdit = async (data) => {
  let res = await store.fetch(data.id)
  if (res.errcode == '0') {
    res.data.time = [data.start_time, data.end_time]
    form.value = res.data
    if (res.data.ext_info && res.data.ext_info.length > 0) investigateList.value = res.data.ext_info
    if (res.data.ext_finals && res.data.ext_finals.length > 0) finalsList.value = res.data.ext_finals
    await searchProcess()
    dialog.value = { type: '1', show: true, title: '修改赛事' }
  }
}
// 报名
const toSign = (data) => {
  form.value = data
  dialog.value = { type: '2', show: true, title: '报名管理' }
}
// 修改赛事状态
const toMatch = (data) => {
  form.value = data
  dialog.value = { type: '3', show: true, title: '赛事状态' }
}
// 分数管理
const toScore = (data) => {
  form.value = data
  dialog.value = { type: '4', show: true, title: '分数管理' }
}
// 查看初审信息
const toPreliminary = (data) => {
  form.value = data
  dialog.value = { type: '5', show: true, title: '报名信息' }
}
// 赛事设置
const toExtOne = (data) => {
  form.value = data
  window.open(`/cxyyMatch/home/?id=${data.id || data._id}`)
}
// 删除
const toDelete = (data) => {
  ElMessageBox.confirm(`您确认删除${data.name}该数据?`, '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
    .then(async () => {
      const res = await store.del(data.id)
      if ($checkRes(res, true)) {
        search({ skip, limit })
      }
    })
    .catch(() => {})
}
const toSave = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid) => {
    if (valid) {
      const data = cloneDeep(form.value)
      const other = { status: '0', user: user.value.id }
      if (data.time && data.time.length > 1) {
        data.start_time = data.time[0]
        data.end_time = data.time[1]
      }
      delete data.time
      if (investigateList.value && investigateList.value.length > 0) data.ext_info = investigateList.value
      if (finalsList.value && finalsList.value.length > 0) data.ext_finals = finalsList.value
      let res
      if (get(data, 'id')) res = await store.update({ ...data, ...other })
      else res = await store.create({ ...data, ...other })
      if (res.errcode == 0) {
        ElMessageBox.alert('赛事发布成功', '赛事发布', {
          confirmButtonText: '确定',
          callback: async () => {
            if (res.data && res.data.id) match_id.value = res.data.id
            await toProcessSave()
            await toClose()
          }
        })
      }
    } else {
      ElMessage({
        message: '有相关必填项没有填写',
        type: 'warning'
      })
    }
  })
}
const toDraftSave = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid) => {
    if (valid) {
      const data = cloneDeep(form.value)
      const other = { status: '-2', user: user.value.id }
      if (data.time && data.time.length > 1) {
        data.start_time = data.time[0]
        data.end_time = data.time[1]
      }
      delete data.time
      if (investigateList.value && investigateList.value.length > 0) data.ext_info = investigateList.value
      if (finalsList.value && finalsList.value.length > 0) data.ext_finals = finalsList.value
      let res
      if (get(data, 'id')) res = await store.update({ ...data, ...other })
      else res = await store.create({ ...data, ...other })
      if (res.errcode == 0) {
        ElMessageBox.alert('赛事发布成功', '赛事发布', {
          confirmButtonText: '确定',
          callback: async () => {
            if (res.data && res.data.id) match_id.value = res.data.id
            await toProcessSave()
            await toClose()
          }
        })
      }
    } else {
      ElMessage({
        message: '有相关必填项没有填写',
        type: 'warning'
      })
    }
  })
}
// 审核保存
const toExam = async (row) => {
  ElMessageBox.confirm(`您确认保存并提交审核该数据?`, '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
    .then(async () => {
      const data = cloneDeep(row)
      let res = await store.update({ id: data.id, status: '0', user: user.value.id })
      if ($checkRes(res, true)) {
        toClose()
      }
    })
    .catch(() => {})
}

// 修改赛事状态
const toMatchSave = async (row) => {
  const data = cloneDeep(row)
  console.log(data)

  if (data.form == '4' && data.match_status == '2') {
    const res = await store.step1({ match_id: data.id })
    if ($checkRes(res, true)) {
      toClose()
    }
  } else {
    const res = await store.update({ id: data.id, match_status: data.match_status })
    if ($checkRes(res, true)) {
      toClose()
    }
  }
}
const toClose = async () => {
  form.value = { time: [], video: [], rules: {} }
  dialog.value = { show: false }
  investigateList.value = []
  finalsList.value = []
  processList.value = []
  activeName.value = 'first'
  await search({ skip, limit })
}
// 上传图片
const onUpload = (e) => {
  const { model, value } = e
  form.value[model] = value
}
// 流程添加
const toProcessSave = async () => {
  for (const val of processList.value) {
    const data = cloneDeep(val)
    delete data.sid
    if (!data.match) data.match = match_id.value
    if (get(data, 'id')) await processStore.update(data)
    else await processStore.create(data)
  }
}
// 流程添加
const addProcess = () => {
  let list = processList.value || []
  list.push({ sid: moment().valueOf(), name: '', time: '', is_use: '', match: form.value.id })
  processList.value = list
}
// 流程删除
const delProcess = async (e) => {
  if (e.sid) {
    let list = processList.value.filter((i) => i.sid != e.sid)
    processList.value = list
  } else {
    let list = processList.value.filter((i) => i.id != e.id)
    processList.value = list
  }
  if (e.id) await processStore.del(e.id)
}
// 分页
const changePage = (page = currentPage.value) => {
  search({ skip: (page - 1) * limit, limit: limit })
}
const sizeChange = (limits) => {
  limit = limits
  currentPage.value = 1
  search({ skip: 0, limit: limit })
}
// 调查问卷填写
const addInvest = () => {
  dialogVisible.value = true
  investigateForm.value = { sid: moment().valueOf(), problem: '', type: '', is_must: '', reply: '', answer: '', remark: '' }
}
// 调查问卷修改
const updateInvest = (e) => {
  dialogVisible.value = true
  if (e.answer && e.answer.length > 0) {
    if (e.type == '8') answerFile.value = e.answer
    else answerList.value = e.answer
  }
  investigateForm.value = e
}
// 删除调查问卷
const delInvest = async (e) => {
  let list = investigateList.value.filter((i) => i.sid != e.sid)
  investigateList.value = list
}
// 保存
const toInvestigateSave = (data) => {
  if (answerList.value && answerList.value.length > 0) data.answer = answerList.value
  else data.answer = answerFile.value
  let investigate = investigateList.value.find((i) => i.sid == data.sid)
  if (investigate) {
    investigateList.value = investigateList.value.map((i) => {
      if (i.sid == data.sid) return data
      else return i
    })
  } else investigateList.value.push(data)
  toInvestClose()
}
const toInvestClose = () => {
  investigateForm.value = {}
  answerList.value = []
  dialogVisible.value = false
}
// 答案添加
const addAnswer = () => {
  let list = answerList.value || []
  list.push({ sid: moment().valueOf(), text: '' })
  answerList.value = list
}
//  答案删除
const delAnswer = async (e) => {
  let list = answerList.value.filter((i) => i.sid != e.sid)
  answerList.value = list
}
// 维度添加
const addFinals = () => {
  let list = finalsList.value || []
  list.push({ sid: moment().valueOf(), name: '' })
  finalsList.value = list
}
// 维度删除
const delFinals = async (e) => {
  let list = finalsList.value.filter((i) => i.sid != e.sid)
  finalsList.value = list
}
// 上传图片
const onaUpload = (e) => {
  const { value } = e
  answerFile.value = value
}
</script>
<style scoped lang="scss">
.main {
  .one {
    margin: 0 0 10px 0;
    .one_1 {
      height: 50px;
    }
    .one_2 {
      height: 50px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .one_left {
        background: #1875df;
        padding: 0 10px;
        height: 30px;
        color: #fff;
        line-height: 30px;
        text-align: center;
        font-size: 16px;
        cursor: default;
      }
    }
  }
  .thr {
    display: flex;
    justify-content: center;
    margin: 20px 0 0 0;
  }
}
.add {
  margin: 0 0 10px 0;
}
.button {
  text-align: center;
  margin: 10px 0 0 0;
}
.tables {
  .tables_1 {
    margin: 0 0 10px 0;
  }
}
</style>
