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 {scoreType} from "@/utils/constant";
import {formatTree} from "@views/music-library/musicUtil";

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,
      isConvertibleScore: null as any,//是否支持转简谱
      status: null as any, // 是否启用
      scoreType: null as any,//默认谱面
    })
    const formsRef = ref()

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

    onMounted(async () => {
      state.rowData = props.rowData
      formatTree(props.musicSheetCategories)
      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
      forms.isConvertibleScore = data[0].isConvertibleScore
      forms.scoreType = data[0].scoreType
      forms.status = data[0].status
    })

    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}
                    checkStrategy={"child"}
                    onUpdateValue={(value: any) => {
                      forms.musicSheetCategoryId = value
                    }}
                    clearable
                />
              </NFormItem>
              <NFormItem
                  label="收费方式"
                  path="paymentType"
                  rule={[
                    {
                      required: true,
                      message: '请选择收费方式'
                    }
                  ]}
              >
                <NSelect
                    placeholder="请选择收费方式"
                    options={[
                      {
                        label:'免费',
                        value:'FREE'
                      },
                      {
                        label:'会员',
                        value:'VIP'
                      }
                    ]}
                    v-model:value={forms.paymentType}
                    clearable
                />
              </NFormItem>
              <NFormItem
                  label="默认谱面"
                  path="scoreType"
                  rule={[
                    {
                      required: true,
                      message: '请选择默认谱面'
                    }
                  ]}
              >
                <NSelect
                    v-model:value={forms.scoreType}
                    placeholder="请选择默认谱面"
                    options={getSelectDataFromObj(scoreType)}
                    clearable
                />
              </NFormItem>
              <NFormItem
                  label="支持转谱"
                  path="isConvertibleScore"
                  rule={[
                    {
                      required: true,
                      message: '请选择是否支持转谱',
                      trigger: ['input', 'blur'],
                      type: 'boolean'
                    }
                  ]}
              >
                <NSelect
                    v-model:value={forms.isConvertibleScore}
                    options={
                      [
                        {
                          label: '是',
                          value: true
                        },
                        {
                          label: '否',
                          value: false
                        }
                      ] as any
                    }
                    placeholder="请选择是否支持转谱"
                    onUpdateValue={async (value: any) => {
                      if (!value) {
                        //如果不支持，修改默认谱面
                        // forms.scoreType = 'STAVE'
                      }
                    }}
                    clearable
                ></NSelect>
              </NFormItem>
              <NFormItem
                  label="是否启用"
                  path="status"
                  rule={[
                    {
                      required: true,
                      message: '请选择是否启用'
                    }
                  ]}
              >
                <NSelect
                    placeholder="请选择是否启用"
                    options={[
                      {
                        label: '是',
                        value: true
                      },
                      {
                        label: '否',
                        value: false
                      }
                    ] as any}
                    v-model:value={forms.status}
                    clearable
                />
              </NFormItem>
              <NFormItem
                  label="排序值"
                  path="sortNo"
              >
                <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>
      )
    }
  }
})