<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { useGetCategoryList } from '@/composables/useGetCategoryList'
import { createVideo } from '../api'
import UploadMultipleVideo from '../components/UploadMultipleVideo.vue'

// const emit = defineEmits<Emits>()

const router = useRouter()

let { list: selectTree }: any = useGetCategoryList()
const defaultProps = { children: 'children', label: 'category_name', value: 'id' }
const closeStatus = ref(true)
const form = reactive({
  source_id: '',
  name: '0',
  classification: ''
})

// interface Emits {
//   (e: 'update:modelValue', modelValue: boolean): void
//   (e: 'update'): void
// }
const uploadMultipleVideoRef = ref<InstanceType<typeof UploadMultipleVideo> | null>(null)
// const handleConfirm = () => {
//   emit('update:modelValue', false)
//   emit('update')
// }
// const handleCancel = () => {
//   const fileList = uploadMultipleVideoRef?.uploader?._uploadList || []
//   if (fileList.length) {
//     for (const item of fileList) {
//       if (item.state === 'Uploading') {
//         return ElMessage.error('请先完成上传')
//       }
//     }
//   }
//   emit('update:modelValue', false)
// }
// 上传视频成功
const uploadVideo = (data: any) => {
  const { file, videoId } = data
  const params = {
    name: file.name.slice(0, file.name.lastIndexOf('.')),
    source: '2',
    classification: form.classification,
    knowledge_points: '',
    source_id: videoId,
    cover: ''
  }
  createVideo(params).then(() => {
    ElMessage.success('视频上传成功')
  })
}
const handleClose = (val: any) => {
  closeStatus.value = val.closeStatus
}

const goBack = function () {
  router.push('/resource/video')
}
</script>
<template>
  <el-form :model="form" label-suffix="：">
    <el-form-item label="视频分类" prop="classification">
      <el-tree-select
        node-key="id"
        :render-after-expand="false"
        :props="defaultProps"
        v-model="form.classification"
        :data="selectTree"
        clearable
        placeholder="视频分类"
        :default-expanded-keys="selectTree.length ? [selectTree[0]?.id] : []"
      />
    </el-form-item>
    <el-form-item label="视频文件" prop="source_id" v-if="form.classification !== ''">
      <UploadMultipleVideo @upload="uploadVideo" @canClose="handleClose" ref="uploadMultipleVideoRef" />
    </el-form-item>
    <el-form-item label="视频名称" prop="name">
      <el-radio-group v-model="form.name">
        <el-radio label="0">源自文件名称</el-radio>
      </el-radio-group>
    </el-form-item>
    <!-- disabled -->
    <el-form-item>
      <el-button type="primary" @click="goBack">保存</el-button>
      <el-button @click="goBack">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<style></style>
