<template>
  <view class="container">
    <uv-tabs :list="tabList" @click="clickTab"></uv-tabs>
    <view style="padding-top: 20rpx;">
      <uv-input placeholder="请输入车牌号" shape="circle" prefixIcon="search" prefixIconStyle="font-size: 22px;color: #909399"
        @confirm="handleSearchCar"></uv-input>
    </view>
    <mescroll-uni ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption"
      :up="upOption" :fixed="false" :top="10" style="padding: 30rpx;">
      <view>
        <view v-for="item, index in carList" :key="'car' + index" class="modern-card">
          <view class="modern-card-header">
            <view class="modern-card-title">
              <text class="car-plate">{{ item.licensePlate }}</text>
            </view>
            <view class="car-status" :class="'status-' + item.status">{{ getStatusText(item.status) }}</view>
          </view>
          <view class="modern-card-body">
            <view class="avatar-section">
              <view class="avatar">
                <text>{{ item.driverUser ? item.driverUser.nickName[0] : '无' }}</text>
              </view>
              <view>
                <text class="applier-name">{{ item.driverUser ? item.driverUser.nickName : '无' }}</text>
                <text class="dept-name">驾驶员</text>
              </view>
            </view>
            <view class="info-row">
              <text class="info-label">品牌：</text>
              <text class="info-value">{{ item.brand }}</text>
            </view>
            <view class="info-row">
              <text class="info-label">购置时间：</text>
              <text class="info-value">{{ item.acquisitionTime }}</text>
            </view>
            <view class="info-row">
              <text class="info-label">是否为租车：</text>
              <text class="info-value">{{ item.isRent == "0" ? '否' : '是' }}</text>
            </view>
            <view class="info-row">
              <text class="info-label">油耗（km）：</text>
              <text>{{ item.fuel }}元</text>
            </view>
            <view class="info-row">
              <text class="info-label">保险（天）：</text>
              <text>{{ item.insurance }}元</text>
            </view>
            <view class="info-row">
              <text class="info-label">维修（天）：</text>
              <text>{{ item.maintenance }}元</text>
            </view>
            <view class="info-row">
              <text class="info-label">单日成本：</text>
              <text class="manager-amount-highlight-detail">￥{{ item.dayCost }}</text>
            </view>
          </view>
          <view class="modern-card-footer">
            <button class="footer-btn detail-btn" @click="showExpenseList(item)">维保记录</button>
            <button class="footer-btn detail-btn2" @click="showUseCarList(item)">派出记录</button>
          </view>
        </view>
      </view>
    </mescroll-uni>
    <uv-popup ref="expensePopup" mode="bottom">
      <view class="popup-detail-list" @touchmove.stop>
        <view class="popup-close-btn" @click="$refs.expensePopup.close()">×</view>
        <view class="popup-detail-title">维保记录</view>
        <view v-for="item, index in expenseList" :key="'c' + index" class="popup-detail-item"
          v-if="expenseList.length > 0">
          <view class="popup-detail-main">
            <view class="popup-detail-row">
              <text class="popup-detail-label">负责人：</text>
              <text class="popup-detail-value">{{ item.user ? item.user.nickName : '' }}</text>
            </view>
            <view class="popup-detail-row">
              <text class="popup-detail-label">费用类型：</text>
              <text class="popup-detail-value">{{ item.expenseType }}</text>
            </view>
            <view class="popup-detail-row">
              <text class="popup-detail-label">费用金额：</text>
              <text class="popup-detail-value">{{ item.expense }}</text>
            </view>
            <view class="popup-detail-row">
              <text class="popup-detail-label">发生日期：</text>
              <text class="popup-detail-value">{{ item.occurDate }}</text>
            </view>
            <view class="popup-detail-row">
              <text class="popup-detail-label">备注：</text>
              <text class="popup-detail-value">{{ item.remark }}</text>
            </view>
          </view>
        </view>
        <view v-else>
          <view class="popup-detail-empty">暂无维保记录</view>
        </view>
      </view>
    </uv-popup>
  </view>
</template>

<script>
import { listCar, getCar, delCar, addCar, updateCar } from "@/api/oa/car/car";
import { listDeviceExpense, getDeviceExpense, delDeviceExpense, addDeviceExpense, updateDeviceExpense } from "@/api/oa/device/deviceExpense";
import MescrollMixin from '@/uni_modules/mescroll/components/mescroll-uni/mescroll-mixins.js';
export default {
  mixins: [MescrollMixin],
  data() {
    return {
      tabList: [{
        name: '可使用',
        value: '1'
      }, {
        name: '已派出',
        value: '0'
      }, {
        name: '已还车',
        value: '3'
      }, {
        name: '已报废',
        value: '4'
      }],
      selectedTab: '可使用',
      carList: [],
      expenseList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        licensePlate: null,
        driver: null,
        status: '1'
      },
      mescroll: null,
      downOption: {
        auto: true,
        textOutOffset: '下拉刷新',
        textLoading: '加载中...'
      },
      upOption: {
        auto: false,
        page: { num: 1, size: 50 },
        noMoreSize: 5,
        empty: { tip: '暂无借款数据' },
        textNoMore: '~ 没有更多数据了 ~'
      },
    }
  },
  onLoad: function (options) {
    uni.startPullDownRefresh();
  },
  methods: {
    handleSearchCar(e) {
      this.queryParams.licensePlate = e;
      uni.startPullDownRefresh();
    },
    clickTab(obj) {
      let index = obj.index;
      this.selectedTab = this.tabList[index].name;
      this.queryParams.status = this.tabList[index].value;
      uni.startPullDownRefresh();
    },
    // 加载数据
    async loadData() {
      try {
        let res = null;
        res = await listCar(this.queryParams)
        return { data: res.rows, total: res.total }
      } catch (error) {
        console.error(error);
        return { data: [] };
      }
    },
    mescrollInit(mescroll) {
      this.mescroll = mescroll;
    },
    async downCallback() {
      this.carList = []; // 清空列表
      this.queryParams.pageNum = 1
      const res = await this.loadData()
      if (res.error) {
        this.mescroll.endErr();
      } else {
        this.carList = res.data;
        this.mescroll.endSuccess(res.data.length, res.total);
      }
      uni.stopPullDownRefresh();
    },
    async upCallback(page) {
      this.queryParams.pageNum = page.num
      const res = await this.loadData()
      if (res.error) {
        this.mescroll.endErr();
      } else {
        this.carList = this.carList.concat(res.data);
        this.mescroll.endSuccess(res.data.length, res.total);
      }
    },
    getStatusText(status) {
      switch (status) {
        case '0': return '已派出';
        case '1': return '可使用';
        case '4': return '已报废';
        case '3': return '已还车';
        default: return '未知';
      }
    },
    showExpenseList(item) {
      listDeviceExpense({ carId: item.carId, pageNum: 1, pageSize: 100 }).then(res => {
        this.expenseList = res.rows;
        this.$refs.expensePopup.open();
      })
    },
    showUseCarList(item) {
      uni.navigateTo({
        url: `/pages/oa/car/useCarList?carId=${item.carId}&licensePlate=${item.licensePlate}`
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 20rpx;
  background-color: #f5f5f5;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.modern-card {
  background: #fff;
  border-radius: 18rpx;
  margin-bottom: 32rpx;
  box-shadow: 0 6rpx 16rpx rgba(0, 0, 0, 0.08);
  overflow: hidden;
  transition: box-shadow 0.2s;
  border: 1rpx solid #f0f0f0;
}

.modern-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(90deg, #e0e7ff 0%, #f8fafc 100%);
  padding: 24rpx 28rpx 16rpx 28rpx;
}

.modern-card-title {
  display: flex;
  flex-direction: column;
}

.car-plate {
  font-size: 32rpx;
  color: #22223b;
  font-weight: bold;
  margin-bottom: 8rpx;
}

.car-status {
  font-size: 24rpx;
  font-weight: 500;
  padding: 4rpx 16rpx;
  border-radius: 8rpx;
  display: inline-block;
  width: fit-content;
}

.status-0 {
  background: #fef3c7;
  color: #d97706;
}

/* 已派出 */
.status-1 {
  background: #d4f3e9;
  color: #10b981;
}

/* 可使用 */
.status-4 {
  background: #fee2e2;
  color: #ef4444;
}

/* 已报废 */
.status-3 {
  background: #dbeafe;
  color: #3b82f6;
}

/* 已还车 */

.modern-card-body {
  padding: 20rpx 28rpx 24rpx 28rpx;
}

.avatar-section {
  display: flex;
  align-items: center;
  margin-bottom: 18rpx;
}

.avatar {
  width: 56rpx;
  height: 56rpx;
  border-radius: 50%;
  background: #a5b4fc;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28rpx;
  font-weight: bold;
  margin-right: 18rpx;
}

.applier-name {
  font-size: 28rpx;
  color: #22223b;
  font-weight: 500;
}

.dept-name {
  font-size: 24rpx;
  color: #64748b;
  margin-left: 8rpx;
}

.info-row {
  display: flex;
  margin-bottom: 10rpx;
}

.info-label {
  color: #64748b;
  font-size: 24rpx;
  min-width: 120rpx;
}

.info-value {
  color: #22223b;
  font-size: 24rpx;
  flex: 1;
  word-break: break-all;
}

.amount-highlight-detail {
  background: #fef3c7;
  color: #d97706;
  font-size: 28rpx;
  font-weight: bold;
  border-radius: 10rpx;
  padding: 4rpx 18rpx;
  margin-left: 8rpx;
}

.manager-amount-highlight-detail {
  background: #d4f3e9;
  color: #10b981;
  font-size: 28rpx;
  font-weight: bold;
  border-radius: 10rpx;
  padding: 4rpx 18rpx;
  margin-left: 8rpx;
}

.modern-card-footer {
  display: flex;
  justify-content: flex-end;
  gap: 20rpx;
  padding: 16rpx 28rpx 20rpx 28rpx;
  border-top: 1px solid #f0f0f0;
  background: #fafbfc;
}

.footer-btn {
  font-size: 26rpx;
  border-radius: 8rpx;
  border: none;
  outline: none;
  background: #e0e7ff;
  color: #3b3b4f;
}

.detail-btn {
  background: #6366f1;
  color: #fff;
}

.detail-btn2 {
  background: #f4c542;
  color: #fff;
}

.popup-detail-list {
  padding: 32rpx 24rpx 48rpx 24rpx;
  background: #fff;
  border-radius: 24rpx 24rpx 0 0;
  min-height: 300rpx;
  max-height: 70vh;
  overflow-y: auto;
  position: relative;
}

.popup-detail-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #22223b;
  margin-bottom: 24rpx;
  text-align: center;
}

.popup-detail-item {
  display: flex;
  align-items: flex-start;
  background: #f7f8fa;
  border-radius: 12rpx;
  margin-bottom: 18rpx;
  padding: 18rpx 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.popup-detail-index {
  width: 44rpx;
  height: 44rpx;
  background: #6366f1;
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24rpx;
  font-weight: bold;
  margin-right: 18rpx;
}

.popup-detail-main {
  flex: 1;
}

.popup-detail-row {
  display: flex;
  align-items: center;
  margin-bottom: 8rpx;
}

.popup-detail-label {
  color: #64748b;
  font-size: 24rpx;
  min-width: 120rpx;
}

.popup-detail-value {
  color: #22223b;
  font-size: 24rpx;
}

.popup-detail-amount {
  color: #d97706;
  font-size: 28rpx;
  font-weight: bold;
  margin-left: 8rpx;
}

.popup-detail-manager-amount {
  color: #10b981;
  font-size: 28rpx;
  font-weight: bold;
  margin-left: 8rpx;
}

.popup-close-btn {
  position: absolute;
  right: 24rpx;
  top: 18rpx;
  font-size: 40rpx;
  color: #999;
  z-index: 10;
  cursor: pointer;
  line-height: 1;
}
</style>