<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { useI18n } from '@/hooks/web/useI18n'
import { ElLink, ElMessage, ElPopconfirm } from 'element-plus'
import { Table } from '@/components/Table'
import { useTable } from '@/hooks/web/useTable'
import { TableData } from '@/api/table/types'
import { reactive, ref, unref } from 'vue'
import { useRouter } from 'vue-router'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { delTableListApi, getTableListApi } from '@/api/multipleQuery/serviceLog'

defineOptions({
  name: 'ServiceLog'
})

const { push } = useRouter()

const ids = ref<string[]>([])

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState
    const res = await getTableListApi({
      pageNum: unref(currentPage),
      pageSize: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: res.data.list,
      total: res.data.total
    }
  },
  fetchDelApi: async () => {
    const res = await delTableListApi(unref(ids))
    return !!res
  }
})
const { loading, currentPage, pageSize, dataList, total } = tableState
const { getList, getElTableExpose, delList, refresh } = tableMethods
const searchParams = ref({})
const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}
const { t } = useI18n()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'selection',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      type: 'selection'
    }
  },
  {
    field: 'index',
    label: t('tableDemo.index'),
    type: 'index',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    }
  },
  {
    field: 'serviceName',
    label: t('tableDemo.servicename'),
    search: {
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '可信区表字段信息查询',
            value: '1'
          },
          {
            label: '台账查询接口',
            value: '2'
          }
        ],
        on: {
          change: (value: string) => {
            console.log(value)
          }
        }
      }
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    }
  },
  {
    field: 'serviceType',
    label: t('tableDemo.servicetype'),
    search: {
      hidden: true
    },
    form: {
      component: 'Input',
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24
    }
  },
  {
    field: 'serviceVersion',
    label: t('tableDemo.serviceversionnumber'),
    search: {
      hidden: true
    },
    form: {
      component: 'Input',
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24
    }
  },
  {
    field: 'requestType',
    label: t('tableDemo.requestmode'),
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    }
  },
  {
    field: 'requestDate',
    label: t('tableDemo.requestdate'),
    search: {
      component: 'DatePicker'
    },
    form: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    }
  },
  {
    field: 'requestTime',
    label: t('tableDemo.requesttime'),
    search: {
      hidden: true
    },
    form: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    }
  },
  {
    field: 'requestMsg',
    label: t('tableDemo.requestmessage'),
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          return (
            <>
              <ElLink type="primary" underline={false} onClick={() => action(data.row, 'detail')}>
                {t('exampleDemo.detail')}
              </ElLink>
            </>
          )
        }
      }
    }
  },
  {
    field: 'responseDate',
    label: t('tableDemo.responsedate'),
    search: {
      component: 'DatePicker'
    },
    form: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    }
  },
  {
    field: 'responseTime',
    label: t('tableDemo.responsetime'),
    search: {
      hidden: true
    },
    form: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    }
  },
  {
    field: 'responseState',
    label: t('tableDemo.responsestatus'),
    search: {
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '成功',
            value: '1'
          },
          {
            label: '失败',
            value: '2'
          }
        ],
        on: {
          change: (value: string) => {
            console.log(value)
          }
        }
      }
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    }
  },
  {
    field: 'responseMsg',
    label: t('tableDemo.responsemessage'),
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    }
  },
  {
    field: 'responseMessage',
    label: t('tableDemo.responseMessage'),
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          return (
            <>
              <ElLink type="primary" underline={false} onClick={() => action(data.row, 'detail')}>
                {t('tableDemo.download')}
              </ElLink>
              <ElPopconfirm
                title={t('common.delTableMsg')}
                width={200}
                v-slots={{
                  reference: () => {
                    return (
                      <>
                        <ElLink type="primary" underline={false}>
                          {t('exampleDemo.del')}
                        </ElLink>
                      </>
                    )
                  }
                }}
                onConfirm={() => delData(data.row)}
              ></ElPopconfirm>
            </>
          )
        }
      }
    }
  },
  // {
  //   field: 'importance',
  //   label: t('tableDemo.importance'),
  //   search: {
  //     hidden: true
  //   },
  //   form: {
  //     component: 'Select',
  //     componentProps: {
  //       style: {
  //         width: '100%'
  //       },
  //       options: [
  //         {
  //           label: '重要',
  //           value: 3
  //         },
  //         {
  //           label: '良好',
  //           value: 2
  //         },
  //         {
  //           label: '一般',
  //           value: 1
  //         }
  //       ]
  //     }
  //   },
  //   detail: {
  //     slots: {
  //       default: (data: any) => {
  //         return (
  //           <ElTag
  //             type={
  //               data.importance === 1 ? 'success' : data.importance === 2 ? 'warning' : 'danger'
  //             }
  //           >
  //             {data.importance === 1
  //               ? t('tableDemo.important')
  //               : data.importance === 2
  //               ? t('tableDemo.good')
  //               : t('tableDemo.commonly')}
  //           </ElTag>
  //         )
  //       }
  //     }
  //   }
  // },
  // {
  //   field: 'pageviews',
  //   label: t('tableDemo.pageviews'),
  //   search: {
  //     hidden: true
  //   },
  //   form: {
  //     component: 'InputNumber',
  //     value: 0
  //   }
  // },
  // {
  //   field: 'content',
  //   label: t('exampleDemo.content'),
  //   search: {
  //     hidden: true
  //   },
  //   table: {
  //     show: false
  //   },
  //   form: {
  //     component: 'Editor',
  //     colProps: {
  //       span: 24
  //     }
  //   },
  //   detail: {
  //     span: 24,
  //     slots: {
  //       default: (data: any) => {
  //         return <div innerHTML={data.content}></div>
  //       }
  //     }
  //   }
  // },
  {
    field: 'action',
    width: '260px',
    label: t('tableDemo.action'),
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      hidden: true
    }
  }
])

// @ts-ignore
const { allSchemas } = useCrudSchemas(crudSchemas)

const delLoading = ref(false)

const delData = async (row: TableData) => {
  delLoading.value = true
  const res = await delTableListApi([row.id])
    .catch(() => {})
    .finally(() => {
      delLoading.value = false
    })
  delLoading.value = false
  if (res) {
    const { code } = res.data
    if (code === 1) {
      ElMessage.success('操作成功!')
      getList()
    } else {
      ElMessage.error('操作失败!')
    }
  }
}

const action = (row: TableData, type: string) => {
  push(`/example/example-${type}?id=${row.id}`)
}

const disabled = ref(true)

const onSelectionChange = (selection: any[]) => {
  disabled.value = selection.length === 0
}
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />

    <Table
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
      @refresh="refresh"
      @selection-change="onSelectionChange"
    >
      <template #buttons>
        <!-- <ElButton type="primary" @click="AddAction">{{ t('exampleDemo.add') }}</ElButton>
      <ElButton :loading="delLoading" type="primary" :disabled="disabled" @click="delData(null)">
        {{ t('exampleDemo.del') }}
      </ElButton> -->
      </template>
    </Table>
  </ContentWrap>
</template>
