<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { useGetCategoryList } from '@/composables/useGetCategoryList'
import { createVideo } from '../api'
import UploadMultipleVideo from './UploadMultipleVideo.vue'
const emit = defineEmits<Emits>()
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
}
</script>
<template>
  <!-- <el-dialog title="批量导入视频" :close-on-click-modal="false" :before-close="handleCancel" width="50vw"> -->
  <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>
  </el-form>
  <!-- <template #footer> -->
  <template>
    <el-button type="primary" @click="handleConfirm" :disabled="closeStatus === false">保存</el-button>
    <el-button @click="handleCancel" :disabled="closeStatus === false">取消</el-button>
  </template>
  <!-- </el-dialog> -->
</template>
<style></style>
