import {
  NButton,
  NSpace,
  useMessage,
  NForm,
  NFormItem,
  NCascader
} from 'naive-ui';
import { defineComponent, onMounted, reactive, ref } from 'vue';
import styles from '../index.module.less';
import CSelect from '/src/components/CSelect';
import { getConfiguredSubjects, updateInstrument } 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,
      gradeYear: null,
      instrumentId: null,
      id: null
    });

    const getConfigSubject = async () => {
      try {
        const { data } = await getConfiguredSubjects({
          gradeYear: createClassForm.gradeYear,
          currentGradeNum: createClassForm.currentGradeNum,
          currentClass: createClassForm.currentClass
        });
        const temp = data || [];
        subjectList.value = temp;
      } catch {
        //
      }
    };
    onMounted(async () => {
      // 获取教材分类列表
      // await catchStore.getSubjects();
      // subjectList.value = [
      //   { id: null, name: '选择声部' },
      //   ...catchStore.getSubjectList
      // console.log(props.activeRow, ' props.activeRow');
      // ];

      createClassForm.gradeYear = props.activeRow.gradeYear;
      createClassForm.currentGradeNum = props.activeRow.currentGradeNum;
      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('confirm', {
            lastUseCoursewareId: props.activeRow.lessonCoursewareId,
            unit: props.activeRow.lessonCoursewareKnowledgeDetailId,
            instrumentId: createClassForm.instrumentId,
            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="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>
    );
  }
});
