<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.createTime"
          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="platformCollectionAmount">
        <template #default="scope">
          ￥{{ fenToYuan(scope.row.platformCollectionAmount) }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="应结现金" align="center" prop="shopAmount">
        <template #default="scope">
          ￥{{ fenToYuan(scope.row.shopAmount) }}
        </template>
      </el-table-column> -->
      <el-table-column label="抵扣佣金" align="center" prop="shopHighAmount">
        <template #default="scope">
          {{ pointsToYuan(scope.row.shopAmount) }}
        </template>
      </el-table-column>
      <el-table-column label="抵扣消费分" align="center" prop="shopHighAmount">
        <template #default="scope">
          {{ pointsToYuan(scope.row.consumptionPoints) }}
        </template>
      </el-table-column>
      <el-table-column label="应结(成本)" align="center" prop="shopTotalHighAmount">
        <template #default="scope"> ￥{{ fenToYuan(scope.row.shopTotalHighAmount) }} </template>
      </el-table-column>
      <el-table-column
        label="是否结账"
        align="center"
        prop="status"
        :formatter="formatStatus"
        width="180px"
      />
      <el-table-column
        label="账单日期"
        align="center"
        prop="createTime"
        :formatter="dateFormatter2"
        width="180px"
      />

      <el-table-column label="操作" align="center" width="350px">
        <template #default="scope">
          <!-- 全部完成后不给显示导入，没有导出过网商付款单也不给显示导入 -->
          <el-button
            type="primary"
            size="small"
            plain
            @click="handleImport($event, scope.row)"
            v-if="scope.row.status !== 1 && scope.row.exportStatus"
            :loading="exportLoading"
            v-hasPermi="['trade:shop-settlement:export']"
          >
            导入凭证
          </el-button>
          <el-button
            type="success"
            size="small"
            plain
            @click="handleExport($event, scope.row)"
            :loading="exportLoading"
            v-hasPermi="['trade:shop-settlement:export']"
          >
            导出
          </el-button>
          <el-button
            size="small"
            plain
            type="success"
            @click="exportPaymentList($event, scope.row)"
            v-hasPermi="['trade:shop-settlement:export']"
          >
            导出付款单(网商银行)
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

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

<script setup lang="ts">
import { dateFormatter2, formatDate } from '@/utils/formatTime'
import download from '@/utils/download'
import { PtSettlementApi, PtSettlementVO } from '@/api/system/distri/ptsettlement'
import PtSettlementForm from './PtSettlementForm.vue'
import ImportPayMent from './ImportPayMent.vue'
import { fenToYuan, pointsToYuan } from '@/utils'
import { Setting, Search, Operation } from '@element-plus/icons-vue'
const props = defineProps({
  merchantId: {
    type: Number,
    default: undefined
  }
})
/**
 * 
 * 格式化是否结账状态
 * @param row 行数据
 * @param column 字段
 * @param cellValue 字段值
 * 
 */
const formatStatus = (row, column, cellValue): string => {
  if (cellValue === null) {
    return '未完成'
  }
  switch (cellValue) {
    case 0:
      return '未完成'
    case 1:
      return '已完成'
    case 2:
      return '部分完成'
    case 3:
      return '失败'
    default:
      return ''
  }
}

const searchMoreShow = ref(false)
// 打开更多搜索条件
const showSearchMore = () => {
  searchMoreShow.value = !searchMoreShow.value
}
const hideSearchMore = () => {
  searchMoreShow.value = false
}
/** 平台每日结算信息表，记录店铺的结算信息 列表 */
defineOptions({ name: 'PtSettlement' })

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

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

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PtSettlementApi.getPtSettlementPage(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 = (event) => {
  formRef.value.open(event, props.merchantId)
}

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

// 导入
const importRef = ref()
const handleImport = async (event, row) => {
  event.stopPropagation()
  importRef.value.open(event, props.merchantId, row)
}
/** 导出按钮操作 */
const handleExport = async (event, row) => {
  event.stopPropagation()
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PtSettlementApi.exportPtSettlement({ ptSettlementId: row.id })
    const fileName = formatDate(row.createTime, 'YYYY-MM-DD') + '-平台结算单.xls'
    download.excel(data, fileName)
  } catch {
  } finally {
    exportLoading.value = false
  }
}
/** 导出付款单按钮操作 */
const exportPaymentList = async (event, row) => {
  event.stopPropagation()
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PtSettlementApi.exportPaymentList({ ptSettlementId: row.id })
    const fileName = formatDate(row.createTime, 'YYYY-MM-DD') + '-平台结算付款单.xls'
    download.excel(data, fileName)
  } catch {
  } finally {
    exportLoading.value = false;
    getList();
  }
}

// const handleExport = async (event,row) => {
//   event.stopPropagation();

//   try {
//     // 导出的二次确认
//     await message.exportConfirm()
//     // 发起导出
//     exportLoading.value = true
//     const data = await PtSettlementApi.exportOrder(queryParams)
//     download.excel(data, '平台每日结算信息表，记录店铺的结算信息.xls')
//   } catch {
//   } finally {
//     exportLoading.value = false
//   }
// }

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