<template>
  <!-- 搜索工作栏 -->
  <div class="search-one">
    <div style="text-align: right" class="search-input">
      <el-button :icon="Operation" @click="showSearchMore" style="margin-left: 0" />
    </div>
  </div>
  <div class="searchMore" v-show="searchMoreShow" style="width: calc(100% - 60px)">
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="120px"
    >
      <el-form-item label="账单日期" prop="createTime">
        <el-date-picker
          v-model="queryParams.orderCalcTime"
          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-row>
        <el-col>
          <el-form-item>
            <el-button @click="hideSearchMore"> 收起 </el-button>
            <el-button @click="resetQuery">
              <Icon class="mr-5px" icon="ep:refresh" />
              重置
            </el-button>
            <el-button @click="handleQuery" plain type="primary">
              <Icon class="mr-5px" icon="ep:search" />
              搜索
            </el-button>
            <!-- <el-button type="success" plain @click="handleExport" v-hasPermi="['distri:pt-settlement:export']" :loading="exportLoading">
              
              <Icon icon="ep:download" class="mr-5px" /> 导出
            </el-button> -->
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>

  <!-- 列表 -->
  <ContentWrap class="mt-15px">
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" @row-click="openForm($event)" row-class-name="row">
      <el-table-column label="账单编号" align="center" prop="id" />
      
      <el-table-column label="金额" align="center" prop="price">
        <template #default="scope">
          ￥{{ fenToYuan(scope.row.price) }}
        </template>
      </el-table-column>
      <el-table-column label="确收金额" align="center" prop="receivedPrice">
        <template #default="scope">
          ￥{{ fenToYuan(scope.row.receivedPrice) }}
        </template>
      </el-table-column>
      <el-table-column label="退款金额" align="center" prop="refundPrice">
        <template #default="scope">
          ￥{{ fenToYuan(scope.row.refundPrice) }}
        </template>
      </el-table-column>

      <el-table-column label="佣金" align="center" prop="integral">
        <template #default="scope">
          {{ pointsToPoint(scope.row.integral) }}
        </template>
      </el-table-column>
      <el-table-column label="确收佣金" align="center" prop="receivedIntegral">
        <template #default="scope">
          {{ pointsToPoint(scope.row.receivedIntegral) }}
        </template>
      </el-table-column>
      <el-table-column label="退款佣金" align="center" prop="refundIntegral">
        <template #default="scope">
          {{ pointsToPoint(scope.row.refundIntegral) }}
        </template>
      </el-table-column>

      <el-table-column label="消费分" align="center" prop="consumptionPoints">
        <template #default="scope">
          {{ pointsToPoint(scope.row.consumptionPoints) }}
        </template>
      </el-table-column>
      <el-table-column label="确收消费分" align="center" prop="receivedConsumptionPoints">
        <template #default="scope">
          {{ pointsToPoint(scope.row.receivedConsumptionPoints) }}
        </template>
      </el-table-column>
      <el-table-column label="退款消费分" align="center" prop="refundConsumptionPoints">
        <template #default="scope">
          {{ pointsToPoint(scope.row.refundConsumptionPoints) }}
        </template>
      </el-table-column>

      <el-table-column label="订单数" align="center" prop="orderCount" />
      <!-- <el-table-column label="订单所属日期" align="center" prop="orderCalcTime" /> -->
      <el-table-column
        label="账单日期"
        align="center"
        prop="orderCalcTime"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          
          <el-button
                type="success"
                plain
                @click="handleExport($event,scope.row)"
                :loading="exportLoading"
                v-hasPermi="['distri:daily-bill:export']"
                size="small"  
              >
              导出
            </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <PtDailyBillForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { PtDailyBillApi, PtDailyBillVO } from '@/api/system/distri/ptdailybill'
import { DailyBillApi, DailyBillVO } from '@/api/system/distri/dailybill'
import PtDailyBillForm from './PtDailyBillForm.vue'
import { fenToYuan, pointsToPoint } from '@/utils'
import { Setting, Search, Operation } from '@element-plus/icons-vue'
const props = defineProps({
	merchantId: {
		type: Number,
		default: undefined
	}
})
const searchMoreShow = ref(false)
// 打开更多搜索条件
const showSearchMore = () => {
  searchMoreShow.value = !searchMoreShow.value
}
const hideSearchMore = () => {
  searchMoreShow.value = false
}
/** 平台每日账单统计 列表 */
defineOptions({ name: 'PtDailyBill' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<PtDailyBillVO[]>([]) // 列表的数据
// 列表的总页数
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  
  orderCalcTime: undefined,
 
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

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

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

}

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

/** 添加/修改操作 */
const formRef = ref()
const openForm = (event) => {
  formRef.value.open(event)
}

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

/** 导出按钮操作 */
const handleExport = async (event, row) => {
  event.stopPropagation();
  // console.log(row.orderCalcTime)
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DailyBillApi.exportDailyBill({ptDailyBillId:row.id})
    download.excel(data, '每日账单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style src="@/styles/SearchIndex.css" lang="scss" scoped></style>
<style>
.row {
  cursor: pointer;
}
.dailybill-dialog > .el-dialog__body {
  padding: 10px;
  min-height:500px;
}
</style>
<style scoped>
::v-deep .el-card__body {
  padding: 10px;
}
</style>
