<template>
  <div class="app-container">
    <!-- 查询及操作区域 -->
    <div class="head-container">
      <div class="filter-container">
        <el-input
          v-model="query.batchNo"
          placeholder="请输入结算单批次号"
          class="filter-item"
          style="width: 200px;"
          clearable
        />
        <el-button type="primary" class="filter-item" @click="handleSearch">
          <i class="el-icon-search" style="color: white;" />
          查询
        </el-button>
        <el-button type="primary" class="filter-item" @click="handleGenerate">
          生成结算单
        </el-button>
      </div>
    </div>

    <!-- 表格 -->
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column prop="batchNo" label="订单批次号" align="center" />
      <el-table-column prop="settlementTimeScope" label="结算时间" width="180" align="center" />
      <el-table-column prop="operator" label="制单人" align="center" />
      <el-table-column prop="updateTime" label="导出时间" width="180" align="center" />
      <el-table-column prop="settlementStatus" label="结算状态" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.settlementStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleDetail(scope.row)"
          >
            查看详情
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="handlePrint(scope.row)"
          >
            打印结算单
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        :current-page.sync="page.current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.size"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getSettleOrderList } from '@/api/order'
import { getUserNameValue } from '@/api/dropdown'

export default {
  name: 'settlementOrder',
  data() {
    return {
      loading: false,
      query: {
        batchNo: ''
      },
      page: {
        current: 1,
        size: 10,
        total: 2
      },
      tableData: [
        {
          attractionId: 2,
          batchNo: "JS_2025072600008",
          createBy: "admin",
          createTime: "2025-07-26 14:42:38",
          operatorId: 1,
          settlementStatus: 2,
          settlementTimeScope: "2025-07-19 17:00:00~2025-07-19 17:00:00",
          updateBy: "admin",
          updateTime: "2025-07-26 14:42:38"
        },
      ],
      selection: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      this.loading = true
      try {
        const response = await getSettleOrderList(this.query)
        const operatorList = await getUserNameValue(0); // 只请求一次，避免每条都请求

        const operatorMap = new Map();
        operatorList.forEach(item => {
          operatorMap.set(item.key, item.value);
        });
        response.records = response.records.map(record => ({
          ...record,
          settlementStatus: this.getSettlementStatus(record.settlementStatus),
          operator: record.operatorId ? (operatorMap.get(record.operatorId) || '未知') : '未知',
        }))
        console.log('获取结算单列表:', response)
        this.tableData = response.records || []
        this.page.total = Number(response.total) || 0
      } catch (error) {
        console.error('获取结算单列表失败:', error)
      } finally {
        this.loading = false
      }
    },
    getSettlementStatus(status) {
      const statusMap = {
        0: "结算中",
        1: "已确认",
        2: "结算完成",
        3: "已取消",
      };
      return statusMap[status] || "未知状态";
    },
    async getOperatorName(operatorId) {
      const response = await getUserNameValue(0);
      if (!response || response.length === 0) {
        return '未知';
      }
      const operatorName = response.find(item => item.key === operatorId);
      return operatorName.value || '未知';
    },
    handleSearch() {
      // 实现搜索逻辑
      console.log('搜索条件:', this.query)
    },
    handleSelectionChange(val) {
      this.selection = val
    },
    handleSizeChange(val) {
      this.page.size = val
      // 重新加载数据
    },
    handleCurrentChange(val) {
      this.page.current = val
      // 重新加载数据
    },
    handleDetail(row) {
      // 查看详情
      console.log('查看详情:', row)
      // 跳转到订单详情页
      this.$router.push({
        path: '/order/orderDetail',
        query: { orderId: row.id.toString() }
      })
    },
    handlePrint(row) {
      // 打印结算单
      console.log('打印结算单:', row)
    },
    handleGenerate() {
      this.$router.push('/order/generateOrder')
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
}

.filter-container {
  display: flex;
  align-items: center;
  gap: 10px;

  .filter-item {
    margin-right: 10px;
  }
}

// .pagination-container {
//   position: fixed;
//   bottom: 0;
//   right: 0;
//   left: 200px;
//   height: 150px;
//   background: white;
//   padding: 10px;
//   text-align: left;
//   box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.08);
// }
// .el-pagination {
//   padding: 2px 25px;
// }


.el-table {
  margin-bottom: 70px;
}


.el-button--text {
  color: #409EFF;
  &:hover {
    color: #66b1ff;
  }
}

.el-table {
  margin-top: 20px;
  ::v-deep .el-table__body-wrapper {
    overflow-x: auto;
  }
}
</style>
