import { NButton, NSpace, useMessage, NForm, NFormItem } from 'naive-ui';
import { defineComponent, onMounted, reactive, ref } from 'vue';
import styles from '../index.module.less';
import CSelect from '/src/components/CSelect';
import { resetClass } from '../api';
import { useCatchStore } from '/src/store/modules/catchData';
export default defineComponent({
  props: {
    activeRow: {
      type: Object,
      default: () => ({ id: '' })
    },
    subjectList: {
      type: Array,
      default: () => []
    }
  },
  name: 'resetStudent',
  emits: ['close', 'getList', 'confirm'],
  setup(props, { emit }) {
    const catchStore = useCatchStore();
    const data = reactive({
      uploading: false
    });
    const message = useMessage();
    const subjectList = ref([] as any);
    const foemsRef = ref();
    const createClassForm = reactive({
      currentGradeNum: null,
      currentClass: null,
      subjectId: null,
      id: null
    });
    onMounted(async () => {
      // 获取教材分类列表
      await catchStore.getSubjects();
      subjectList.value = [
        { id: null, name: '选择声部' },
        ...catchStore.getSubjectList
      ];

      createClassForm.currentGradeNum = props.activeRow.currentGradeNum;
      createClassForm.currentClass = props.activeRow.currentClass;
      createClassForm.subjectId = props.activeRow.subjectId;
      createClassForm.id = props.activeRow.id;
    });
    const submitForms = () => {
      foemsRef.value.validate(async (error: any) => {
        if (error) {
          return;
        }
        data.uploading = true;
        try {
          await resetClass({ ...createClassForm });
          message.success('修改成功');
          emit('close');
          emit('confirm', {
            lastUseCoursewareId: props.activeRow.lessonCoursewareId,
            unit: props.activeRow.lessonCoursewareKnowledgeDetailId,
            subjectId: createClassForm.subjectId,
            name: props.activeRow.name, // 班级名称
            classGroupId: props.activeRow.id // 班级编号
          });
          emit('getList');
        } catch (e) {
          console.log(e);
        }
        data.uploading = false;
      });
    };
    return () => (
      <div class={[styles.addClass]}>
        <NForm label-placement="left" model={createClassForm} ref={foemsRef}>
          <NFormItem
            path="subjectId"
            rule={[
              {
                required: true,
                message: '请选择声部'
              }
            ]}>
            <CSelect
              {...({
                style: { width: '400px' },
                options: subjectList.value,
                placeholder: '选择声部',
                labelField: 'name',
                valueField: 'id',
                clearable: true
              } as any)}
              v-model:value={createClassForm.subjectId}></CSelect>
          </NFormItem>
        </NForm>
        <NSpace class={styles.btnGroup} justify="center">
          <NButton round onClick={() => emit('close')}>
            取消
          </NButton>
          <NButton
            round
            loading={data.uploading}
            onClick={() => submitForms()}
            type="primary">
            确定
          </NButton>
        </NSpace>
      </div>
    );
  }
});
