<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton, ElTooltip, ElMessage, ElButtonGroup } from 'element-plus'
import { Table } from '@/components/Table'
import { getTableListApi, delTableListApi, saveTableApi, updateTableApi } from '@/api/manage/file'
import { useTable } from '@/hooks/web/useTable'
import { ref, unref } from 'vue'
import { useEmitt } from '@/hooks/event/useEmitt'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { TableSetting } from '@/components/TableSetting'
import { usePageStore } from '@/store/modules/page'
import { set } from 'lodash-es'
import { useStorage } from '@/hooks/web/useStorage'
import { Dialog } from '@/components/Dialog'
import { FileData } from '@/api/manage/types'
import Write from './components/Write.vue'
import { useIcon } from '@/hooks/web/useIcon'
import { Qrcode } from '@/components/Qrcode'
import { uploadFile } from '@/api/common'
defineOptions({
  name: 'FilePage'
})
const QRIcon = useIcon({ icon: 'ic:round-qr-code' })
const DeleteIcon = useIcon({ icon: 'ep:delete' })
const DownLoadIcon = useIcon({ icon: 'ep:download' })
const copyIcon = useIcon({ icon: 'ep:document-copy' })
const EditIcon = useIcon({ icon: 'ep:edit' })
const id = ref<string>('')
const QrVisible = ref<boolean>(false)
const QrSrc = ref<string>('')
const { getStorage } = useStorage()
const searchParams = ref({})
const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}
const currentRow = ref<FileData>({
  virtualPath: ''
})
const formState = ref<string>('add')
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: Number(res.data.totalCount)
    }
  },
  fetchDelApi: async () => {
    const res = await delTableListApi(unref(id))
    return !!res
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList, delList, setColumn } = tableMethods

getList()

useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      currentPage.value = 1
    }
    getList()
  }
})

const { t } = useI18n()
const appStore = usePageStore()
const dialogVisible = ref(false)
// const fileList = ref([])
const crudSchemas: CrudSchema[] = [
  {
    field: 'name',
    label: '名称',
    minWidth: 100,
    table: {
      hidden: false
    },
    form: {
      colProps: {
        span: 24
      }
    }
  },
  {
    field: 'version',
    label: '版本号',
    minWidth: 100,
    search: {
      hidden: true
    },
    table: {
      hidden: false
    }
  },
  {
    field: 'code',
    label: '版本序列号',
    minWidth: 100,
    table: {
      hidden: false
    },
    search: {
      hidden: true
    },
    form: {
      component: 'InputNumber'
    }
  },
  {
    field: 'fileName',
    label: '文件名',
    minWidth: 120,
    table: {
      hidden: false
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'virtualPath',
    label: '文件链接',
    minWidth: 120,
    search: {
      hidden: true
    },
    table: {
      hidden: false
    },
    form: {
      colProps: {
        span: 24
      },
      component: 'Upload',
      componentProps: {
        httpRequest: (data: any) => {
          delLoading.value = true
          let file = data.file
          let formData = new FormData()
          formData.append('file', file)
          uploadFile(file.size <= 1024 * 1000 ? formData : file).then((response) => {
            console.log(response)
            let resUrl = ''
            if (file.size <= 1024 * 1000) {
              resUrl = response.data.virtualPath
            } else {
              resUrl = response.url.replace(
                'xn-back.oss-cn-nanjing.aliyuncs.com',
                'oss.dacundianzi.com'
              )
            }
            currentRow.value = {
              ...currentRow.value,
              virtualPath: resUrl,
              fileName: file.name
            }
            const write = unref(writeRef)
            write?.setValues({
              bannerUrl: resUrl
            })
            delLoading.value = false
          })
          // uploadFile(formData).then((response) => {
          //   currentRow.value = {
          //     ...currentRow.value,
          //     ...response.data
          //   }
          //   delLoading.value = false
          // })
        },
        class: 'filePageUploader',
        fileList: currentRow.value.virtualPath
          ? [
              {
                url: currentRow.value.virtualPath
              }
            ]
          : [],
        headers: {
          token: getStorage('token')
        },
        onSuccess: (response) => {
          currentRow.value = {
            ...currentRow.value,
            ...response.data
          }
          const write = unref(writeRef)
          write?.setValues({
            ...response.data
          })
        },
        slots: {
          default: () => <ElButton type="primary">上传文件</ElButton>
        }
      }
    }
  },
  {
    field: 'filePath',
    label: '文件路径',
    minWidth: 120,
    search: {
      hidden: true
    },
    table: {
      hidden: false
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'content',
    label: '版本更新说明',
    minWidth: 120,
    search: {
      hidden: true
    },
    table: {
      hidden: false
    },
    form: {
      colProps: {
        span: 24
      },
      componentProps: {
        type: 'textarea'
      }
    }
  },
  {
    field: 'remark',
    label: '备注',
    minWidth: 120,
    search: {
      hidden: true
    },
    table: {
      hidden: false
    },
    form: {
      colProps: {
        span: 24
      },
      componentProps: {
        type: 'textarea'
      }
    }
  },
  {
    field: 'createTime',
    label: '创建时间',
    minWidth: 160,
    table: {
      hidden: false
    },
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'action',
    width: '250px',
    label: '操作',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      hidden: false,
      fixed: 'right',
      slots: {
        default: (data: any) => {
          return (
            <ElButtonGroup>
              <ElTooltip content="编辑">
                <ElButton text icon={EditIcon} onClick={() => handleEdit(data.row)} />
              </ElTooltip>
              <ElTooltip content="二维码">
                <ElButton text icon={QRIcon} onClick={() => showQrCode(data.row)} />
              </ElTooltip>
              <ElTooltip content="下载">
                <ElButton text icon={DownLoadIcon} onClick={() => downloadFile(data.row)} />
              </ElTooltip>
              <ElTooltip content="复制链接">
                <ElButton text icon={copyIcon} onClick={() => onCopy(data.row)} />
              </ElTooltip>
              <ElTooltip content="删除">
                <ElButton text icon={DeleteIcon} type="danger" onClick={() => delData(data.row)} />
              </ElTooltip>
            </ElButtonGroup>
          )
        }
      }
    }
  }
]

// @ts-ignore
const getSchemas = () => {
  let localSchemas = appStore.getPageData['FilePage']
  if (localSchemas && localSchemas.schemas) {
    let localSchemasArr = localSchemas.schemas
    for (let i = 0; i < localSchemasArr.length; i++) {
      let item = localSchemasArr[i]
      let index = crudSchemas.findIndex((e) => {
        return e.field == item.field
      })
      if (index > 0) {
        set(crudSchemas[index], 'table.hidden', item.table.hidden)
      }
    }
  }
}
getSchemas()
let allSchemas = useCrudSchemas(crudSchemas).allSchemas
// 修改列设置后调用
const setSchemas = (schemas: CrudSchema[]) => {
  let arr = schemas.map((item) => {
    return {
      field: item.field,
      path: 'hidden',
      value: item.table ? item.table.hidden : false
    }
  })
  setColumn(arr)
}
const writeRef = ref<ComponentRef<typeof Write>>()

const handleEdit = (row: FileData) => {
  currentRow.value = row
  formState.value = 'edit'
  dialogVisible.value = true
}

const save = async () => {
  const write = unref(writeRef)
  const formData = await write?.submit()
  if (formData) {
    delLoading.value = true
    try {
      if (formData.id) {
        let res = await updateTableApi(formData)
        if (res) {
          currentPage.value = 1
          getList()
        }
      } else {
        let res = await saveTableApi(formData)
        if (res) {
          currentPage.value = 1
          getList()
        }
      }
    } catch (error) {
    } finally {
      delLoading.value = false
      dialogVisible.value = false
    }
  }
}

const delLoading = ref(false)

const delData = async (row: FileData) => {
  if (!row.id) return
  id.value = row?.id
  delLoading.value = true
  await delList(unref(id).length).finally(() => {
    delLoading.value = false
  })
}

const showQrCode = (row: FileData) => {
  QrSrc.value = row.virtualPath
  QrVisible.value = true
}

const downloadFile = (row: FileData) => {
  window.open(row.virtualPath)
}

import useClipboard from 'vue-clipboard3'

const { toClipboard } = useClipboard()

const onCopy = async (row: FileData) => {
  try {
    await toClipboard(row.virtualPath)
    ElMessage({
      message: '复制成功！',
      type: 'success'
    })
  } catch (error) {
    console.log(error)
    ElMessage({
      message: '复制失败！',
      type: 'error'
    })
  }
}

const handleAdd = () => {
  formState.value = 'add'
  currentRow.value = {
    virtualPath: ''
  }
  dialogVisible.value = true
}
</script>

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

    <div class="mb-10px">
      <el-button type="primary" @click="handleAdd">上传文件</el-button>
      <TableSetting page="FilePage" :data="crudSchemas" @set-schemas="setSchemas" />
    </div>

    <Table
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
    />
  </ContentWrap>
  <Dialog v-model="dialogVisible" :title="formState == 'add' ? '新增文件' : '编辑文件'">
    <Write ref="writeRef" :form-schema="allSchemas.formSchema" :current-row="currentRow" />
    <template #footer>
      <ElButton type="primary" :loading="delLoading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
      <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
    </template>
  </Dialog>
  <Dialog v-model="QrVisible" width="430px" title="二维码">
    <Qrcode
      :width="395"
      :options="{
        color: {
          dark: '#55D187',
          light: '#ffffff'
        }
      }"
      :text="QrSrc"
    />
    <template #footer>
      <ElButton @click="QrVisible = false">{{ t('dialogDemo.close') }}</ElButton>
    </template>
  </Dialog>
</template>
@/hooks/event/useEmitt

<style lang="less">
.uploadBtn {
  display: inline-block;
  margin-right: 12px;
}
.filePageUploader {
  width: 100%;
}
</style>
