<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="140px"
    >
      <el-form-item label="用户编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付订单编号" prop="payOrderId">
        <el-input
          v-model="queryParams.payOrderId"
          placeholder="请输入支付订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="发票类型" prop="category">
        <el-select
          v-model="queryParams.category"
          placeholder="请选择发票类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="(val, i) in categoryType"
            :key="i"
            :label="val.label"
            :value="val.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发票抬头" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入发票抬头"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="税号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入税号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="联系邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入联系邮箱"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true">
      <!-- <el-table-column label="用户编号" align="center" prop="userId" /> -->
      <el-table-column label="支付订单编号" align="center" prop="payOrderId" />
      <el-table-column label="开票金额" align="center" prop="price">
        <template #default="scope">
          {{ scope.row.price ? (scope.row.price / 100).toFixed(2) + '元' : '' }}
        </template>
      </el-table-column>
      <el-table-column label="发票类型" align="center" prop="category">
        <template #default="scope">
          {{ categoryType.find(e => e.value === scope.row.category)?.label }}
        </template>
      </el-table-column>
      <el-table-column label="发票类型" align="center" prop="type">
        <template #default="scope">
          {{ typeList.find(e => e.value === scope.row.type)?.label }}
        </template>
      </el-table-column>
      <el-table-column label="发票抬头" align="center" prop="title" />
      <el-table-column label="税号" align="center" prop="code" />
      <el-table-column label="联系邮箱" align="center" prop="email" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="开户银行" align="center" prop="enterpriseBankTitle" />
      <el-table-column label="开户账号" align="center" prop="enterpriseBankNo" />
      <el-table-column label="已开发票地址" align="center" prop="fileUrl" :show-overflow-tooltip="true" />
      <el-table-column fixed="right" min-width="100" label="操作" align="center">
        <template #default="scope">
          <el-button v-hasPermi="['member:invoice-list:status']" v-if="scope.row.status !== 1" link type="primary" @click="handleUpload(scope.row.id)">
            上传发票
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <UploadInvoicePage ref="upload" @refresh="getList" />
</template>

<script setup lang="ts">
import { InvoiceApi } from '@/api/menduner/system/invoice'
import UploadInvoicePage from './uploadInvoice.vue'

/** 交易订单	 列表 */
defineOptions({ name: 'Invoice' })

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  category: undefined,
  phone: undefined,
  email: undefined,
  title: undefined,
  code: undefined,
  payOrderId: undefined,
  userId: undefined
})
const categoryType = ref([
  { label: '个人', value: 0 },
  { label: '企业', value: 1 }
])
const typeList = ref([
  { label: '增值税普通发票', value: 0 },
  { label: '增值税专用发票', value: 1 }
])
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await InvoiceApi.getInvoicePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const upload = ref()
const handleUpload = async (id: string) => {
  upload.value.open(id)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>