import {
  NButton,
  NSpace,
  useMessage,
  NForm,
  NFormItem,
  NSelect
} from 'naive-ui';
import { defineComponent, onMounted, reactive, ref } from 'vue';
import styles from '../index.module.less';
import CSelect from '/src/components/CSelect';
import { addClass } from '../api';
export default defineComponent({
  props: {
    activeRow: {
      type: Object,
      default: () => ({ id: '' })
    },
    gradeNumList: {
      type: Array,
      default: () => []
    },
    classArray: {
      type: Array,
      default: () => []
    },
    subjectList:{
      type: Array,
      default: () => []
    }
  },
  name: 'resetStudent',
  emits: ['close', 'getList'],
  setup(props, { emit }) {
    const data = reactive({
      uploading: false
    });
    const message = useMessage();
    const foemsRef = ref();
    const createClassForm = reactive({
      currentGradeNum: null,
      currentClass: null,
      subjectId:null
    });
    const submitForms = () => {
      data.uploading = true
      foemsRef.value.validate(async (error: any) => {
        if (error) {
          return;
        }
        try {
          const res = await addClass({ ...createClassForm });
          message.success('新增成功');
          emit('close');
          emit('getList');
          data.uploading = false
        } catch (e) {
          data.uploading = false
          console.log(e);
        }
      });
    };
    return () => (
      <div class={[styles.addClass]}>
        <NForm label-placement="left" model={createClassForm} ref={foemsRef}>
          <NFormItem
            path="currentGradeNum"
            rule={[
              {
                required: true,
                message: '请选择年级'
              }
            ]}>
            <CSelect
              {...({
                style: { width: '400px' },
                options: props.gradeNumList,
                placeholder: '选择年级',
                clearable: true
              } as any)}
              v-model:value={createClassForm.currentGradeNum}></CSelect>
          </NFormItem>
          <NFormItem
            path="currentClass"
            rule={[
              {
                required: true,
                message: '请选择班级'
              }
            ]}>
            <CSelect
              {...({
                style: { width: '400px' },
                options: props.classArray,
                placeholder: '选择班级',
                clearable: true
              } as any)}
              v-model:value={createClassForm.currentClass}></CSelect>
          </NFormItem>
          <NFormItem
            path="subjectId"
            rule={[
              {
                required: true,
                message: '请选择声部'
              }
            ]}>
            <CSelect
              {...({
                style: { width: '400px' },
                options: props.subjectList,
                placeholder: '选择声部',
                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>
    );
  }
});
