<template>
  <el-card class="hl-card">
    <template #header>
      <span>发票接收</span>
    </template>
    <ContentWrap class="borderxx">
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="110px"
      >
        <el-form-item label="发票号码" prop="number">
          <el-input
            v-model="queryParams.number"
            placeholder="请输入发票号码"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="开票日期" prop="billingDate">
          <el-date-picker
            v-model="queryParams.billingDate"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="对应订单" prop="orderCode">
          <el-input
            v-model="queryParams.orderCode"
            placeholder="请输入对应订单"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="发票类型" prop="type">
          <el-select
            v-model="queryParams.type"
            placeholder="请选择发票类型"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.HELI_INVOICE_TYPE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item style="margin-left:20px">
          <el-button @click="handleQuery" type="primary"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
          <el-button
            type="primary"
            plain
            @click="openDetail('create')"
            v-hasPermi="['heli:invoice:create']"
          >
            <Icon icon="ep:plus" class="mr-5px" /> 新增
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" class="hl-table">
        <el-table-column fixed type="index" width="100" label="序号" align="center" />
        <el-table-column fixed label="发票号码" align="center" prop="number" width="160">
           <template #default="scope">
              <el-button type="primary" plain link @click="openDetail('detail', scope.row.id)">{{ scope.row.number }}</el-button>
            </template>
        </el-table-column>
        <el-table-column
          fixed
          label="开票日期"
          align="center"
          prop="billingDate"
          :formatter="dateFormatter2"
          width="140"
        />
        <el-table-column fixed label="发票金额" align="center" prop="amount" width="160" />
        <el-table-column label="对应订单" align="center" prop="orderCode" width="220"/>
        <el-table-column label="发票类型" align="center" prop="type" width="160">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_INVOICE_TYPE" :value="scope.row.type" />
          </template>
        </el-table-column>
        <el-table-column label="税率（%）" align="center" prop="rate" width="140" />
        <el-table-column label="备注" align="center" prop="remark" width="180"/>
        <el-table-column label="创建人" align="center" prop="creatorName" width="140" />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          width="220"
        />
        <el-table-column label="提交人" align="center" prop="submitterName" width="140" />
        <el-table-column
          label="提交时间"
          align="center"
          prop="submitTime"
          :formatter="dateFormatter"
          width="220"
        />
        <el-table-column fixed="right" label="发票状态" align="center" prop="status" width="140">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_INVOICE_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="180">
          <template #default="scope">
            <el-button v-if="scope.row.status == 1" link type="primary" @click="openDetail('update', scope.row.id)">
              编辑
            </el-button>
            <el-button link type="primary" @click="openDetail('detail', 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>
  </el-card>

</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import {dateFormatter, dateFormatter2} from '@/utils/formatTime'
import download from '@/utils/download'
import * as InvoiceApi from '@/api/heli/invoice'
import {useCommonStore} from "@/store/modules/common";

defineOptions({ name: 'InvoiceReceive' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const commonStore = useCommonStore()
const router = useRouter()
const businessType = 'FINANCE_RECEIVE_INVOICE'

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  businessType: businessType,
  orderCode: undefined,
  number: undefined,
  type: undefined,
  billingDate: [],
  amount: undefined,
  rate: undefined,
  remark: undefined,
  submitter: undefined,
  submitTime: [],
  canceller: undefined,
  cancelTime: [],
  status: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
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 formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await InvoiceApi.deleteInvoice(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await InvoiceApi.exportInvoice(queryParams)
    download.excel(data, '财务发票.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const openDetail = (type: string, id?: number) => {
  commonStore.setStore('active', type)
  commonStore.setStore('id', id)
  commonStore.setStore('businessType', businessType)
  router.push({
    name: 'InvoiceDetail',
    query: {
      operateId: Math.random()
    }
  })
}

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