<template>
  <!-- 搜索 -->
  <div class="search-one" >
      <div style="text-align: right;" class="search-input">
        <el-input v-model="queryParams.name" placeholder="用户昵称" clearable @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="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="售价" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入售价"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="成本" prop="cost">
        <el-input
          v-model="queryParams.cost"
          placeholder="请输入成本"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="用户自得" prop="grossProfitUserQuota">
        <el-input
          v-model="queryParams.grossProfitUserQuota"
          placeholder="请输入用户自得"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="直推奖" prop="grossProfitAncestorQuota">
        <el-input
          v-model="queryParams.grossProfitAncestorQuota"
          placeholder="直推奖"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="平台服务费" prop="grossProfitPlatformQuota">
        <el-input
          v-model="queryParams.grossProfitPlatformQuota"
          placeholder="请输入平台服务费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </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-240px"
        />
      </el-form-item>
        <el-row>
          <el-col>
            <el-form-item>
              <el-button @click="hideSearchMore">
                收起
              </el-button>
              <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-button type="success" plain @click="handleExport" :loading="exportLoading"
                v-hasPermi="['distri:integral:export']">
                <Icon icon="ep:download" class="mr-5px" /> 导出
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>


  <!-- 列表 -->
  <ContentWrap style="margin-top:10px;padding: 10px;">
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="订单编号" align="center" prop="orderNo" width="200" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <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="cost" >
        <template #default="scope"> 
          ￥{{ fenToYuan(scope.row.cost)}}
        </template>
      </el-table-column>
      <el-table-column label="推广费" align="center" prop="cost" >
        <template #default="scope"> 
          ￥{{fenToYuan(scope.row.price) -  fenToYuan(scope.row.cost)}}
        </template>
      </el-table-column>
      <el-table-column label="商品数量" align="center" prop="productCount" />

      <el-table-column label="用户自得" align="center" prop="grossProfitUserQuota">
        <template #default="scope"> 
          {{pointsToPoint(scope.row.grossProfitUserQuota )}}
        </template>
      </el-table-column>
      <el-table-column label="直推奖" align="center" prop="grossProfitAncestorQuota">
        <template #default="scope"> 
          {{pointsToPoint(scope.row.grossProfitAncestorQuota )}}
        </template>
      </el-table-column>
      <el-table-column label="平台服务费" align="center" prop="grossProfitPlatformQuota">
        <template #default="scope"> 
          {{pointsToPoint(scope.row.grossProfitPlatformQuota )}}
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <!-- <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['distri:order-calc:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['distri:order-calc:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

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

<script setup lang="ts">
import { pointsToPoint,fenToYuan } from '@/utils'

import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { OrderCalcApi, OrderCalcVO } from '@/api/system/distri/ordercalc'
import OrderCalcForm from './OrderCalcForm.vue'
import { Setting, Search, Operation } from '@element-plus/icons-vue'
import { useAppStore } from '@/store/modules/app'
const appStore = useAppStore()
const mobile = computed(() => appStore.getMobile)
const searchMoreShow = ref(false)
// 打开更多搜索条件
const showSearchMore = () => {
  searchMoreShow.value = !searchMoreShow.value
}
const hideSearchMore = () => {
  searchMoreShow.value = false
}
/** 订单产生数据计算 列表 */
defineOptions({ name: 'OrderCalc' })

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

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

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrderCalcApi.getOrderCalcPage(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 OrderCalcApi.deleteOrderCalc(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await OrderCalcApi.exportOrderCalc(queryParams)
    download.excel(data, '订单产生数据计算.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

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

  


<style src="@/styles/SearchIndex.css"  lang="scss" scoped></style>