<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="用户" prop="uid">
        <user-select
          v-model="queryParams.uid"
          placeholder="请选择用户"
          user-type="1"
          dialog-title="选择用户"
          style="width: 200px;"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 统计卡片 -->
    <el-row :gutter="20" style="margin-bottom: 20px;">
      <el-col :span="8">
        <el-card shadow="hover" class="statistics-card income-card">
          <div class="statistics-content">
            <div class="statistics-icon">
              <i class="el-icon-money" style="color: #67C23A; font-size: 24px;"></i>
            </div>
            <div class="statistics-info">
              <div class="statistics-label">总收入</div>
              <div class="statistics-value">¥{{ formatMoney(statistics.income) }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover" class="statistics-card expense-card">
          <div class="statistics-content">
            <div class="statistics-icon">
              <i class="el-icon-shopping-cart-2" style="color: #F56C6C; font-size: 24px;"></i>
            </div>
            <div class="statistics-info">
              <div class="statistics-label">总支出</div>
              <div class="statistics-value">¥{{ formatMoney(statistics.expense) }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover" class="statistics-card balance-card">
          <div class="statistics-content">
            <div class="statistics-icon">
              <i class="el-icon-wallet" style="color: #409EFF; font-size: 24px;"></i>
            </div>
            <div class="statistics-info">
              <div class="statistics-label">余额</div>
              <div class="statistics-value" :style="{ color: statistics.balance >= 0 ? '#67C23A' : '#F56C6C' }">
                ¥{{ formatMoney(statistics.balance) }}
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:PayMoneyLog:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:PayMoneyLog:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:PayMoneyLog:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:PayMoneyLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="PayMoneyLogList" @selection-change="handleSelectionChange" border>
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="ID" align="center" width="55"   prop="id" />


      <el-table-column label="用户" align="center" prop="uname" />
      <el-table-column label="订单ID" align="center" prop="ocode" />
      <el-table-column label="金额" width="120px" align="left"  prop="price" >
        <template slot-scope="scope">
          <span style="color: #E6A23C; font-weight: bold; font-size: 14px;">
            ¥{{ formatMoney(scope.row.price) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="mark" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:PayMoneyLog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:PayMoneyLog:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改支付记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单ID" prop="oid">
          <el-input v-model="form.oid" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="用户" prop="uid">
          <user-select
            v-model="form.uid"
            placeholder="请选择用户"
            user-type="1"
            dialog-title="选择用户"
          />
        </el-form-item>
        <el-form-item label="金额" prop="price">
          <div style="position: relative;">
            <el-input v-model="form.price" placeholder="请输入金额" />
            <span style="position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: #E6A23C; font-weight: bold; pointer-events: none;">¥</span>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="mark">
          <el-input v-model="form.mark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable
            v-model="form.payTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPayMoneyLog, getPayMoneyLog, delPayMoneyLog, addPayMoneyLog, updatePayMoneyLog, getPayMoneyLogStatistics } from "@/api/system/PayMoneyLog"

export default {
  name: "PayMoneyLog",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,


      // 支付记录表格数据
      PayMoneyLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        oid: null,
        orderId: null,
        uid: null,
        price: null,
        mark: null,
        payTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        oid: [
          { required: true, message: "订单ID不能为空", trigger: "blur" }
        ],
        orderId: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        uid: [
          { required: true, message: "用户不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "金额不能为空", trigger: "blur" }
        ],
        mark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        payTime: [
          { required: true, message: "支付时间不能为空", trigger: "blur" }
        ],
      },
      // 统计数据
      statistics: {
        income: 0,
        expense: 0,
        balance: 0
      }
    }
  },
  created() {
    this.getList()
    this.getStatistics()
  },
  methods: {
    /** 查询支付记录列表 */
    getList() {
      this.loading = true
      listPayMoneyLog(this.queryParams).then(response => {
        this.PayMoneyLogList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        oid: null,
        orderId: null,
        uid: null,
        price: null,
        mark: null,
        payTime: null,
        createdAt: null,
        updatedAt: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
      this.getStatistics()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },


    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加支付记录"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getPayMoneyLog(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改支付记录"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePayMoneyLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
              this.getStatistics()
            })
          } else {
            addPayMoneyLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
              this.getStatistics()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除支付记录编号为"' + ids + '"的数据项？').then(function() {
        return delPayMoneyLog(ids)
      }).then(() => {
        this.getList()
        this.getStatistics()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/PayMoneyLog/export', {
        ...this.queryParams
      }, `PayMoneyLog_${new Date().getTime()}.xlsx`)
    },
    // 格式化金额
    formatMoney(value) {
      if (value === null || value === undefined || value === '') {
        return '0.00';
      }
      return parseFloat(value).toFixed(2);
    },
    /** 获取统计数据 */
    getStatistics() {
      getPayMoneyLogStatistics().then(response => {
        this.statistics = response.data
      }).catch(() => {
        this.statistics = {
          income: 0,
          expense: 0,
          balance: 0
        }
      })
    }
  }
}
</script>

<style scoped>
.statistics-card {
  border-radius: 8px;
  transition: all 0.3s ease;
}

.statistics-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.statistics-content {
  display: flex;
  align-items: center;
  padding: 10px 0;
}

.statistics-icon {
  margin-right: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: rgba(103, 194, 58, 0.1);
}

.expense-card .statistics-icon {
  background-color: rgba(245, 108, 108, 0.1);
}

.balance-card .statistics-icon {
  background-color: rgba(64, 158, 255, 0.1);
}

.statistics-info {
  flex: 1;
}

.statistics-label {
  font-size: 14px;
  color: #606266;
  margin-bottom: 5px;
}

.statistics-value {
  font-size: 20px;
  font-weight: bold;
  color: #303133;
}

.income-card .statistics-value {
  color: #67C23A;
}

.expense-card .statistics-value {
  color: #F56C6C;
}
</style>
