import Pagination from '@/components/pagination'
import { NButton, NDataTable, NModal, NSpace, NTag, useDialog, useMessage } from 'naive-ui'
import { defineComponent, onMounted, reactive } from 'vue'
import { leaveCategoryPage, leaveCategoryRemove } from '../../api'
import { filterClientType, filterMessageGroup } from '@/utils/filters'
import LeaveCategoryOperation from './leave-category-operation'
import TheTooltip from '@/components/TheTooltip'

export default defineComponent({
  name: 'subsidy-list',
  setup() {
    const state = reactive({
      loading: false,
      pagination: {
        page: 1,
        rows: 10,
        pageTotal: 0
      },
      dataList: [] as any,
      visiableLeave: false,
      leaveOperation: 'add',
      leaveData: {} as any
    })
    const dialog = useDialog()
    const message = useMessage()

    const columns = () => {
      return [
        {
          title: '编号',
          key: 'id'
        },
        {
          title: '客户端类型',
          key: 'clientType',
          render(row: any) {
            return filterClientType(row.clientType)
          }
        },
        {
          title: '类型名称',
          key: 'name',
          render(row: any) {
            return <TheTooltip content={row.name} />
          }
        },
        {
          title: '备注',
          key: 'remark',
          render(row: any) {
            return <TheTooltip content={row.remark} />
          }
        },
        {
          title: '操作',
          key: 'operation',
          render(row: any) {
            return (
              <NSpace>
                <NButton
                  type="primary"
                  size="small"
                  text
                  //v-auth="leaveCategory/update1610189620435570689"
                  onClick={() => {
                    state.visiableLeave = true
                    state.leaveOperation = 'edit'
                    state.leaveData = row
                  }}
                >
                  修改
                </NButton>
                <NButton
                  type="primary"
                  size="small"
                  text
                  onClick={() => onRemove(row)}
                  //v-auth="leaveCategory/remove1610189694385344513"
                >
                  删除
                </NButton>
              </NSpace>
            )
          }
        }
      ]
    }

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

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

    onMounted(() => {
      getList()
    })
    return () => (
      <>
        <NSpace style="padding-bottom: 12px">
          <NButton
            type="primary"
            //v-auth="leaveCategory/save1610189531537297410"
            onClick={() => {
              state.visiableLeave = true
              state.leaveOperation = 'add'
              state.leaveData = []
            }}
          >
            新增
          </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>

        <NModal
          v-model:show={state.visiableLeave}
          preset="dialog"
          showIcon={false}
          title={state.leaveOperation === 'add' ? '新增请假类型' : '修改请假类型'}
          style={{ width: '600px' }}
        >
          <LeaveCategoryOperation
            type={state.leaveOperation}
            data={state.leaveData}
            onClose={() => (state.visiableLeave = false)}
            onGetList={getList}
          />
        </NModal>
      </>
    )
  }
})
