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 { resetClass } 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,
      id: null
    });
    onMounted(() => {
      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 {
          const res = await resetClass({ ...createClassForm });
          message.success('修改成功');
          emit('close');
          emit('getList');
          data.uploading = false;
        } 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: 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>
    );
  }
});
