<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="85%">
    <!-- 搜索 -->
  <div class="search-one">
    <div style="text-align: right;" class="search-input">
      <el-input v-model="queryParams.nickName" 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="brotherPrice">
        <el-input v-model="queryParams.brotherPrice" placeholder="请输入兄弟分区额度" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="后代分区额度" prop="sonPrice">
        <el-input v-model="queryParams.sonPrice" placeholder="请输入子分区额度" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="对碰后存余" prop="afterHitQuota">
        <el-input v-model="queryParams.afterHitQuota" 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-form-item label="" prop="createTime">
        
      <el-button :type="queryParams.isSelectCrash ? 'primary' : 'default'" @click="toggleButton">
        是否碰撞
      </el-button>
    </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-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
    <!-- 列表 -->
    <ContentWrap style="margin-top: 10px;max-height: 550px;min-height: 550px;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="nickName" />
        <!-- <el-table-column label="分区编号" align="center" prop="partNo" /> -->
        <el-table-column label="兄弟分区计算后额度" align="center" prop="brotherPrice" >
        <template #default="scope">
          <span>{{ pointsToPoint(scope.row.brotherPrice) }}</span>
        </template>
      </el-table-column>  
        <el-table-column label="后代分区计算后额度" align="center" prop="sonPrice" >
        <template #default="scope">
          <span>{{ pointsToPoint(scope.row.sonPrice) }}</span>
        </template>
      </el-table-column>  
        <el-table-column label="对碰后存余" align="center" prop="afterHitQuota" >
        <template #default="scope">
          <span>{{ pointsToPoint(scope.row.afterHitQuota) }}</span>
        </template>
      </el-table-column>  
        <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      </el-table>
      <!-- 分页 -->
      <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
        @pagination="getList" />
    </ContentWrap>
    <!-- 分页 -->

  </Dialog>
</template>
<script setup lang="ts">
import { PartitionCrashApi, PartitionCrashVO } from '@/api/system/distri/partitioncrash'
import { dateFormatter } from '@/utils/formatTime'
import { pointsToPoint } from '@/utils'
import { Setting, Search, Operation } from '@element-plus/icons-vue'

import { useAppStore } from '@/store/modules/app'
const appStore = useAppStore()
const mobile = computed(() => appStore.getMobile)
/** 分区碰撞 表单 */
defineOptions({ name: 'PartitionCrashForm' })

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

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改


const loading = ref(true) // 列表的加载中
const list = ref<PartitionCrashVO[]>([]) // 列表的数据
// 列表的总页数
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  nickName:undefined,
  userId: undefined,
  partNo: undefined,
  brotherPrice: undefined,
  sonPrice: undefined,
  afterHitQuota: undefined,
  createTime: [],
  everyday:undefined,
  isSelectCrash:false
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PartitionCrashApi.getPartitionCrashPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
    hideSearchMore()
  }
}
function toggleButton() {
  queryParams.isSelectCrash = !queryParams.isSelectCrash; // 切换状态
  getList()
}


const searchMoreShow = ref(false)
// 打开更多搜索条件
const showSearchMore = () => {
  searchMoreShow.value = !searchMoreShow.value
}
const hideSearchMore = () => {
  searchMoreShow.value = false;
  
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
const queryFormRef = ref(null)
/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.pageNo = 1
  queryParams.isSelectCrash = false
  queryParams.nickName = undefined
  queryFormRef.value?.resetFields()
  hideSearchMore()
  getList()
}

/** 打开弹窗 */
const open = async (everyday) => {
  dialogVisible.value = true
  dialogTitle.value = "详情"
  queryParams.isSelectCrash = false
  // 修改时，设置数据
  if (everyday) {
    queryParams.everyday = everyday
    resetQuery()
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>
<style src="@/styles/SearchIndex.css"  lang="scss" scoped></style>