<template>
  <div class="app-container">
    <el-form :model="queryParams" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" >
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" style="width:230px"/>
      </el-form-item>
      <el-form-item label="顺序号" >
        <el-input v-model="queryParams.orderNum" placeholder="请输入顺序号" clearable @keyup.enter.native="handleQuery" style="width:230px"/>
      </el-form-item>
      <el-form-item label="用户昵称">
        <el-input v-model="queryParams.userName" placeholder="请输入用户昵称" clearable @keyup.enter.native="handleQuery" style="width:230px"/>
      </el-form-item>
      <el-form-item label="店铺" >
        <el-select v-model="queryParams.shopId" placeholder="请选择店铺" clearable style="width:230px">
          <el-option
            v-for="item in shopList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" >
        <el-date-picker v-model="createdTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" style="width:230px"></el-date-picker>        
      </el-form-item>
      <el-form-item label="付款时间">
        <el-date-picker v-model="payTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" style="width:230px"></el-date-picker>
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select v-model="queryParams.state" placeholder="请选择订单状态" clearable style="width:230px">
          <el-option v-for="item in stateList"  :key="item.value" :label="item.label" :value="item.value"></el-option>
        </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-table :data="refundList">
      <el-table-column label="退款编号" align="center">
        <template slot-scope="scope">
          <a @click="handleDetial(scope.row)" style="color: blue">{{ scope.row.refundNo }}</a>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="orderNo"/>
      <el-table-column label="商品数量" align="center" prop="order.goodsNum" />
      <el-table-column label="金额" align="center" prop="refundAmount" />
      <el-table-column label="店铺" align="center" prop="order.shop.name" />      
      <el-table-column label="用户名称" align="center" prop="order.userName" />
      <el-table-column label="付款时间" align="center" prop="order.payTime"/>  
      <el-table-column label="订单状态" align="center" >
        <template slot-scope="scope">{{formatterState(scope.row.order.state)}}</template>
      </el-table-column> 
      <el-table-column label="申请退款时间" align="center" prop="createdAt"/>
      <el-table-column label="到账时间" align="center" prop="updatedAt"/>
      <el-table-column label="退款状态" align="center" prop="state">
        <template scope="scope">
          <span v-if="scope.row.state=='1'">退款中</span>
          <span v-if="scope.row.state=='2'">退款成功</span>
          <span v-if="scope.row.state=='3'">退款失败</span>
        </template>
      </el-table-column>
      <el-table-column label="发起人" align="center" prop="createUserName"/>   
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 详情对话框 -->
    <RefundDetial ref="refundDetial" ></RefundDetial>

  </div>
</template>

<script>
import { listRefund } from "@/api/system/refund";
import { listShop} from "@/api/system/shop";
import piovince from '@/utils/piovince';
import RefundDetial from '@/components/order/refundDetial.vue';
export default {
  name: "Order",  
  components: { RefundDetial},
  data() {
    return {
      createdTime:[],
      payTime:[],   
      shopList:[
        {value: '',label: '全部'},
      ],
      stateList:[
        {value:"",label:"创建未校验"},
        {value:"0",label:"全部"},
        {value:"1",label:"未支付"},
        {value:"2",label:"已支付"},
        {value:"3",label:"支付制作中"},
        {value:"4",label:"制作完成未取"},
        {value:"5",label:"取餐中"},
        {value:"6",label:"正常完成"},
        {value:"7",label:"未支付取消"},
        {value:"8",label:"支付后制作前取消"},
        {value:"9",label:"制作中取消"},
        {value:"10",label:"制作完成取消"},
        {value:"11",label:"已退款"},
        {value:"12",label:"部分退款"},
        {value:"13",label:"待取超时"},
        {value:"50",label:"其他人工干预状态"},
      ],  
      piovince,
      // 遮罩层
      //loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      refundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        orderNum: null,
        userName: null,
        shopId: null,
        createAtStart: null,
        createAtEnd:null,
        payAtStart:null,
        payAtEnd: null,
        state: null
      },     
      // 表单参数
      form: {}      
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getProvince(province){
      let list=this.piovince;
      for(let i=0;i<list.length;i++){
        if(list[i].value == province){
          return list[i].name;
        }        
      }
    },    
    formatterState(state){
      if(state == 0){
        return "创建未校验";
      }else if(state == 1){
        return "未支付";
      }else if(state == 2){
        return "已支付";
      }else if(state == 3){
        return "支付制作中";
      }else if(state == 4){
        return "制作完成未取";
      }else if(state == 5){
        return "取餐中";
      }else if(state == 6){
        return "正常完成";
      }else if(state == 7){
       return "未支付取消";
      }else if(state == 8){
       return "支付后制作前取消";
      }else if(state == 9){
        return "制作中取消";
      }else if(state == 10){
        return "制作完成取消";        
      }else if(state == 11){
        return "已退款";  
      }else if(state == 12){
        return "部分退款";  
      }else if(state == 13){
        return "待取超时";  
      }else if(state == 50){
        return "其他人工干预状态";  
      }
    },    
    /** 查询订单列表 */
    getList() {
      listRefund(this.queryParams).then(response => {
        if(response.code=="200"){
          this.refundList = response.rows;
          this.total = response.total;
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(this.createdTime.length>0){
        this.queryParams.createAtStart=this.createdTime[0];
        this.queryParams.createAtEnd=this.createdTime[1];
      }
      if(this.payTime.length>0){
        this.queryParams.payAtStart=this.payTime[0];
        this.queryParams.payAtEnd=this.payTime[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.pageNum = 1;   
      this.queryParams.orderNo=null;
      this.queryParams.orderNum=null;
      this.queryParams.userName=null;
      this.queryParams.shopId=null;
      this.createdTime=[];
      this.payTime=[];
      this.queryParams.state=null;   
      this.handleQuery();
    },
    /** 详情按钮操作 */
    handleDetial(row) {
      this.$refs.refundDetial.getDetial(row);
    },
    /** 店铺列表 */
    getShopList() {
      listShop({pageNum:1,pageSize:10000}).then(response => {
        if(response.code=="200"){
          this.shopList=[];
          if(response.rows.length>0){
            for(let i=0;i<response.rows.length;i++){              
              this.shopList.push({value:response.rows[i].id,label: response.rows[i].name},)
            }
          }
        }
      });
    }
    
  }
};
</script>
