<template>
  <!-- 搜索 -->
  <div class="search">
    <el-tabs v-model="queryParams.status" class="parent-tabs" @tab-click="tabClick" v-if="!mobile">
      <el-tab-pane v-for="item in statusTabs" :key="item.value" :label="item.label" :name="item.value" />
    </el-tabs>
    <div style="text-align: right;" class="search-input">
      <el-input v-model="queryParams.no" placeholder="支付单号" @keyup.enter="handleQuery">
          <template #suffix>
            <Icon class="mr-5px" icon="ep:search" @click="handleQuery" style="cursor: pointer;" />
          </template>
          <template #append>
            <el-button :icon="Operation" @click="showSearchMore" />
          </template>
        </el-input>
    </div>
  </div>
  <div class="searchMore" v-show="searchMoreShow" :style="{ width: (mobile ? '88.7%' : '96%') }">
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="应用编号" prop="appId">
        <el-select
          clearable
          v-model="queryParams.appId"
          placeholder="请选择应用信息"
          class="!w-240px"
        >
          <el-option v-for="item in appList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付渠道" prop="channelCode">
        <el-select
          v-model="queryParams.channelCode"
          placeholder="请选择支付渠道"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PAY_CHANNEL_CODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商户单号" prop="merchantOrderId">
        <el-input
          v-model="queryParams.merchantOrderId"
          placeholder="请输入商户单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付单号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入支付单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="渠道单号" prop="channelOrderNo">
        <el-input
          v-model="queryParams.channelOrderNo"
          placeholder="请输入渠道单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择支付状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.PAY_ORDER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <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-220px"
        />
      </el-form-item>
      <el-row>
        <el-col>
          <el-form-item>
            <el-button @click="hideSearchMore">
              收起
            </el-button>

            <el-button @click="resetQuery">
              重置
            </el-button>
            <el-button @click="handleQuery">
              <Icon icon="ep:search" />搜索
            </el-button>
            <!-- <el-button
            type="success"
            plain
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['system:tenant:export']"
          >
            <Icon icon="ep:download" class="mr-5px" /> 导出
          </el-button> -->
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
  <el-row>
    <el-col v-for="(o, index) in list" class="card" :key="index" :span="7" :xl="7" :lg="7" :md="7" :sm="24" :xs="24">
      <el-card @click="openTabs('view', o)">
        <div style="display: flex;justify-content: space-between;align-items: center;margin-bottom: 5px;">
          <p style="width: 100%;white-space: nowrap;text-overflow: ellipsis;overflow: hidden;font-size: 16px;color:#000"
            :title="o.subject">{{ o.subject }}
          </p>
          <!-- <div v-show="!mobile" class="setting" @click.stop="openTabs('update', o)" v-hasPermi="['system:user:update']">
            <el-icon size="20" color="rgb(220 223 231)">
              <Setting />
            </el-icon>
          </div> -->
        </div>
        <div style="display:flex;">
          <div style="width: 216px; height: 122px; margin-right:10px; border:1px solid rgb(220, 223, 231); border-radius: 5px; display: flex; align-items: center; justify-content: center; overflow: hidden;">
            <img :src="o.picUrl" style="width: 100%; height: 100%; object-fit: cover; object-position: center;" />
          </div>

          <div style='line-height: 25px;'>
            <p>￥{{ parseFloat((o.price) / 100).toFixed(2) }}</p>
            <p>{{ o.orderNo || '暂无'}}</p>
            <p>{{ getDictLabel(DICT_TYPE.PAY_ORDER_STATUS,o.status) || '暂无'}}</p>
            <p>{{ getDictLabel(DICT_TYPE.PAY_CHANNEL_CODE,o.channelCode) || '暂无' }}</p>
            <p>{{ formatDate(o.createTime, 'YYYY/MM/DD hh:mm') }}</p>
          </div>
        </div>
      </el-card>
    </el-col>
  </el-row>
  <div v-if="list.length == 0">
    <p style="text-align: center;margin-top: 100px;">暂无收款订单</p>
  </div>

  <!-- 分页 -->
  <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  <!-- 表单弹窗：预览 -->
  <PayOrderTabs ref="PayOrderTabsRef" @success="getList" />
</template>
<script lang="ts" setup>
import { formatDate } from '@/utils/formatTime'
import { DICT_TYPE, getIntDictOptions, getStrDictOptions,getDictLabel } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as OrderApi from '@/api/pay/order'
import download from '@/utils/download'

defineOptions({ name: 'PayOrder' })

const message = useMessage() // 消息弹窗

const loading = ref(false) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  appId: null,
  channelCode: null,
  merchantOrderId: null,
  channelOrderNo: null,
  no: null,
  status: '',
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出等待
const appList = ref([]) // 支付应用列表集合

/*********************** 中星查询列表开始 **********************/

import { Setting, Search, Operation } from '@element-plus/icons-vue'
// 从路由中引入store中的app仓库
import { useAppStore } from '@/store/modules/app'
import { useRouter } from 'vue-router'
import PayOrderTabs from './PayOrderTabs.vue'
const appStore = useAppStore()
const mobile = computed(() => appStore.getMobile)
const searchMoreShow = ref(false)
// 打开更多搜索条件
const showSearchMore = () => {
  searchMoreShow.value = !searchMoreShow.value
}
const hideSearchMore = () => {
  searchMoreShow.value = false
}
const PayOrderTabsRef = ref()

const { push } = useRouter()
const openTabs = (type: string, obj?: object) => {
  if (mobile.value) {
    const userObj = JSON.stringify(obj)
    // MemberUserMobileTab
		push({ name: 'PayOrderMobileTab', query: { 'type':type,'obj':userObj } })
	} else {
    PayOrderTabsRef.value.open(type, obj)
	}
}
const statusTabs = ref([
  {
    label: '全部',
    value: ''
  }
])
/************************ 中星查询列表结束 ***************************/


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

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

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

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await OrderApi.exportOrder(queryParams)
    download.excel(data, '支付订单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 预览详情 */
const detailRef = ref()
const openDetail = (id: number) => {
  detailRef.value.open(id)
}
/** tab 切换 */
const tabClick = async (tab: TabsPaneContext) => {
  queryParams.status = tab.paneName
  await getList()
}
/** 初始化 **/
onMounted(async () => {
  await getList()
  // 设置 statuses 过滤
  for (const dict of getIntDictOptions(DICT_TYPE.PAY_ORDER_STATUS)) {
    statusTabs.value.push({
      label: dict.label,
      value: dict.value
    })
  }
})
</script>
<style>
.order-font {
  padding: 2px 0;
  font-size: 12px;
}
</style>
<style src="@/styles/SearchIndex.css"  lang="scss" scoped></style>
<style lang="scss" scoped>
.setting {
  display: none !important;
}

.card:hover {
  .setting {
    display: block !important;

    .el-icon {
      margin-top: 5px;
    }
  }
}
</style>