import {
  NButton,
  NSpace,
  useMessage,
  NForm,
  NFormItem,
  NCascader
} from 'naive-ui';
import { defineComponent, onMounted, reactive, ref } from 'vue';
import styles from '../index.module.less';
import { getConfiguredSubjects, updateInstrument } from '../api';
export default defineComponent({
  props: {
    activeRow: {
      type: Object,
      default: () => ({ id: '' })
    },
    gradeNumList: {
      type: Array,
      default: () => []
    },
    classArray: {
      type: Array,
      default: () => []
    }
  },
  name: 'resetStudent',
  emits: ['close', 'getList'],
  setup(props, { emit }) {
    const data = reactive({
      uploading: false
    });
    const message = useMessage();
    const foemsRef = ref();
    const subjectList = ref([] as any);
    const createClassForm = reactive({
      currentGradeNum: null,
      gradeYear: null,
      currentClass: null,
      instrumentId: null,
      id: null
    });
    onMounted(() => {
      createClassForm.currentGradeNum = props.activeRow.currentGradeNum;
      createClassForm.gradeYear = props.activeRow.gradeYear;
      createClassForm.currentClass = props.activeRow.currentClass;
      createClassForm.instrumentId = props.activeRow.instrumentId;
      createClassForm.id = props.activeRow.id;

      getConfigSubject();
    });
    const submitForms = () => {
      foemsRef.value.validate(async (error: any) => {
        if (error) {
          return;
        }
        data.uploading = true;
        try {
          await updateInstrument({ ...createClassForm });
          message.success('修改成功');
          emit('close');
          emit('getList');
          data.uploading = false;
        } catch (e) {
          console.log(e);
        }
        data.uploading = false;
      });
    };

    const getConfigSubject = async () => {
      try {
        const { data } = await getConfiguredSubjects({
          gradeYear: createClassForm.gradeYear,
          currentGradeNum: createClassForm.currentGradeNum,
          currentClass: createClassForm.currentClass
        });
        const temp = data || [];
        subjectList.value = temp;
      } catch {
        //
      }
    };

    return () => (
      <div class={[styles.addClass]}>
        <NForm label-placement="left" model={createClassForm} ref={foemsRef}>
          <NFormItem
            path="instrumentId"
            rule={[
              {
                required: true,
                message: '请选择乐器'
              }
            ]}>
            <NCascader
              placeholder="请选择乐器"
              v-model:value={createClassForm.instrumentId}
              options={subjectList.value}
              checkStrategy="child"
              showPath={false}
              childrenField="instruments"
              expandTrigger="hover"
              labelField="name"
              valueField="id"
              clearable
              filterable
              style={{ width: '400px' }}
            />
          </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>
    );
  }
});
