import SaveForm from '@/components/save-form'
import Pagination from '@/components/pagination'
import {
  NButton,
  NDataTable,
  NFormItem,
  NInput,
  NModal,
  NSelect,
  NSpace,
  NTag,
  useDialog,
  useMessage
} from 'naive-ui'
import { defineComponent, onMounted, reactive, ref } from 'vue'
import { subjectBasicConfigPage, subjectBasicConfigUpdateConfigStatus } from '../api'
import SongOperation from './song-operation'
import numeral from 'numeral'

export default defineComponent({
  name: 'subsidy-list',
  setup() {
    const state = reactive({
      loading: false,
      pagination: {
        page: 1,
        rows: 10,
        pageTotal: 0
      },
      searchForm: {
        keyword: null,
        status: null
      },
      dataList: [] as any,
      visiableSong: false,
      songOperation: 'add',
      songData: {} as any
    })
    const dialog = useDialog()
    const message = useMessage()

    const columns = () => {
      return [
        {
          title: '编号',
          key: 'id'
        },
        {
          title: '声部名称',
          key: 'subjectName'
        },
        {
          title: '招生人数限制',
          key: 'studentEnrollmentUpperLimit',
          render(row: any) {
            return row.studentEnrollmentUpperLimit ? row.studentEnrollmentUpperLimit : '无限制'
          }
        },
        {
          title: '开团乐器',
          key: 'instrumentsName'
        },
        {
          title: '乐器团购价',
          key: 'instrumentsGroupPrice',
          render(row: any) {
            return numeral(row.instrumentsGroupPrice).format('0,0.00') + '元'
          }
        },
        {
          title: '乐器原价',
          key: 'instrumentsOriginalPrice',
          render(row: any) {
            return numeral(row.instrumentsOriginalPrice).format('0,0.00') + '元'
          }
        },
        {
          title: '教材',
          key: 'textbookName'
        },
        {
          title: '教材团购价',
          key: 'textbookGroupPrice',
          render(row: any) {
            return numeral(row.textbookGroupPrice).format('0,0.00') + '元'
          }
        },
        {
          title: '教材原价',
          key: 'textbookOriginalPrice',
          render(row: any) {
            return numeral(row.textbookOriginalPrice).format('0,0.00') + '元'
          }
        },
        {
          title: '更新人',
          key: 'operatorName'
        },
        {
          title: '更新时间',
          key: 'updateTime'
        },
        {
          title: '状态',
          key: 'enableFlag',
          render(row: any) {
            return row.enableFlag ? (
              <NTag type="primary">启用</NTag>
            ) : (
              <NTag type="error">停用</NTag>
            )
          }
        },
        {
          title: '操作',
          key: 'operation',
          render(row: any) {
            return (
              <NSpace>
                <NButton
                  type="primary"
                  size="small"
                  text
                  //v-auth="subjectBasicConfig/update1598501736003452929"
                  onClick={() => {
                    state.visiableSong = true
                    state.songOperation = 'edit'
                    state.songData = row
                  }}
                >
                  修改
                </NButton>

                <NButton
                  type="primary"
                  size="small"
                  text
                  //v-auth="subjectBasicConfig/updateConfigStatus1668958388100288514"
                  onClick={() => onOperation(row)}
                >
                  {!row.enableFlag ? '启用' : '停用'}
                </NButton>
              </NSpace>
            )
          }
        }
      ]
    }

    const onOperation = async (row: any) => {
      const content = !row.enableFlag
        ? `您是否启用“${row.subjectName}”？`
        : `您是否停用“${row.subjectName}”？`
      dialog.warning({
        title: '提示',
        content,
        positiveText: '确定',
        negativeText: '取消',
        onPositiveClick: async () => {
          try {
            await subjectBasicConfigUpdateConfigStatus({
              id: row.id,
              enableFlag: row.enableFlag ? false : true
            })
            message.success(`${!row.enableFlag ? '启用' : '停用'}成功`)
            getList()
          } catch {}
        }
      })
    }

    const getList = async () => {
      try {
        state.loading = true
        const { data } = await subjectBasicConfigPage({ ...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">
        <h2>声部参数</h2>
        {/* <SaveForm
          ref={saveForm}
          model={state.searchForm}
          onSubmit={onSubmit}
          onSetModel={(val: any) => (state.searchForm = val)}
        >
          <NFormItem label="关键字" path="search">
            <NInput
              v-model:value={state.searchForm.keyword}
              placeholder="员工编号/姓名/手机号"
              clearable
            />
          </NFormItem>
          <NFormItem label="状态" path="status">
            <NSelect
              v-model:value={state.searchForm.status}
              placeholder="请选择状态"
              options={employeeArray}
              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="subjectBasicConfig/save1598501676989595649"
              onClick={() => {
                state.visiableSong = true
                state.songOperation = 'add'
                state.songData = {}
              }}
            >
              添加声部参数
            </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
          ></Pagination>
        </div>
        <NModal
          v-model:show={state.visiableSong}
          preset="dialog"
          showIcon={false}
          title={state.songOperation === 'add' ? '新增声部参数' : '修改声部参数'}
          style={{ width: '800px' }}
        >
          <SongOperation
            type={state.songOperation}
            data={state.songData}
            onClose={() => (state.visiableSong = false)}
            onGetList={getList}
          />
        </NModal>
      </div>
    )
  }
})
