import SaveForm from '@/components/save-form'
import Pagination from '@/components/pagination'
import { NButton, NDataTable, NFormItem, NInput, NModal, NSpace } from 'naive-ui'
import { defineComponent, reactive, onMounted } from 'vue'
import SubsidyOperation from '../modal/subsidy-operation'
import { orchestraSubsidyStandardPage } from '../api'

export default defineComponent({
  name: 'subsidy-list',
  setup() {
    const state = reactive({
      loading: false,
      pagination: {
        page: 1,
        rows: 10,
        pageTotal: 0
      },
      dataList: [] as any,
      visiableSubsidy: false,
      subsidyOperation: 'add',
      subsidyData: {} as any
    })

    const columns = () => {
      return [
        {
          title: '编号',
          key: 'id'
        },
        {
          title: '补助标准名称',
          key: 'subsidyStandardName'
        },
        {
          title: '更新人',
          key: 'operatorName'
        },
        {
          title: '更新时间',
          key: 'updateTime'
        },
        {
          title: '操作',
          key: 'operation',
          render(row: any) {
            return (
              <NSpace>
                <NButton
                  type="primary"
                  size="small"
                  text
                  //v-auth="orchestraSubsidyStandard/update1597887579789053953"
                  onClick={() => {
                    state.visiableSubsidy = true
                    state.subsidyOperation = 'edit'
                    state.subsidyData = row
                  }}
                >
                  修改
                </NButton>
              </NSpace>
            )
          }
        }
      ]
    }

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

    onMounted(() => {
      getList()
    })
    return () => (
      <>
        <NSpace style={{ paddingBottom: '12px' }}>
          <NButton
            type="primary"
            //v-auth="orchestraSubsidyStandard/save1597887486016999426"
            onClick={() => {
              state.visiableSubsidy = true
              state.subsidyOperation = 'add'
              state.subsidyData = {}
            }}
          >
            添加补助标准
          </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="subsidy-list"
        ></Pagination>

        <NModal
          v-model:show={state.visiableSubsidy}
          preset="dialog"
          showIcon={false}
          title={state.subsidyOperation === 'add' ? '新增补助标准' : '修改补助标准'}
          style={{ width: '800px' }}
        >
          <SubsidyOperation
            type={state.subsidyOperation}
            data={state.subsidyData}
            onClose={() => (state.visiableSubsidy = false)}
            onGetList={getList}
          />
        </NModal>
      </>
    )
  }
})
