import SaveForm from '@/components/save-form'
import Pagination from '@/components/pagination'
import {
  NButton,
  NDataTable,
  NFormItem,
  NInput,
  NModal,
  NSpace,
  useDialog,
  useMessage
} from 'naive-ui'
import { defineComponent, onMounted, reactive, ref } from 'vue'
import { sysPositionPage, sysPositionRemove } from '../api'
import { filterPosition } from '@/utils/filters'
import StationOperation from './station-operation'

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

    const columns = () => {
      return [
        {
          title: '编号',
          key: 'id'
        },
        {
          title: '岗位名称',
          key: 'roleName'
        },
        {
          title: '岗位类型',
          key: 'jobType',
          render(row: any) {
            return filterPosition(row.jobType)
          }
        },
        {
          title: '操作',
          key: 'operation',
          render(row: any) {
            return (
              <NSpace>
                <NButton
                  type="primary"
                  size="small"
                  text
                  //v-auth="sysPosition/update1597891012130623489"
                  onClick={() => {
                    state.visiableRole = true
                    state.roleOperation = 'edit'
                    state.roleData = row
                  }}
                >
                  修改
                </NButton>
                <NButton
                  type="primary"
                  size="small"
                  text
                  onClick={() => onRmove(row)}
                  //v-auth="sysPosition/remove1597891111267192834"
                >
                  删除
                </NButton>
              </NSpace>
            )
          }
        }
      ]
    }

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

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

    onMounted(() => {
      getList()
    })
    return () => (
      <div class="system-menu-container">
        {/* <h2>岗位管理</h2> */}
        <div class={['section-container']}>
          <NSpace style={{ paddingBottom: '12px' }}>
            <NButton
              type="primary"
              //v-auth="sysPosition/save1597890897835839489"
              onClick={() => {
                state.visiableRole = true
                state.roleOperation = 'add'
                state.roleData = {}
              }}
            >
              添加岗位
            </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}
            saveKey="StationManage"
            sync
          ></Pagination>
        </div>
        <NModal
          v-model:show={state.visiableRole}
          preset="dialog"
          showIcon={false}
          title={state.roleOperation === 'add' ? '新增岗位' : '修改岗位'}
          style={{ width: '500px' }}
        >
          <StationOperation
            type={state.roleOperation}
            data={state.roleData}
            onClose={() => (state.visiableRole = false)}
            onGetList={getList}
          />
        </NModal>
      </div>
    )
  }
})
