import {defineComponent, onMounted, reactive, ref} from "vue";
import {NButton, NCascader, NForm, NFormItem, NInputNumber, NSelect, NSpace, useMessage} from "naive-ui";
import {musicSheetApplicationExtendCategoryApplicationExtendInfo, musicSheetApplicationExtendUpdate} from "@views/music-library/api";
import {getSelectDataFromObj} from "@/utils/objectUtil";
import {musicSheetPaymentType} from "@/utils/constant";

export default defineComponent({
  name: 'project-music-cooleshow-edu-updateMusic',
  props: {
    appId: {
      type: String,
      required: true
    },
    rowData: {
      type: Object,
      required: true
    },
    musicSheetCategories: {
      type: Array,
      default: () => []
    }
  },
  emits: ['close', 'getList'],
  setup(props, {slots, attrs, emit}) {
    const message = useMessage()
    const btnLoading = ref(false)
    const forms = reactive({
      musicSheetCategoryId: null as any,
      sortNo: null as any,
      paymentType: null as any,
    })
    const formsRef = ref()

    const state = reactive({
      rowData: null as any,
      musicSheetCategories: [] as any,
    })

    onMounted(async () => {
      state.rowData = props.rowData
      state.musicSheetCategories = props.musicSheetCategories
      const {data} = await musicSheetApplicationExtendCategoryApplicationExtendInfo({musicSheetId: state.rowData.id, applicationId: props.appId}) as any
      if (!data) {
        message.error("加载应用失败")
        return
      }
      forms.musicSheetCategoryId = data[0].musicSheetCategoryId
      forms.sortNo = data[0].sortNo
      forms.paymentType = data[0].paymentType

    })

    const onSubmit = async () => {
      formsRef.value.validate(async (error: any) => {
        if (error) return false
        btnLoading.value = true
        try {
          const res = await musicSheetApplicationExtendUpdate(
              {
                ...forms,
                musicSheetId: state.rowData.id,
                applicationId: props.appId
              }
          ) as any;
          if (res && res.code === 200) {
            emit('close')
            emit('getList')
          }
        } catch (error) {
        }
        btnLoading.value = false
      })
    }

    return () => {
      return (
          <div style="background: #fff; padding-top: 12px">
            <NForm
                ref={formsRef}
                labelPlacement="top"
                model={forms}
                label-placement="left"
                label-width="auto"
            >
              <NFormItem
                  label="曲目分类"
                  path="musicSheetCategoryId"
                  rule={[
                    {
                      required: true,
                      message: '请选择曲目分类'
                    }
                  ]}
              >
                <NCascader
                    valueField="id"
                    labelField="name"
                    children-field="children"
                    placeholder="请选择曲目分类"
                    value={forms.musicSheetCategoryId}
                    options={state.musicSheetCategories}
                    onUpdateValue={(value: any) => {
                      forms.musicSheetCategoryId = value
                    }}
                    clearable
                />
              </NFormItem>
              <NFormItem
                  label="收费方式"
                  path="paymentType"
                  rule={[
                    {
                      required: true,
                      message: '请选择收费方式'
                    }
                  ]}
              >
                <NSelect
                    placeholder="请选择收费方式"
                    options={getSelectDataFromObj(musicSheetPaymentType)}
                    v-model:value={forms.paymentType}
                    clearable
                />
              </NFormItem>
              <NFormItem
                  label="排序值"
                  path="sortNo"
                  rule={[
                    {
                      required: true,
                      message: '请输入排序值'
                    }
                  ]}
              >
                <NInputNumber
                    v-model:value={forms.sortNo}
                    placeholder="请输入排序值"
                    clearable
                    min={0}
                    max={9999}
                    style={{width: '100%'}}
                />
              </NFormItem>
            </NForm>

            <NSpace justify="end">
              <NButton onClick={() => emit('close')}>取消</NButton>
              <NButton type="primary" onClick={onSubmit}
                       loading={btnLoading.value}
                       disabled={btnLoading.value}
              >
                保存
              </NButton>
            </NSpace>
          </div>
      )
    }
  }
})