<script lang="ts" setup>
import { editCharacter } from '../../api'
import type { FormInstance, FormRules } from 'element-plus'

const emit = defineEmits<Emits>()
const ruleFormRef = ref<FormInstance>()
// 封面状态
const form = reactive({
  name: ''
})
const rules = reactive<FormRules>({
  name: [{ required: true, message: '请输入节名称', trigger: 'blur' }]
})
const props = defineProps({
  isEditResourcesNameDialog: {
    type: Boolean,
    required: true
  },
  course_id: {
    type: String,
    required: true
  },
  chapterName: {
    type: String,
    required: true
  },
  sectionName: {
    type: String,
    required: true
  },
  chapterID: {
    type: String,
    required: true
  },
  resourceId: {
    type: String,
    required: true
  }
})
interface Emits {
  (e: 'update:isEditResourcesNameDialog', isEditResourcesNameDialog: boolean): void
  (e: 'create'): void
}

// 取消
const handleCancel = () => {
  emit('update:isEditResourcesNameDialog', false)
}
// 确定
const handleConfirm = () => {
  const params = Object.assign({ resource_id: props.resourceId, id: props.chapterID, course_id: props.course_id }, form)
  console.log(params, '123', props.resourceId)
  editCharacter(params).then(() => {
    emit('update:isEditResourcesNameDialog', false)
    emit('create')
  })
}
onMounted(() => {
  form.name = props.sectionName
})
</script>
<template>
  <el-dialog
    :model-value="isEditResourcesNameDialog"
    draggable
    :before-close="handleCancel"
    width="30%"
    title="编辑:资源"
  >
    <el-form :model="form" :rules="rules" ref="ruleFormRef" label-width="100px" label-suffix="：">
      <!-- <el-form-item label="所属章" prop="chapterName">
        <p>{{ chapterName }}</p>
      </el-form-item> -->
      <el-form-item label="资源名称" prop="name">
        <el-input v-model="form.name" maxlength="25" onkeydown="if (event.keyCode===13){return false;}"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span>
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
