<template>
  <el-config-provider :locale="zhCn">
    <el-container class="content">
      <div class="work-area">
        <fieldset class="search-area">
          <el-form ref="searchQueryFormRef" :model="searchQueryFormEntity" :label-position="labelPosition"
                   label-width="158px" style="max-width: 100%" status-icon>
            <div style="display: flex;justify-content: space-between;">
              <el-row>
                <el-form-item label="站台类型">
                  <el-select-v2 style="width: 196px;" v-model="searchQueryFormEntity.standType" placeholder="站台类型"
                                :options="addAllOptionOfOptions(standTypeOptions)"
                                @change="search()"></el-select-v2>
                </el-form-item>
                <el-form-item label="站台ID">
                  <el-input v-model="searchQueryFormEntity.standId" @keyup.enter="search()" clearable/>
                </el-form-item>
              </el-row>
              <div style="align-content: center;">
                <el-row>
                  <el-button type="primary" class="btn-search" @click="search()">查询</el-button>
                  <el-button type="warning" class="btn-search" @click="clearQuery()">清除输入</el-button>
                </el-row>
              </div>
            </div>
          </el-form>
        </fieldset>
        <div class="table-area">
          <el-table :data="tableData" stripe border v-loading="tableLoading" class="table-class"
                    :max-height="maxHeight" highlight-current-row @row-click="getCurrentRow"
                    :header-cell-style="{ 'text-align': 'center' }" :cell-style="{ 'text-align': 'center' }"
                    @sort-change="handleSortChange">
            <el-table-column width="65px" fixed="left">
              <template v-slot="scope">
                <el-radio :label="scope.row.standId" v-model="standId">&nbsp;</el-radio>
              </template>
            </el-table-column>
            <el-table-column prop="standId" label="站台ID" fixed="left" min-width="120px" sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="standName" label="站台名称" min-width="150px" sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="standType" label="站台类型" min-width="120px" :formatter="standTypeFormat"
                             sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="standStatus" label="站台状态" :formatter="standStatusFormat"
                             min-width="120px" sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="standIp" label="站台IP" min-width="150px" sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="standDesc" label="站台描述" min-width="180px" sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column prop="lastUpdateTime" label="最后更新时间" min-width="180px" sortable="custom"
                             :formatter="timeFormat" show-overflow-tooltip/>
            <el-table-column prop="lastUpdateUser" label="最后更新人" min-width="120px" sortable="custom"
                             show-overflow-tooltip/>
            <el-table-column fixed="right" label="操作" width="170px">
                <template v-slot="scope">
                    <div style="display: inline-block; align-content: center;">
                        <el-button type="primary"
                            @click="editCurrentRowFormEntity(scope.row)">编辑</el-button>
                    </div>
                </template>
            </el-table-column>
          </el-table>
          <br/>
          <el-pagination v-model:current-page="baseTableQuery.currentPage"
                         v-model:page-size="baseTableQuery.pageSize" :page-sizes="[10, 25, 50]" :small="false"
                         :disabled="false" :background="false" :default-page-size="10" @size-change="handlePageSizeChange"
                         @current-change="processTableData" layout="total, sizes, prev, pager, next, jumper"
                         :total="baseTableQuery.total"/>
        </div>
        <el-dialog v-model="rowEditFlag" title="站台信息" width="40%" draggable :show-close="false">
          <el-form ref="rowEditFormRef" :model="rowFormEntity" :label-position="labelPosition"
                   label-width="100px" style="max-width: 100%" status-icon>
            <el-row :gutter="16">
              <el-col :span="12" :offset="0">
                <el-form-item label="站台ID">
                  <el-input v-model="rowFormEntity.standId" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="站台名称">
                  <el-input v-model="rowFormEntity.standName"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="16">
              <el-col :span="12" :offset="0">
                <el-form-item label="站台类型">
                  <el-select-v2 v-model="rowFormEntity.standType" placeholder="请选择站台类型"
                                :options="standTypeOptions"></el-select-v2>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="站台状态">
                  <el-select-v2 v-model="rowFormEntity.standStatus" placeholder="请选择站台状态"
                                :options="standStatusOptions"></el-select-v2>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="16">
              <el-col :span="12" :offset="0">
                <el-form-item label="站台IP">
                  <el-input v-model="rowFormEntity.standIp"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="站台描述">
                  <el-input v-model="rowFormEntity.standDesc"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <template #footer>
                        <span class="dialog-footer">
                            <el-button type="primary" @click="rowEditFlag = false">取消</el-button>
                            <el-button type="success" @click="submitUpdateRow(rowFormEntity)">
                                确定
                            </el-button>
                        </span>
          </template>
        </el-dialog>
      </div>
    </el-container>
  </el-config-provider>
</template>

<script setup>
import store from '@/store'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import {timeFormatter} from '@/utils/formatter.js'
import {ref, reactive, onMounted, nextTick, onBeforeUnmount} from 'vue'
import {ElMessage} from 'element-plus'
import {genTableRequest, addAllOptionOfOptions} from '@/utils/generator.js'
import {labelPosition} from '@/constant/form.js'
import {loading} from '@/utils/loading'
import {getAllStands, updateStandInfo} from "@/api/stand";

/**
 * 常量定义
 */
const STAND_ID = store.getters.getStandId
const USER_NAME = store.getters.getUserName

// 站台类型选项
const standTypeOptions = [
  { value: 1, label: '拣选站台' },
  { value: 2, label: '入库站台' }
]

// 站台状态选项
const standStatusOptions = [
  { value: 0, label: '可用' },
  { value: 1, label: '不可用' }
]

/**
 * 变量定义
 */
let maxHeight = ref(window.innerHeight * 0.55)
let tableLoading = ref(false)
let tableData = ref([])
let allTableData = ref([]) // 存储完整的数据，用于前端分页和排序
let baseTableQuery = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0,
  sortBy: new Map([['standId', true]]),// 按照站台ID排序
  standId: STAND_ID,
  userName: USER_NAME
})
let searchQueryFormEntity = reactive({
  standId: '',
  standType: -99
})
let searchQueryFormRef = ref()
let rowEditFlag = ref(false)
let standId = ''
let rowEditFormRef = ref()
let rowFormEntity = reactive({})
/**
 * 系统方法
 */
onMounted(() => {
  nextTick(() => {
    window.addEventListener('resize', resizeHeight)
    search()
  })
})
onBeforeUnmount(() => {
  nextTick(() => {
    window.removeEventListener('resize', resizeHeight)
  })
})
const resizeHeight = () => {
  maxHeight.value = window.innerHeight * 0.55
}
/**
 * 自定义方法
 */
// 查询
const search = () => {
  tableLoading.value = true
  let request = {}
  // 设定查询参数
  if (searchQueryFormEntity.standId.trim()) {
    request.standId = searchQueryFormEntity.standId.trim()
  }
  if (searchQueryFormEntity.standType !== -99) {
    request.standType = searchQueryFormEntity.standType
  }
  
  getAllStands(request).then((res) => {
    const response = res.data
    if (response.code === 0) {
      const data = response.data
      if (data != null) {
        allTableData.value = data
        baseTableQuery.total = data.length
        // 处理分页和排序
        processTableData()
      } else {
        allTableData.value = []
        tableData.value = []
        baseTableQuery.total = 0
      }
    } else {
      ElMessage.error(response.message)
    }
  }).catch(err => {
    console.log(err)
    ElMessage.error('查询数据异常。')
  }).finally(() => {
    tableLoading.value = false
  })
}

// 处理分页和排序
const processTableData = () => {
  let data = [...allTableData.value]
  
  // 前端排序处理
  if (baseTableQuery.sortBy.size > 0) {
    data = sortTableData(data, baseTableQuery.sortBy)
  }
  
  // 前端分页处理
  const startIndex = (baseTableQuery.currentPage - 1) * baseTableQuery.pageSize
  const endIndex = startIndex + baseTableQuery.pageSize
  tableData.value = data.slice(startIndex, endIndex)
}

// 前端排序函数
const sortTableData = (data, sortBy) => {
  if (sortBy.size === 0) {
    return data
  }
  
  return data.slice().sort((a, b) => {
    for (const [key, ascending] of sortBy) {
      let aValue = a[key]
      let bValue = b[key]
      
      // 处理null/undefined值
      if (aValue == null && bValue == null) continue
      if (aValue == null) return ascending ? 1 : -1  // null值排在后面
      if (bValue == null) return ascending ? -1 : 1
      
      // 处理时间字符串
      if (key === 'lastUpdateTime') {
        aValue = new Date(aValue).getTime()
        bValue = new Date(bValue).getTime()
      }
      
      // 处理数字类型（standType, standStatus等）
      if (typeof aValue === 'number' && typeof bValue === 'number') {
        if (aValue !== bValue) {
          return ascending ? aValue - bValue : bValue - aValue
        }
        continue
      }
      
      // 处理字符串
      if (typeof aValue === 'string' && typeof bValue === 'string') {
        aValue = aValue.toLowerCase()
        bValue = bValue.toLowerCase()
      }
      
      // 转换为字符串进行比较（兼容混合类型）
      aValue = String(aValue)
      bValue = String(bValue)
      
      if (aValue < bValue) {
        return ascending ? -1 : 1
      }
      if (aValue > bValue) {
        return ascending ? 1 : -1
      }
    }
    return 0
  })
}
const clearQuery = () => {
  searchQueryFormEntity.standId = ''
  searchQueryFormEntity.standType = -99
  // 重置分页
  baseTableQuery.currentPage = 1
  // 重置排序
  baseTableQuery.sortBy.clear()
  baseTableQuery.sortBy.set('standId', true) // 默认按站台ID升序排序
  search()
}

// 处理页面大小变化
const handlePageSizeChange = () => {
  // 页面大小变化时，重置到第一页
  baseTableQuery.currentPage = 1
  processTableData()
}
const handleSortChange = (data) => {
  // 临时调试信息
  console.log('handleSortChange called with:', data)
  console.log('allTableData length:', allTableData.value.length)
  
  // 清空当前排序设置
  baseTableQuery.sortBy.clear()
  
  // 如果排序不为null，则设置新的排序
  if (data.order) {
    const isAscending = data.order.toLowerCase() === 'ascending'
    baseTableQuery.sortBy.set(data.prop, isAscending)
    console.log('Setting sort:', data.prop, isAscending)
  } else {
    // 如果order为null，恢复默认排序
    baseTableQuery.sortBy.set('standId', true)
    console.log('Resetting to default sort: standId')
  }
  
  // 排序变化时，重置到第一页
  baseTableQuery.currentPage = 1
  processTableData()
}
const getCurrentRow = (row) => {
  standId = row.standId
}
const timeFormat = (row, column, cellValue, index) => {
  return timeFormatter(cellValue)
}
const standTypeFormat = (row, column, cellValue, index) => {
  const typeMap = {
    1: '拣选站台',
    2: '入库站台'
  }
  return typeMap[cellValue] || '未知'
}
const standStatusFormat = (row, column, cellValue, index) => {
  return cellValue === 1 ? '可用' : '不可用'
}
// 编辑弹框
const editCurrentRowFormEntity = (row) => {
  // 设置form值
  rowFormEntity.standId = row.standId
  rowFormEntity.standName = row.standName
  rowFormEntity.standType = row.standType
  rowFormEntity.standStatus = row.standStatus
  rowFormEntity.standIp = row.standIp
  rowFormEntity.standDesc = row.standDesc
  // 弹出框
  rowEditFlag.value = true
}
// 更新当前行数据
const submitUpdateRow = (rowFormEntity) => {
  const request = {
    standId: rowFormEntity.standId,
    standName: rowFormEntity.standName,
    standType: rowFormEntity.standType,
    standStatus: rowFormEntity.standStatus,
    standIp: rowFormEntity.standIp,
    standDesc: rowFormEntity.standDesc,
    userName: 'wms-web'
  }
  loading.open('更新中...')
  updateStandInfo(request).then(res => {
    if (res.data.code == 0) {
      ElMessage.success('更新数据成功。')
      rowEditFlag.value = false
      search()
    } else {
      ElMessage.error('更新数据失败。')
    }
  }).catch(err => {
    console.log(err)
    ElMessage.error('更新数据异常。')
  }).finally(() => {
    loading.close()
  })
}
</script>
<style scoped>
.content {
  display: flex;
  width: 100%;
}

.work-area {
  width: 100%;
  /* padding: 5px; */
}

.search-area {
  margin: auto;
  min-height: fit-content;
  max-height: 40%;
  margin-bottom: 10px;
  min-width: inherit;
  border: solid 1px;
  border-radius: 10px;
  box-shadow: 0px 15px 10px -15px #000;
  overflow: auto;
  padding: 10px;
}

.table-area {
  margin: auto;
  min-height: fit-content;
  max-height: 60%;
  margin-bottom: 10px;
  min-width: inherit;
  border: solid 1px;
  border-radius: 10px;
  box-shadow: 0px 15px 10px -15px #000;
  overflow: auto;
  padding: 10px;
}

.el-form-item {
  margin: 5px 5px 5px 5px;
}

.el-form-item .el-input {
  width: 196px;
}

.el-form-item .el-input-number {
  width: 196px;
}

.el-form-item .el-select-v2 {
  width: 196px;
}

.table-class {
  margin: 5px 5px 5px 5px;
  width: inherit;
}

.el-pagination {
  padding-left: 5px;
}

.my-autocomplete li {
  width: 196px;
  line-height: normal;
  padding: 7px;
}

.my-autocomplete li .name {
  text-overflow: ellipsis;
  overflow: hidden;
}

.my-autocomplete li .addr {
  font-size: 12px;
  color: #b4b4b4;
}

.my-autocomplete li .highlighted .addr {
  color: #ddd;
}

.my-autocomplete li .goods_id {
  color: brown;
}

.my-autocomplete li .goods_name {
  color: cornflowerblue;
}

.btn-search {
  height: 30px;
  width: 80px;
  margin: auto 5px 5px auto;
  color: black;
}
</style>