<!--油品订单-->

<template>
  <div>
    <el-card class="box-card">
<!--      <div style="display: flex;">-->
<!--        <div class="top-app-sou">-->
          <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="终端" prop="terminal">
              <el-select
                v-model="queryParams.terminal"
                clearable
                placeholder="全部"
              >
                <el-option v-for="item in terminalList"
                           :key="item.id"
                           :label="item.dictLabel"
                           :value="item.dictValue">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="支付类型" prop="payType">
              <el-select
                v-model="queryParams.payType"
                clearable
                placeholder="全部"
              >
                <el-option v-for="item in payList"
                           :key="item.id"
                           :label="item.dictLabel"
                           :value="item.dictValue">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="手机号">
              <el-input v-model="queryParams.payUser" placeholder="请输入手机号"></el-input>
            </el-form-item>
            <el-form-item label="油枪号" prop="oilGunNum">
              <el-select
                v-model="queryParams.oilGunNum"
                filterable
                clearable
                placeholder="全部"
              >
                <el-option v-for="item in oilNumList"
                           :key="item.id"
                           :label="item.oilGunNum"
                           :value="item.id">
                  <span style="float: left">{{ getName(oilNameList,item.oilName) }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.oilGunNum }}号枪</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>
<!--        </div>-->
<!--        <div style="margin-left: 10px">-->
<!--          <el-button type="primary" icon="el-icon-search">搜索</el-button>-->
<!--        </div>-->
<!--      </div>-->
    </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.numberOfOilPens}}</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.theTotalAmountOfTheOffer}}</div>
        </div>
        <div class="box">
          <div class="size-hui">交易数量</div>
          <div class="size-bole">{{orderStatistics.numberOfTransactions}}</div>
        </div>
        <div class="box">
          <div class="size-hui">实收总额</div>
          <div class="size-bole">{{orderStatistics.totalAmountReceived}}</div>
        </div>
        <div class="box">
          <div class="size-hui">微信</div>
          <div class="size-bole">{{orderStatistics.wechat}}</div>
        </div>
        <div class="box">
          <div class="size-hui">支付宝</div>
          <div class="size-bole">{{orderStatistics.alipay}}</div>
        </div>
        <div class="box">
          <div class="size-hui">现金</div>
          <div class="size-bole">{{orderStatistics.cash}}</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">新增会员</el-button>-->
<!--        </div>-->
      </div>
      <div class="table-box">
        <el-table
          :data="oilOrderList"
          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>{{ props.row.userId ? queryUser(props.row.userId):"--" }}</span>
                </el-form-item>
                <el-form-item label="收银员">
                  <span>{{ queryStaf(staffList,props.row.staffId) }}</span>
                </el-form-item>
                <el-form-item label="来源终端">
                  <span>{{ getVal(terminalList,props.row.terminal) }}</span>
                </el-form-item>
                <el-form-item label="订单备注">
                  <span>{{ props.row.remark ? props.row.remark:"--" }}</span>
                  <div style="color: grey;font-size: 12px">订单备注为系统后台操作备注信息,非用户提交信息</div>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column prop="terminal" label="终端" align="center" width="80">
            <template slot-scope="props">
              <span>{{ getVal(terminalList,props.row.terminal) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="orderNo"
            label="订单号"
            width="200">
          </el-table-column>
          <el-table-column label="订单信息" align="center">
            <el-table-column
              prop="oilGunNum"
              label="油品/油枪" align="center"
              width="120">
              <template slot-scope="props">
                <span>{{ getName(oilNameList,props.row.oils) }}/{{ props.row.oilGunNum }}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="orderAmount"
              label="订单金额" align="center"
              width="120">
            </el-table-column>
            <el-table-column
              prop="discountAmount"
              label="优惠金额" align="center"
              width="120">
            </el-table-column>

          </el-table-column>
          <el-table-column label="付款信息" align="center">
            <el-table-column
              prop="payAmount"
              label="实付金额" align="center"
              width="120">
            </el-table-column>
            <el-table-column
              prop="payUser"
              label="付款用户" align="center"
              width="120">
            </el-table-column>
            <el-table-column
              prop="payType"
              label="付款方式" align="center"
              width="120">
              <template slot-scope="scope">
                <span>{{getType(payList,scope.row.payType)}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="orderStatus"
              label="付款状态" align="center"
              width="120">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.orderStatus == 'unpaid'">未支付</el-tag>
                <el-tag type="success" v-else-if="scope.row.orderStatus == 'paid'">已支付</el-tag>
                <el-tag type="danger" v-else>支付失败</el-tag>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column
            prop="invoicing"
            label="开票标识" align="center"
            width="220">
          </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" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <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>
      </div>
    </el-card>

<!--    订单退款-->
    <el-dialog
      title="订单退款"
      width="25%"
      :visible.sync="dialogRefund">
      <div style="text-align: center;font-weight: bold">
        ￥<span style="font-size: 35px">{{ oilOrder.payAmount }}</span>
      </div>
      <div class="tk">退款金额</div>
      <div class="tk">退款单号：{{ oilOrder.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 {queryStaffs} from "@/api/cashier/staff";
  import {getDicts} from "@/api/dict/data";
  import {getOilNameList, getOilNumGun } from "@/api/cashier/oilnumgun";
  import {listOilOrder, oilOrderInfo, orderStatisticsApi} from "@/api/cashier/oilorder";
  import {getUser} from "@/api/cashier/user/user";

  export default {
    name: "order_Cashier",
    data(){
      return{
        oilOrder:{},
        // 退款密码
        password:"",
        radio1:"油号选错",
        dialogRefund:false,
        // 员工列表
        staffList:[],
        // 终端列表
        terminalList:[],
        // 支付类型列表
        payList:[],
        // 油号列表
        oilNameList:[],
        // 油枪列表
        oilNumList:[],
        // 日期范围
        dateRange: [],
        // 油品订单列表
        oilOrderList:[],
        payTypeList:[],
        // 查询参数
        queryParams: {
          page: 1,
          pageSize: 10,
        },
        total:0,

        orderStatistics: {
          numberOfOilPens:'0',
          theTotalAmountOfOil:'0',
          theTotalAmountOfTheOffer:'0',
          numberOfTransactions:'0',
          totalAmountReceived:'0',
          wechat:'0',
          alipay:'0',
          cash:'0',
        }
      }
    },
    created() {
      this.getStaffList();
      this.getOrderStatistics();
      this.getList();
      this.getPayType();
    },
    methods:{
      getPayType(){
        getDicts("payment_type").then(res => {
          this.payTypeList = res.data
        })

      },
      // 订单统计
      getOrderStatistics() {
        orderStatisticsApi().then(res=>{
          this.orderStatistics = res.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;
        oilOrderInfo(id).then( response => {
          this.oilOrder = response.data
        })
      },
      // 根据员工id查询员工信息
      queryStaf(list,id){
        let name = "";
        list.forEach(item => {
          if (item.id == id){
            name = `${item.realName}(${item.mobile})`
          }
        })
        return name;
      },
      getType(list,val){
        let name = "";
        list.forEach(item => {
          if (item.dictValue == val){
            name = item.dictLabel
          }
        })
        return name;
      },
      // 根据id查询会员信息
      async queryUser(id){
        let result = "";
        if (id!=null && id!=""){
          let data = await getUser(id);
          result = data.data.name + "|" + data.data.mobile
        }
        return result.toString();
      },
      getVal(list,val){
        let name = "";
        list.forEach(item => {
          if (item.dictValue == val){
            name = item.dictLabel
          }
        })
        return name;
      },
      // 获取订单列表信息
      getList(val){
        if (val!=undefined){
          this.queryParams.page = val
        }
        listOilOrder(this.addDateRange(this.queryParams, this.dateRange)).then( response => {
          this.oilOrderList = 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,
        }
        // this.resetForm("queryForm");
        this.handleQuery();
      },
      // 获取列表信息
      getStaffList(){
        queryStaffs().then( response => {
          this.staffList = response.data;
        })
        getDicts("terminal").then( response => {
          this.terminalList = response.data;
        })
        getDicts("payment_type").then( response => {
          this.payList = response.data;
        })
        getOilNameList().then( response => {
          this.oilNameList = response.data;
        })
        getOilNumGun({oilNum : "全部"}).then( response => {
          this.oilNumList = response.data
        })
      },
      // 获取油号名称
      getName(oilNameList,id){
        let name = ""
        let _this = this;
        if(oilNameList!=null && oilNameList!=""){
          oilNameList.forEach(item => {
            if (item.id == id){
              name = item.oilName;
              _this.oilType = item.oilType;
            }
          })
        }
        return name;
      },
    }
  }
</script>

<style scoped>
  .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>
