import SaveForm from '@/components/save-form'
import Pagination from '@/components/pagination'
import { NButton, NDataTable, NModal, NSpace, useDialog, useMessage } from 'naive-ui'
import { defineComponent, onMounted, reactive, ref } from 'vue'
import { sysPaymentConfigPage, sysPaymentConfigRemove } from '../../api'
import { filterClientType, filterOpenType } from '@/utils/filters'
import PaymentOperation from './payment-operation'

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

    const columns = () => {
      return [
        {
          title: '编号',
          key: 'id'
        },
        {
          title: '提供方',
          key: 'openType',
          render(row: any) {
            return filterOpenType(row.openType)
          }
        },
        {
          title: '客户端类型',
          key: 'clientType',
          render(row: any) {
            return filterClientType(row.clientType)
          }
        },
        {
          title: '参数名称',
          key: 'paramName'
        },
        {
          title: '参数值',
          key: 'paramValue'
        },
        {
          title: '描述',
          key: 'description'
        },
        {
          title: '操作',
          key: 'operation',
          render(row: any) {
            return (
              <NSpace>
                <NButton
                  type="primary"
                  size="small"
                  text
                  //v-auth="sysPaymentConfig/update1597903527669518338"
                  onClick={() => {
                    state.visiablePayment = true
                    state.paymentOperation = 'edit'
                    state.paymentData = row
                  }}
                >
                  修改
                </NButton>
                <NButton
                  type="primary"
                  size="small"
                  text
                  onClick={() => onRemove(row)}
                  //v-auth="sysPaymentConfig/remove1597903613933768705"
                >
                  删除
                </NButton>
              </NSpace>
            )
          }
        }
      ]
    }

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

    const getList = async () => {
      try {
        state.loading = true
        const { data } = await sysPaymentConfigPage({ ...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="sysPaymentConfig/save1597903459457552386"
            onClick={() => {
              state.visiablePayment = true
              state.paymentOperation = 'add'
              state.paymentData = []
            }}
          >
            新增
          </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="payemnt-setting"
        ></Pagination>

        <NModal
          v-model:show={state.visiablePayment}
          preset="dialog"
          showIcon={false}
          title={state.paymentOperation === 'add' ? '新增支付参数' : '修改支付参数'}
          style={{ width: '550px' }}
        >
          <PaymentOperation
            type={state.paymentOperation}
            data={state.paymentData}
            onClose={() => (state.visiablePayment = false)}
            onGetList={getList}
          />
        </NModal>
      </>
    )
  }
})
