<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="加油枪号" prop="oilGun">
        <el-input
          v-model="queryParams.oilGun"
          placeholder="请输入加油枪号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="油品名称" prop="oilName">
        <el-input
          v-model="queryParams.oilName"
          placeholder="请输入油品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="用户id(消费者)" prop="consumerId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.consumerId"-->
      <!--          placeholder="请输入用户id(消费者)"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="消费者" prop="consumer">
        <el-input
          v-model="queryParams.consumer"
          placeholder="请输入消费者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="金额" prop="amt">
        <el-input
          v-model="queryParams.amt"
          placeholder="请输入金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="油站id" prop="stationId">
        <el-input
          v-model="queryParams.stationId"
          placeholder="请输入油站id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in payStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="加油升数" prop="orderLiters">
        <el-input
          v-model="queryParams.orderLiters"
          placeholder="请输入加油升数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付类型" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付类型" clearable size="small">
          <el-option
            v-for="dict in payTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="payWay">
        <el-select v-model="queryParams.payWay" placeholder="请选择支付类型" clearable size="small">
          <el-option
            v-for="dict in payWayOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="加油员" prop="oilPersonnel">
        <el-input
          v-model="queryParams.oilPersonnel"
          placeholder="请输入加油员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间" prop="createdDate">
        <el-date-picker style="width: 350px"
          v-model="dateRangeCreatedDate" size="mini" value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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="primary"-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['station:order:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['station:order:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['station:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['station: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">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column v-if="false" label="订单id" align="center" prop="orderId" />
      <el-table-column label="订单号"  width="200" align="center" prop="orderNo" />
      <el-table-column label="加油枪号" align="center" prop="oilGun" />
      <el-table-column label="油品名称" align="center" prop="oilName" />
      <el-table-column v-if="false" label="用户id(消费者)" align="center" prop="consumerId" />
      <el-table-column label="消费者" width="120" align="center" prop="consumer" />
      <el-table-column label="金额" align="center" prop="amt" />
      <el-table-column label="加油升数" align="center" prop="orderLiters" />
      <el-table-column v-if="false" label="油站id" align="center" prop="stationId" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFotmat"/>
      <el-table-column label="支付类型" align="center" prop="payType" :formatter="payTypeFotmat"/>
      <el-table-column label="支付方式" align="center" prop="payWay" :formatter="payWayFotmat"/>
      <el-table-column label="订单类型" align="center" prop="orderType" />
      <el-table-column label="加油员" align="center" prop="oilPersonnel" />
      <el-table-column label="创建时间" align="center" prop="createdDate" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payDate" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单类型" align="center" prop="orderType" v-if="false" />
      <el-table-column label="操作" align="center" width="150" 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="['station:order:edit']"-->
          <!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['station:order: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="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="加油枪号" prop="oilGun">
          <el-input v-model="form.oilGun" placeholder="请输入加油枪号" />
        </el-form-item>
        <el-form-item label="油品名称" prop="oilName">
          <el-input v-model="form.oilName" placeholder="请输入油品名称" />
        </el-form-item>
        <el-form-item label="用户id(消费者)" prop="consumerId">
          <el-input v-model="form.consumerId" placeholder="请输入用户id(消费者)" />
        </el-form-item>
        <el-form-item label="消费者" prop="consumer">
          <el-input v-model="form.consumer" placeholder="请输入消费者" />
        </el-form-item>
        <el-form-item label="金额" prop="amt">
          <el-input v-model="form.amt" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="油站id" prop="stationId">
          <el-input v-model="form.stationId" placeholder="请输入油站id" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
          <el-select v-model="form.status" placeholder="请选择支付类型：随行付：sxf">
            <el-option
              v-for="dict in payStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="加油升数" prop="orderLiters">
          <el-input v-model="form.orderLiters" placeholder="请输入加油升数" />
        </el-form-item>
        <el-form-item label="支付类型" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择支付类型：随行付：sxf">
            <el-option
              v-for="dict in payTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="支付方式" prop="payWay">
          <el-select v-model="form.payWay" multiple placeholder="请选择">
            <el-option
              v-for="dict in payWayOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="支付时间" prop="payDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.payDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="加油员" prop="oilPersonnel">
          <el-input v-model="form.oilPersonnel" placeholder="请输入加油员" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.createdDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单类型" prop="orderType">
          <el-select v-model="form.orderType" placeholder="请选择订单类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </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, exportOrder } from "@/api/station/order";

  export default {
    name: "Order",
    data() {
      return {

        //创建订单时间间隔
        dateRangeCreatedDate:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 订单支付表格数据
        orderList: [],
        //字典
        payTypeOptions:[],
        payWayOptions:[],
        payStatusOptions:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderNo: null,
          oilGun: null,
          oilName: null,
          consumerId: null,
          consumer: null,
          amt: null,
          stationId: null,
          status: null,
          orderLiters: null,
          payType: null,
          payWay: null,
          // pay:[],
          // cre:[],
          payDate: null,
          payMaxDate: null,
          payMinDate: null,
          createdMaxDate: null,
          createdMinDate: null,
          oilPersonnel: null,
          createdDate: null,
          orderType: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
      //字典
      this.getDicts("pay_type").then(response => {
        this.payTypeOptions = response.data;
      });
      this.getDicts("pay_way").then(response => {
        this.payWayOptions = response.data;
      });
      this.getDicts("pay_status").then(response => {
        this.payStatusOptions = response.data;
      });
    },
    methods: {
      payTypeFotmat(row, column){
        if(row.payType === 'sxf'){
          return '随行付'
        }
      },
      payWayFotmat(row, column){
        if(row.payWay === '02'){
          return '公众号'
        }else if(row.payWay === '03'){
          return '小程序'
        }
      },
      //statusFotmat
      statusFotmat(row, column){
        if(row.status === '0'){
          return '未支付'
        }else if(row.status === '1'){
          return '已支付'
        }
      },
      //orderTypeFotmat
      // orderTypeFotmat(row, column){
      //   if(row.orderType === '1'){
      //     return '柴油'
      //   }else if(row.orderType === '2'){
      //     return '汽油'
      //   }
      // },

      /** 查询订单支付列表 */
      getList() {
        this.loading = true;
        listOrder(this.addDateRange(this.queryParams, this.dateRangeCreatedDate)).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,
          oilGun: null,
          oilName: null,
          consumerId: null,
          consumer: null,
          amt: null,
          stationId: null,
          status: "0",
          orderLiters: null,
          payType: null,
          payWay: null,
          payDate: null,
          oilPersonnel: null,
          createdDate: null,
          orderType: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRangeCreatedDate =[];
        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.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addOrder(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const orderIds = row.orderId || this.ids;
        this.$confirm('是否确认删除订单支付编号为"' + orderIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(orderIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有订单支付数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
