<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="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付单号" label-width="100px" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入支付单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker clearable
          v-model="queryParams.payTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付方式" clearable>
          <el-option
            v-for="dict in dict.type.ss_pay_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.ss_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange" :default-sort="defaultSort"  @sort-change="onSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="column of showColumns">
        <el-table-column
          :key="column.key"
          :label="column.label"
          :prop="column.key"
          :align="column.align"
          :min-width="column.minWidth"
          :sort-orders="orderSorts"
          :sortable="column.sortable"
          :show-overflow-tooltip="column.overflow"
          :width="column.width"
        >
          <template slot-scope="d">
            <template v-if="column.key === 'id'">
              {{d.row[column.key]}}
            </template>
            <template v-else-if="column.key === 'payTime'">
              <span>{{ parseTime(d.row.payTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
            <template v-else-if="column.key === 'payType'">
              <dict-tag :options="dict.type.ss_pay_type" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'paid'">
              <dict-tag :options="dict.type.et_order_pay_status" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'type'">
              <dict-tag :options="dict.type.ss_order_type" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'totalFee' || column.key === 'payFee' || column.key === 'handlingCharge' || column.key === 'platformServiceFee'">
              {{d.row[column.key] | dv}} 元
            </template>
            <template v-else-if="column.key === 'duration'">
              {{d.row[column.key] | dv}} 小时
            </template>
            <template v-else-if="column.key === 'status'">
              <dict-tag :options="dict.type.ss_order_status" :value="d.row[column.key]"/>
            </template>
            <template v-else>
              {{d.row[column.key]}}
            </template>
          </template>
        </el-table-column>
      </template>
    </el-table>
<!--    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">-->
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="订单号" width="150" align="center" prop="orderNo" />-->
<!--      <el-table-column label="用户" align="center" prop="userName" />-->
<!--      <el-table-column label="支付单号" width="150" align="center" prop="outTradeNo" />-->
<!--&lt;!&ndash;      <el-table-column label="手机号" align="center" prop="phone" />&ndash;&gt;-->
<!--      <el-table-column label="支付时间" align="center" prop="payTime" width="90">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="支付方式" align="center" prop="payType">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.ss_pay_type" :value="scope.row.payType"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="支付状态" align="center" prop="paid">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.et_order_pay_status" :value="scope.row.paid"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="类型" align="center" prop="type">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.ss_order_type" :value="scope.row.type"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="订单总金额" align="center" prop="totalFee" />-->
<!--      <el-table-column label="实际支付金额" align="center" prop="payFee" />-->
<!--      <el-table-column label="订单时长" align="center" prop="duration" />-->
<!--      <el-table-column label="状态" align="center" prop="status">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.ss_order_status" :value="scope.row.status"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="手续费" align="center" prop="handlingCharge" />-->
<!--      <el-table-column label="服务费" align="center" prop="platformServiceFee" />-->
<!--      <el-table-column label="支付渠道" align="center" prop="channelName" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--        </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="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="支付单号" prop="outTradeNo">
          <el-input v-model="form.outTradeNo" placeholder="请输入支付单号" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="计费规则id" prop="ruleId">
          <el-input v-model="form.ruleId" placeholder="请输入计费规则id" />
        </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="支付方式" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择支付方式">
            <el-option
              v-for="dict in dict.type.ss_pay_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付状态" prop="paid">
          <el-input v-model="form.paid" placeholder="请输入支付状态" />
        </el-form-item>
        <el-form-item label="订单总金额" prop="totalFee">
          <el-input v-model="form.totalFee" placeholder="请输入订单总金额" />
        </el-form-item>
        <el-form-item label="实际支付金额" prop="payFee">
          <el-input v-model="form.payFee" placeholder="请输入实际支付金额" />
        </el-form-item>
        <el-form-item label="备注" prop="mark">
          <el-input v-model="form.mark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="订单时长" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入订单时长" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.ss_order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手续费" prop="handlingCharge">
          <el-input v-model="form.handlingCharge" placeholder="请输入手续费" />
        </el-form-item>
        <el-form-item label="服务费" prop="platformServiceFee">
          <el-input v-model="form.platformServiceFee" placeholder="请输入服务费" />
        </el-form-item>
        <el-form-item label="运营商分账" prop="operatorDividend">
          <el-input v-model="form.operatorDividend" placeholder="请输入运营商分账" />
        </el-form-item>
        <el-form-item label="支付渠道id" prop="payChannel">
          <el-input v-model="form.payChannel" placeholder="请输入支付渠道id" />
        </el-form-item>
        <el-form-item label="店铺id" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入店铺id" />
        </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 { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/system/order";
import { $showColumns } from '@/utils/mixins';
import {parseTime} from "../../../utils/ruoyi";

// 默认排序字段
const defaultSort = {
  prop: "createTime",
  order: "descending"
}
export default {
  name: "Order",
  mixins: [$showColumns],
  props: ['storeId'],
  dicts: ['ss_order_status', 'ss_pay_type', 'rl_distribution_mode', 'rl_rental_unit','ss_order_type','et_order_pay_status'],
  data() {
    return {
      // 字段列表
      columns: [
        {key: 'orderNo', visible: true, label: '订单号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'userName', visible: true, label: '用户', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'outTradeNo', visible: true, label: '支付单号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'payTime', visible: true, label: '支付时间', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'payType', visible: true, label: '支付方式', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'paid', visible: true, label: '支付状态', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'type', visible: true, label: '类型', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'totalFee', visible: true, label: '订单总金额', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'payFee', visible: true, label: '实际支付金额', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'duration', visible: true, label: '订单时长', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'status', visible: true, label: '状态', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'handlingCharge', visible: true, label: '手续费', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'platformServiceFee', visible: true, label: '服务费', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'channelName', visible: true, label: '支付渠道', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        orderNo: null,
        outTradeNo: null,
        userId: null,
        ruleId: null,
        payTime: null,
        payType: null,
        paid: null,
        type: null,
        totalFee: null,
        payFee: null,
        dispatchFee: null,
        ridingFee: null,
        mark: null,
        duration: null,
        status: null,
        returnTime: null,
        depositDeduction: null,
        deductionAmount: null,
        autoRefundDeposit: null,
        rentalUnit: null,
        handlingCharge: null,
        platformServiceFee: null,
        operatorDividend: null,
        payChannel: null,
        deliveryMethod: null,
        pickupTime: null,
        agentId: null,
        storeId: null,
        pickupCity: null,
        pickupLoc: null,
        pickupLon: null,
        pickupLat: null,
        rentalPeriod: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        paid: [
          { required: true, message: "支付状态不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 当排序按钮被点击时触发 **/
    onSortChange(column) {
      if (column.order == null) {
        this.queryParams.orderByColumn = defaultSort.prop;
        this.queryParams.isAsc = defaultSort.order;
      } else {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
      }
      this.getList();
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.storeId = this.storeId;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderNo: null,
        outTradeNo: null,
        userId: null,
        ruleId: null,
        deviceMac: null,
        payTime: null,
        payType: null,
        paid: null,
        type: null,
        totalFee: null,
        payFee: null,
        mark: null,
        duration: null,
        status: null,
        createTime: null,
        depositDeduction: null,
        depositOrderNo: null,
        deductionAmount: null,
        changeReason: null,
        autoRefundDeposit: null,
        handlingCharge: null,
        platformServiceFee: null,
        operatorDividend: null,
        payChannel: null,
        storeId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
