<script setup>
import request from "~/utils/request";
import { resolveSingleFileString, resolveFileString } from '~/utils/helper';
import { getCountDownTime } from '~/utils/day';
import getTeamInfo from '~/store/team';
import { child } from '~/store/child';
import { showSuccessToast } from "vant";


const props = defineProps({
  id: {
    type: String,
    default: undefined,
  },
})

const router = useRouter()
function handleNavLeftClick() {
  router.back()
}

let loading = $ref(true)
let data = $ref()
request({
  url: 'khfw/kcbkb_ksgl/detail',
  data: {
    kkks_id: props.id,  
    token: child.token
  }
}).then(res => {
  if (res.code === '1') {
    data = res.data.one_info

    if (data.sub_info.pj.list.length) {
      overlayForm.kkc_kcpjxj = data.sub_info.pj.list[0].kkc_kcpjxj
      overlayForm.kkc_jspjxj = data.sub_info.pj.list[0].kkc_jspjxj
      overlayForm.kkc_fwpjxj = data.sub_info.pj.list[0].kkc_fwpjxj
      overlayForm.kkc_kcpjnr = data.sub_info.pj.list[0].kkc_kcpjnr
    } else {
      disabled = false
    }

    loading = false
  }

})

const overlayForm = reactive({
  kkc_kcpjxj: 5,
  kkc_jspjxj: 5,
  kkc_fwpjxj: 5,
  kkc_kcpjnr: '',
})

let disabled = $ref(true)

function handleOverlaySubmit() {
  // overlayShow = false
  getTeamInfo.then(res => {
    const submitForm = {
      kkb_id: data.kkb_id,
      kk_id: data.kkb_id,

      kxq_code: res.kxq_code,
      kxq_name: res.kxq_name,

      kkbk_id: data.kkbk_id,
      kkc_sszt: data.kkks_sszt,

      ...overlayForm,
    }

    request({
      url: '/khfw/kcpj/add',
      data: {
        khfw_kcpj: submitForm,
        token: child.token
      }
    }).then(res => {
      if (res.code === '1') {
        disabled = true
        showSuccessToast('提交成功')
      }
    })
  })

}

// 周一待讨论
// 1. 教案数量
// 2. 上传文件数据格式 重要
// 3. 评价限制，本人评价
</script>

<template>
  <van-sticky>
    <van-nav-bar title="课程安排详情" left-arrow @click-left="handleNavLeftClick">
    </van-nav-bar>
  </van-sticky>

  <div class="p-2">
    <van-skeleton title avatar :row="3" :loading="loading">
      <div class="my-2">
        <div class="font-bold text-lg py-1">{{ data.kkks_name }}</div>
        <div class="py-1 flex justify-between text-sm text-dark-700 dark:text-light-900">
          <div class="mr-2">{{ data.kkks_start_datetime }} - {{ data.kkks_end_datetime }}</div>
          <div>{{ data.kkks_skjs }}</div>
        </div>
        <div class="py-1 flex text-sm ">
          <div>课程教案：</div>
          <div class="text-blue-500 underline">
            <div v-for="item in data.sub_info.ja">
              <a :href="resolveSingleFileString(item.kkj_fj).url">{{ resolveSingleFileString(item.kkj_fj).name }}</a>
            </div>
          </div>
        </div>
      </div>

      <div>
        <div class="font-bold text-lg pre my-2">课堂情况反馈</div>
        <div class="text-sm text-dark-600 dark:text-light-900">
          {{ data.sub_info.fk.kkk_content }}
        </div>
        <div class="flex flex-wrap">
          <div v-for="item in resolveFileString(data.sub_info.fk.kkk_img)" class="w-1/3 flex justify-center h-110px">
            <img :src="item.url" :alt="item.name" class="w-110px object-cover">
          </div>
        </div>
      </div>

      <!--  v-if="getCountDownTime(data.kkks_end_datetime) === 0" -->
      <div class="w-full" v-if="getCountDownTime(data.kkks_end_datetime) === 0">
        <van-divider />

        <div class="text-xs mb-2">课时已结束，您可以对此进行评价</div>

        <van-form @submit="handleOverlaySubmit">
          <van-field name="kkc_kcpjxj" label="课程评价">
            <template #input>
              <van-rate v-model="overlayForm.kkc_kcpjxj" />
            </template>
          </van-field>
          <van-field name="kkc_jspjxj" label="老师评价">
            <template #input>
              <van-rate v-model="overlayForm.kkc_jspjxj" />
            </template>
          </van-field>
          <van-field name="kkc_fwpjxj" label="服务评价">
            <template #input>
              <van-rate v-model="overlayForm.kkc_fwpjxj" />
            </template>
          </van-field>
          <van-field v-model="overlayForm.kkc_kcpjnr" name="kkc_kcpjnr" placeholder="请输入评价内容..." type="textarea"
            rows="4" maxlength="500" show-word-limit autosize />

          <div class="text-xs text-red-600 mt-2">* 只能对课程评价一次，评价后无法修改，请确认后提交</div>

          <div style="margin: 16px;">
            <van-button round block type="primary" native-type="submit" :disabled="disabled">
              {{ disabled ? '已提交' : '提交' }}
            </van-button>
          </div>
        </van-form>
      </div>

    </van-skeleton>
  </div>
</template>
