<template>
  <div class="index">
    <el-row>
      <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
        <el-col :span="24" class="one">
          <div class="one_left" @click="toAdd">添加分数</div>
          <div class="one_right">
            <el-input v-model="searchForm.matchPath_name" style="width: 250px" size="large" placeholder="搜索" @change="search" :suffix-icon="Search" />
          </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="matchPath_name" align="center" label="流程名称" />
            <el-table-column prop="sign_name" align="center" label="选手" />
            <el-table-column prop="score" align="center" label="分数" width="180" />
            <el-table-column prop="time" align="center" label="时间" width="180" />
            <el-table-column align="center" label="操作" width="180">
              <template #default="{ row }">
                <el-link :underline="false" type="primary" size="mini" @click="toEdit(row)" style="margin-right: 10px">修改</el-link>
                <el-link :underline="false" type="danger" size="mini" @click="toDelete(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'">
          <custom-form v-model="form" :fields="formFields" :rules="rules" @save="toSave" :DraftSave="false" submitText="保存">
            <template #matchPath>
              <el-option v-for="i in matchPathList" :key="i.id" :label="i.name" :value="i.id"></el-option>
            </template>
            <template #sign>
              <el-option v-for="i in signList" :key="i.id" :label="i.name" :value="i.id"></el-option>
            </template>
          </custom-form>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script setup>
import moment from 'moment'
import { cloneDeep, get } from 'lodash-es'
const $checkRes = inject('$checkRes')
// 接口
import { ScoreStore } from '@/store/api/platform/score'
import { SignStore } from '@/store/api/platform/sign'
import { ProcessStore } from '@/store/api/platform/process'
const store = ScoreStore()
const processStore = ProcessStore()
const signStore = SignStore()
// 路由
const searchForm = ref({})

// 列表
const list = ref([])
let skip = 0
let limit = inject('limit')
const total = ref(0)
const currentPage = ref(1)

const props = defineProps({
  matchForm: { type: Object, default: () => {} }
})

const { matchForm } = toRefs(props)

const formFields = ref([
  { label: '流程', model: 'matchPath', type: 'select' },
  { label: '选手', model: 'sign', type: 'select' },
  { label: '分数', model: 'score', type: 'number' }
])
const rules = reactive({
  sign: [{ required: true, message: '请选择选手', trigger: 'blur' }],
  score: [{ required: true, message: '请填写分数', trigger: 'blur' }]
})
const dialog = ref({ type: '1', show: false, title: '维护分数' })
const form = ref({})
// 字典表
const matchPathList = ref([])
const signList = ref([])
// 加载中
const loading = ref(false)
// 请求
onMounted(async () => {
  loading.value = true
  await searchOther()
  await search({ skip, limit })
  loading.value = false
})
const searchOther = async () => {
  let result
  // 流程
  result = await processStore.query({ match: matchForm.value.id, is_use: '0' })
  if ($checkRes(result)) matchPathList.value = result.data
  // 选手
  result = await signStore.query({ match: matchForm.value.id, status: '1' })
  if ($checkRes(result)) signList.value = result.data
}
const search = async (query = { skip, limit }) => {
  skip = query.skip
  limit = query.limit
  const info = { skip: query.skip, limit: query.limit, ...searchForm.value, match: matchForm.value.id }
  const res = await store.list(info)
  if (res.errcode == '0') {
    list.value = res.data
    total.value = res.total
  }
}
// 添加
const toAdd = () => {
  dialog.value = { type: '1', show: true, title: '添加分数' }
}
// 修改
const toEdit = (data) => {
  form.value = data
  dialog.value = { type: '1', show: true, title: '修改分数' }
}
// 删除
const toDelete = async (data) => {
  const res = await store.del(data.id)
  if ($checkRes(res, true)) {
    search({ skip, limit })
  }
}
const toSave = async () => {
  const data = cloneDeep(form.value)
  const other = { time: moment().format('YYYY-MM-DD'), match: matchForm.value.id }
  let res
  if (get(data, 'id')) res = await store.update({ ...data, ...other })
  else res = await store.create({ ...data, ...other })
  if ($checkRes(res, true)) {
    search({ skip, limit })
    toClose()
  }
}
const toClose = () => {
  form.value = {}
  dialog.value = { show: false }
}
// 分页
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 })
}
</script>
<style scoped lang="scss">
.main {
  .one {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0 0 10px 0;
    .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;
  }
}
</style>
