<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" v-hasPermi="['system:order:query']">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>   
    <el-table :data="orderList">
      <el-table-column label="订单编号" align="center">
         <template slot-scope="scope">
          <a @click="handleUpdate(scope.row)" style="color: blue">{{ scope.row.orderNo }}</a>
        </template>
      </el-table-column>
      <el-table-column label="顺序号" align="center" prop="orderNum" />
      <el-table-column label="商品数量" align="center" prop="goodsNum" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="城市" align="center" >
        <template slot-scope="scope">
          {{getProvince(scope.row.shop.city)}}
        </template>
      </el-table-column>
      <el-table-column label="店铺" align="center" prop="shop.name" />
      <el-table-column label="订单来源" align="center" prop="source">
        <template slot-scope="scope">
          <span v-if="scope.row.source=='1'">点单屏A</span>
          <span v-if="scope.row.source=='2'">点单屏B</span>
          <span v-if="scope.row.source=='3'">小程序</span>
          <span v-if="scope.row.source=='12'">点单屏</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="创建时间" align="center" prop="createdAt"/>
      <el-table-column label="付款时间" align="center" prop="payTime"/>  
      <el-table-column label="订单状态" align="center" prop="state">
        <template slot-scope="scope">{{formatterState(scope.row.state)}}</template>
      </el-table-column>      
      <el-table-column width="200" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
           <el-button size="mini" type="text" v-if="authBtn(['2','3','4','5','6','7','13'], scope.row.state)"
            @click="refundClick(scope.row)"  v-hasPermi="['system:order:refund']">退款</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" />
    <!-- 详情对话框 -->
    <OrderDetial ref="orderDetial" @getList="getList"></OrderDetial> 
      
  </div>
</template>

<script>
import { listOrder} from "@/api/system/order";
import { listShop} from "@/api/system/shop";
import OrderDetial from '@/components/order/orderDetial.vue';
import piovince from '@/utils/piovince';
export default {
  name: "Order",  
  components: { OrderDetial},
  data() {  
    return {
      createdTime:[],
      payTime:[],   
      shopList:[
        {value: '',label: '全部'},
      ],
      stateList:[
        {value:null,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:"14",label:"部分退款"},
        {value:"15",label:"已退款"},
        {value:"50",label:"其他人工干预状态"},
      ],  
      piovince,
      // 遮罩层
      //loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      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();
    this.getShopList();
  },
  methods: {
    getProvince(province){
      let list=this.piovince;
      for(let i=0;i<list.length;i++){
        if(list[i].children.length>0){
          let childrenList=list[i].children;
          for(let j=0;j<childrenList.length;j++){
            if(childrenList[j].value == province){
              return childrenList[j].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 == 14){
        return "部分退款（部分退款）";  
      }else if(state == 15){
        return "已退款（已退款）";  
      }else if(state == 50){
        return "其他人工干预状态（其他人工干预状态）";  
      }
    },
    /** 查询订单列表 */
    getList() {
      listOrder(this.queryParams).then(response => {
        if(response.code=="200"){
          this.orderList = response.rows;
          this.total = response.total;
        }
      });
    }, 
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.createAtStart=null;
      this.queryParams.createAtEnd=null;
      this.queryParams.payAtStart=null;
      this.queryParams.payAtEnd=null;
      if(this.createdTime!=null && this.createdTime.length>0 ){
        this.queryParams.createAtStart=this.createdTime[0];
        this.queryParams.createAtEnd=this.createdTime[1];
      }
      if(this.payTime!=null && 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.createAtStart=null,
      this.queryParams.createAtEnd=null,
      this.queryParams.payAtStart=null,
      this.queryParams.payAtEnd= null,
      this.queryParams.state=null;   
      this.handleQuery();
    },
    /** 详情按钮操作 */
    handleUpdate(row) {
      this.$refs.orderDetial.handleUpdate(row);
    }, 
    refundClick(row){
      this.$refs.orderDetial.refundClick(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>
