import SaveForm from '@/components/save-form'
import Pagination from '@/components/pagination'
import {
  NButton,
  NDataTable,
  NFormItem,
  NInput,
  NModal,
  NSpace,
  NTag,
  useDialog,
  useMessage
} from 'naive-ui'
import { defineComponent, onMounted, reactive, ref } from 'vue'
import { musicTagPage, musicTagRemove, musicTagState } from '../../api'
import TagOperation from '../modal/tag-operation'

export default defineComponent({
  name: 'content-flash',
  setup() {
    const dialog = useDialog()
    const message = useMessage()
    const state = reactive({
      loading: false,
      pagination: {
        page: 1,
        rows: 10,
        pageTotal: 0
      },
      searchForm: {
        keyword: null
      },
      dataList: [] as any,
      visiableTag: false,
      tagOperation: 'add',
      tagData: {} as any
    })

    const columns = () => {
      return [
        {
          title: '编号',
          key: 'id'
        },
        {
          title: '名称',
          key: 'name'
        },
        // {
        //   title: '平台乐谱启用/总数',
        //   key: 'enablePlatformMusicSheetNum',
        //   render(row: any) {
        //     return `${row.enablePlatformMusicSheetNum}/${row.musicPlatformSheetNum}`
        //   }
        // },
        // {
        //   title: '老师乐谱启用/总数',
        //   key: 'enableTeacherMusicSheetNum',
        //   render(row: any) {
        //     return `${row.enableTeacherMusicSheetNum}/${row.musicTeacherSheetNum}`
        //   }
        // },
        {
          title: '更新人',
          key: 'updateUser'
        },
        {
          title: '更新时间',
          key: 'updateTime'
        },
        {
          title: '排序',
          key: 'sortNumber'
        },
        {
          title: '状态',
          key: 'state',
          render(row: any) {
            return (
              <NTag type={row.state ? 'primary' : 'default'}>{row.state ? '启用' : '停用'}</NTag>
            )
          }
        },
        {
          title: '操作',
          key: 'operation',
          render(row: any) {
            return (
              <NSpace>
                <NButton
                  type="primary"
                  size="small"
                  text
                  onClick={() => onChangeState(row)}
                  //v-auth="musicTag/state1602302067191672833"
                >
                  {row.state ? '停用' : '启用'}
                </NButton>

                <NButton
                  type="primary"
                  size="small"
                  text
                  //v-auth="musicTag/update1602301883418243073"
                  onClick={() => {
                    state.visiableTag = true
                    state.tagOperation = 'edit'
                    state.tagData = row
                  }}
                >
                  修改
                </NButton>
                <NButton
                  type="primary"
                  size="small"
                  text
                  onClick={() => onRmove(row)}
                  //v-auth="musicTag/remove1602301979954343937"
                >
                  删除
                </NButton>
              </NSpace>
            )
          }
        }
      ]
    }

    const onChangeState = (row: any) => {
      // musicTag/state
      const statusStr = row.state ? '停用' : '启用'
      dialog.warning({
        title: '警告',
        content: `是否${statusStr}？`,
        positiveText: '确定',
        negativeText: '取消',
        onPositiveClick: async () => {
          try {
            await musicTagState({
              id: row.id
            })
            getList()
            message.success(`${statusStr}成功`)
          } catch {}
        }
      })
    }

    const onRmove = (row: any): void => {
      dialog.warning({
        title: '警告',
        content: `删除"${row.name}"，是否继续？`,
        positiveText: '确定',
        negativeText: '取消',
        onPositiveClick: async () => {
          try {
            await musicTagRemove({ id: row.id })
            getList()
            message.success('删除成功')
          } catch {}
        }
      })
    }

    const getList = async () => {
      try {
        state.loading = true
        const { data } = await musicTagPage({ ...state.pagination, ...state.searchForm })
        state.loading = false
        state.pagination.pageTotal = Number(data.total)
        state.dataList = data.rows || []
      } catch {
        state.loading = false
      }
    }

    const saveForm = ref()
    const onSubmit = () => {
      state.pagination.page = 1
      getList()
    }

    const onSearch = () => {
      saveForm.value?.submit()
    }
    const onBtnReset = () => {
      saveForm.value?.reset()
    }

    onMounted(() => {
      getList()
    })
    return () => (
      <div class="system-menu-container">
        <SaveForm
          ref={saveForm}
          model={state.searchForm}
          onSubmit={onSubmit}
          saveKey="tag-list"
          onSetModel={(val: any) => (state.searchForm = val)}
        >
          <NFormItem label="名称" path="keyword">
            <NInput placeholder="请输入名称" v-model:value={state.searchForm.keyword} clearable />
          </NFormItem>
          <NFormItem>
            <NSpace>
              <NButton type="primary" onClick={onSearch}>
                搜索
              </NButton>
              <NButton type="default" onClick={onBtnReset}>
                重置
              </NButton>
            </NSpace>
          </NFormItem>
        </SaveForm>
        <div class={['section-container']}>
          <NSpace style={{ paddingBottom: '12px' }}>
            <NButton
              type="primary"
              //v-auth="musicTag/save1602301806310158338"
              onClick={() => {
                state.visiableTag = true
                state.tagOperation = 'add'
                state.tagData = {}
              }}
            >
              添加曲目标签
            </NButton>
          </NSpace>

          <NDataTable
            loading={state.loading}
            columns={columns()}
            data={state.dataList}
          ></NDataTable>
          <Pagination
            v-model:page={state.pagination.page}
            v-model:pageSize={state.pagination.rows}
            v-model:pageTotal={state.pagination.pageTotal}
            onList={getList}
            sync
            saveKey="tag-list"
          ></Pagination>
        </div>
        <NModal
          v-model:show={state.visiableTag}
          preset="dialog"
          showIcon={false}
          title={state.tagOperation === 'add' ? '新增曲目标签' : '修改曲目标签'}
          style={{ width: '500px' }}
        >
          <TagOperation
            type={state.tagOperation}
            data={state.tagData}
            onClose={() => (state.visiableTag = false)}
            onGetList={getList}
          />
        </NModal>
      </div>
    )
  }
})
