<!--普通收款订单-->
<template>
  <div>
    <el-card class="box-card">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="交易员工" prop="staffId">
          <el-select
            v-model="queryParams.staffId"
            clearable
            placeholder="全部"
          >
            <el-option v-for="item in staffList" :key="item.id" :label="item.realName" :value="item.id">
              <span style="float: left">{{ item.realName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.mobile }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单号">
          <el-input v-model="queryParams.orderNo" placeholder="订单号、官方单号检索"></el-input>
        </el-form-item>
        <el-form-item label="交易时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            size="medium"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!--    统计-->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>小程序订单统计</span>
      </div>
      <div class="box-gang">
        <div class="box">
          <div class="size-hui">笔数</div>
          <div class="size-bole">{{ orderStatistics.numberOfStrokes }}</div>
        </div>
        <div class="box">
          <div class="size-hui">订单总金额</div>
          <div class="size-bole">{{ orderStatistics.theTotalAmountOfTheOrder }}</div>
        </div>
        <div class="box">
          <div class="size-hui">实付总金额</div>
          <div class="size-bole">{{ orderStatistics.theTotalAmountPaid }}</div>
        </div>
        <div class="box">
          <div class="size-hui">油品优惠</div>
          <div class="size-bole">{{ orderStatistics.oilDiscounts }}</div>
        </div>
        <div class="box">
          <div class="size-hui">油品总金额</div>
          <div class="size-bole">{{ orderStatistics.theTotalAmountOfOil }}</div>
        </div>
        <div class="box">
          <div class="size-hui">商品总金额</div>
          <div class="size-bole">{{ orderStatistics.theTotalAmountOfTheItem?orderStatistics.theTotalAmountOfTheItem:0 }}</div>
        </div>

      </div>
    </el-card>
    <el-card class="box-card">
      <div class="wgang">
        <div>小程序订单列表</div>
        <div style="display: flex ">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="exportExcelApplet()">导出订单</el-button>
        </div>
      </div>
      <div class="table-box">
        <el-table
          :data="list"
          style="width: 100%">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" class="demo-table-expand" style="margin-left: 20px">
                <el-form-item label="收银员">
                  <span>{{ getRealName(staffList,props.row.staffId) }}</span>
                </el-form-item>
                <el-form-item label="收银员">
                  <span>{{ getMobile(staffList,props.row.staffId) }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column
            prop="terminal"
            label="终端"
            width="100">
            <template slot-scope="scope">
              <span>{{getType(terminalList,scope.row.terminal)}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="orderNo"
            label="订单号"
            width="220">
          </el-table-column>
          <el-table-column
            prop="amount"
            label="订单金额">
          </el-table-column>
          <el-table-column
            prop="payAmount"
            label="实付金额">
          </el-table-column>
          <el-table-column label="订单金额组成">
            <el-table-column
              prop="oilOrderAmount"
              label="油品金额">
            </el-table-column>
            <el-table-column
              prop="goodsOrderAmount"
              label="商品金额">
            </el-table-column>
          </el-table-column>

          <el-table-column
            prop="payUser"
            label="付款用户">
          </el-table-column>

          <el-table-column
            prop="status"
            label="状态">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status == 'unpaid'">未支付</el-tag>
              <el-tag type="success" v-else-if="scope.row.status == 'paid'">已支付</el-tag>
              <el-tag type="danger" v-else>支付失败</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="交易时间" align="center" width="160" prop="payTime">
            <template slot-scope="scope">
              <span>{{ scope.row.payTime ? parseTime(scope.row.payTime):"--" }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <!--              <el-button style="width: 60px" size="mini"-->
              <!--                         @click="getOrdersInfo(scope.row.id)"-->
              <!--                         type="success" plain round>详情</el-button>-->
              <el-button style="width: 60px" size="mini"
                         @click="patchwork(scope.row)"
                         type="primary" plain round>补打</el-button>
              <!--              <el-button style="width: 60px" size="mini"-->
              <!--                         @click="handleRefund(scope.row.id)"-->
              <!--                         type="danger" plain round>退款</el-button>-->
            </template>
          </el-table-column>

        </el-table>
      </div>
      <div class="pagination-box">
        <el-pagination
          background
          v-show="total>0"
          layout="prev, pager, next"
          :total="total"
          :page.sync="queryParams.page"
          :limit.sync="queryParams.pageSize"
          @current-change="getList">
        </el-pagination>

        <!--        <pagination-->
        <!--          background-->
        <!--          v-show="total>0"-->
        <!--          layout="prev, pager, next"-->
        <!--          :total="total"-->
        <!--          :page.sync="queryParams.page"-->
        <!--          :limit.sync="queryParams.pageSize"-->
        <!--          @pagination="getList"-->
        <!--        />-->
      </div>


    </el-card>

    <!--    订单退款-->
    <el-dialog
      title="订单退款"
      width="25%"
      :visible.sync="dialogRefund">
      <div style="text-align: center;font-weight: bold">
        ￥<span style="font-size: 35px">{{ cashierOrder.amount }}</span>
      </div>
      <div class="tk">退款金额</div>
      <div class="tk">退款单号：{{ cashierOrder.orderNo }}</div>
      <div style="margin: 10px 0">
        退款原因<br/>
        <el-radio-group v-model="radio1" style="margin: 10px 0">
          <el-radio-button label="油号选错"></el-radio-button>
          <el-radio-button label="金额错误"></el-radio-button>
          <el-radio-button label="其他原因"></el-radio-button>
        </el-radio-group>
      </div>
      <div>
        <el-input placeholder="请输入密码" v-model="password" show-password></el-input>
        <br/>
        <span style="color: grey;font-size: 12px">退款仅支持全额退款,退款金额将按照支付信息原路退回、优惠券、储值卡等原路退回处理</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogRefund = false">取 消</el-button>
        <el-button type="primary" @click="dialogRefund = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {cashierOrder, listCashierOrder,orderStatisticsApi} from "@/api/order/cashierorder";
import {getDicts} from "@/api/order/data";
import {queryStaffs} from "@/api/order/staff";
import { exportExcelCashierApi } from "@/api/order/exportExcel";

  export default {
    name: "order_Cashier",
    data(){
      return{
        // 查询参数
        queryParams: {
          page: 1,
          pageSize: 10,
          terminal:"applet",
        },
        // 总条数
        total:0,
        // 列表信息
        list:[],
        dialogRefund:false,
        // 收银员订单详情
        cashierOrder:{},
        // 退款密码
        password:"",
        radio1:"油号选错",
        // 终端列表
        terminalList:[],
        // 员工列表
        staffList:[],
        // 日期范围
        dateRange: [],

        labelPosition: 'right',
        formLabelAlign: {
          name: '',
        },
        orderStatistics:{
          numberOfStrokes:'0',
          theTotalAmountOfTheOrder:'0',
          theTotalAmountPaid:'0',
          oilDiscounts:'0',
          theTotalAmountOfOil:'0',
          theTotalAmountOfTheItem:'0',
        },
      }
    },
    created() {
      this.getList();
      this.getOrderStatistics();

      this.getTerList();
    },
    methods:  {
      exportExcelApplet() {
        exportExcelCashierApi(this.addDateRange(this.queryParams, this.dateRange)).then(res=>{
          const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
          this.$download.saveAs(blob,'小程序订单统计.xLsx')
        })
      },
      // 获取订单统计信息
      getOrderStatistics() {
        orderStatisticsApi(this.addDateRange(this.queryParams, this.dateRange)).then(res=>{
          this.orderStatistics = res.data
        })
      },
      // 获取员工姓名
      getRealName(list,val){
        let name = "";
        list.forEach(item => {
          if (item.id == val){
            name = item.realName
          }
        })
        return name;
      },
      // 获取员工手机号
      getMobile(list,val){
        let name = "";
        list.forEach(item => {
          if (item.id == val){
            name = item.mobile
          }
        })
        return name;
      },
      getType(list,val){
        let name = "";
        list.forEach(item => {
          if (item.dictValue == val){
            name = item.dictLabel
          }
        })
        return name;
      },
      // 获取终端列表信息
      getTerList(){
        getDicts("terminal").then( response => {
          this.terminalList = response.data;
        })
        queryStaffs().then( response => {
          this.staffList = response.data;
        })
      },
      // 补打
      patchwork(data){
        const orderNo = data.orderNo
        this.$modal.confirm('确定您要补打当前订单吗？当前订单号[' + orderNo + '],请确保云打印机正在运行中').then(function() {
          // return delUser(row.id);
        }).then(() => {
          this.$modal.msgSuccess("打印指令已发送打印机,请查看打印结果");
        }).catch(() => {});
      },
      // 退款
      handleRefund(id){
        this.dialogRefund = true;
        cashierOrder(id).then( response => {
          this.cashierOrder = response.data
        })
      },
      // 获取列表信息
      getList(val){
        if (val!=undefined){
          this.queryParams.page = val
        }
        console.log("123123123",this.queryParams)
        listCashierOrder(this.addDateRange(this.queryParams, this.dateRange)).then( response => {
          this.list = response.data.records;
          this.total = response.data.total;
        })
      },
      // 搜索按钮操作
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
        this.getOrderStatistics();
      },
      // 重置按钮操作
      resetQuery() {
        this.dateRange = [];
        this.queryParams = {
          page: 1,
          pageSize: 10,
          terminal:"applet",
        }
        // this.resetForm("queryForm");
        this.handleQuery();
      },
    }
  }
</script>

<style scoped>
.app-container{
    width: 100%;
    height: 100%;
    background: #f6f8f9;
  }
  .app-top{
    width: 100%;

    height: 60px;
    box-sizing: border-box;
    padding: 10px;
  }
  .clearfix{
    width: 100%;

  }
  .box-card{
    width: 100%;
    margin-bottom: 15px;
  }
  .box-gang{
    width: 100%;
    display: flex;
    align-items: center;
  }
  .box{
    width: 200px;
    box-sizing: border-box;
    padding: 10px;
    background: #f9f9f9;
    height: 86px;
    margin-right: 10px;
  }
  .size-hui{
    font-size: 14px;
    margin-bottom: 10px;
    margin-top: 10px;
  }
  .size-bole{
    font-weight: bold;
  }
  .wgang{
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;

  }
  .table-box{
    width: 100%;
  }
  .pagination-box{
    width: 100%;
    margin: 10px auto;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .top-app-sou{
    width: 20%;
  }
  .tk{
    text-align: center;
    color: grey;
    margin: 10px 0;
  }

</style>
