<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="168px" @submit.native.prevent>

      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="订单状态" prop="orderStatus">
        <el-select
          v-model="queryParams.orderStatus"
          placeholder="请选择订单状态"
          clearable
          style="width: 140px"
        >
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="手机号码" prop="mobileNumber">
        <el-input
          v-model="queryParams.mobileNumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker
          v-model="createTimeRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :clearable=false
        ></el-date-picker>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <div>
        {{dateDescription}}期间，总用电量{{sumUsedElectricity}}度，总消费金额{{sumOrderAmount}}元
      </div>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--<el-table-column label="主键" align="center" prop="id"/>-->
      <el-table-column label="订单编号" align="center" prop="orderCode" width="180px">
        <template slot-scope="scope">
          <router-link
            :to="'/order/index/orderDetail/'+scope.row.orderCode"
            class="link-type"
          >
            <span>{{ scope.row.orderCode }}</span>
          </router-link>
        </template>
      </el-table-column>
      <!--<el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.order_status"
            :value="scope.row.orderStatus"
          />
        </template>
      </el-table-column>-->
      <el-table-column label="订单状态描述" align="center" prop="orderStatusDescribe"/>
      <el-table-column label="会员昵称" align="center" prop="nickName" width="120px"/>

      <el-table-column label="电话号码" align="center" prop="mobileNumber" width="120px"/>

      <el-table-column label="站点" align="center" prop="stationName"/>
      <el-table-column label="充电桩枪口号" align="center" prop="pileConnectorCode" width="200px"/>
      <el-table-column label="微信商户订单号" align="center" prop="outTradeNo" width="165px"/>
      <el-table-column label="启动方式" align="center" prop="startMode" width="100px">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.start_mode"
            :value="scope.row.startMode"
          />
        </template>
      </el-table-column>

      <el-table-column label="支付方式" align="center" prop="payMode">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.pay_mode"
            :value="scope.row.payMode"
          />
        </template>
      </el-table-column>

      <el-table-column label="支付状态" align="center" prop="payStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.pay_status"
            :value="scope.row.payStatus"
          />
        </template>
      </el-table-column>

      <el-table-column label="支付金额" align="center" prop="payAmount"/>
      <el-table-column label="充电度数" align="center" prop="chargingDegree" width="100px"/>
      <el-table-column label="起始soc" align="center" prop="startSoc" />
      <el-table-column label="终止soc" align="center" prop="endSoc" />
      <!--<el-table-column label="订单生成日期" align="center" prop="createTime" width="180"/>-->
      <el-table-column label="开始充电时间" align="center" prop="chargeStartTime" width="180"/>
      <el-table-column label="结束充电时间" align="center" prop="chargeEndTime" width="180"/>
      <el-table-column label="订单总金额" align="center" prop="orderAmount" width="100px"/>
    </el-table>

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

<script>
import {listOrder, totalData} from "@/api/order/order";
import Template from "@/views/billing/template";
import {listStation} from "@/api/pile/station";
import {getDay} from "@/utils/common";

export default {
  name: "stationOrderList",
  components: {Template},
  props: {
    stationId: "",
  },
  dicts: ['order_status', 'start_mode', 'pay_mode', 'pay_status'],
  data() {
    return {
      dateDescription: '',
      sumOrderAmount:'',
      sumUsedElectricity:'',
      // 遮罩
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedOrderDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 订单详情表格数据
      orderDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        mobileNumber: null,
        orderStatus: null,
        stationId: this.stationId,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 站点列表
      stationList: [],
      // 订单创建时间范围
      createTimeRange: [],
    };
  },
  created() {
    // console.log('stationOrderList 站点id：', this.stationId);
    // this.dataLoading();
  },
  methods: {
    async dataLoading() {
      console.log("加载站点订单数据...")
      // 设置默认日期
      await this.defaultDate();
      // 查询订单列表
      this.getList();
      // 查询站点列表
      this.getStationList();
      // 获取订单总金额
      this.getOrderTotalData();
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        console.log('order里面的参数', this.orderList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取订单总金额数据
    getOrderTotalData() {
      totalData(this.queryParams).then(response => {
        console.log('getOrderTotalData', response);
        this.dateDescription = response.data.dateDescription;
        this.sumOrderAmount = response.data.sumOrderAmount;
        this.sumUsedElectricity = response.data.sumUsedElectricity;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCode: null,
        orderStatus: "0",
        memberId: null,
        nickName: null,
        mobileNumber: null,
        stationId: null,
        connectorCode: null,
        startMode: null,
        payMode: null,
        payStatus: null,
        payAmount: null,
        payTime: null,
        orderAmount: null,
        startSoc: null,
        endSoc: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.orderDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      // 获取订单列表
      this.getList();
      // 获取订单总金额
      this.getOrderTotalData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 订单详情序号 */
    rowOrderDetailIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },

    /** 复选框选中数据 */
    handleOrderDetailSelectionChange(selection) {
      this.checkedOrderDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    /** 查询充电站信息列表 */
    getStationList() {
      const queryStationParams = {
        pageNum: 1,
        pageSize: 999
      };
      listStation(queryStationParams).then((response) => {
        console.log("订单列表页-查询站点列表", response)
        this.stationList = response.rows;
      });
    },

    //设置默认日期
    defaultDate () {
      console.log(getDay(-7));
      //字符串拼接，开始时间，结束时间
      let beg = getDay(-7) + " 00:00:00";   //当月第一天
      let end = getDay(0) + " 23:59:59"; //当天
      this.createTimeRange = [beg, end] //将值设置给插件绑定的数据
      // console.log("defaultDate", this.createTimeRange);
    },
  },
  watch: {
    createTimeRange(newValue, oldValue) {
      if (newValue != null && newValue.length > 0) {
        this.queryParams.startTime = newValue[0];
        this.queryParams.endTime = newValue[1];
      }
    },
  }
};
</script>
