<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="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=='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(['3','4','5','6','13'], scope.row.state)"
            @click="refund(scope.row.id)">退款</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="订单信息" :visible.sync="open" width="1000px" append-to-body>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="订单信息" name="first">
          <el-form ref="form" :model="form" label-width="80px">
            <el-row>
              <el-col :span="8">
                <el-form-item label="订单编号" >
                  <el-input v-model="form.orderNo" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="顺序号" >
                  <el-input v-model="form.orderNum" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="订单状态" >
                  <el-input v-model="form.state" disabled/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="城市" >
                  <el-input v-model="form.city" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="店铺" >
                  <el-input v-model="form.shopName" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="创建时间" >
                  <el-input v-model="form.createdAt" disabled/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="商品数量" >
                  <el-input v-model="form.goodsNum" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="金额" >
                  <el-input v-model="form.amount" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="付款时间" >
                  <el-input v-model="form.payTime" disabled/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="用户昵称" >
                  <el-input v-model="form.userName" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="用户电话" >
                  <el-input disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="完成时间" >
                  <el-input v-model="form.finishTime" disabled/>
                </el-form-item>
              </el-col>
            </el-row>           
            <el-table :data="goodsList">
              <el-table-column label="商品名称" align="center" prop="goodsName" />
              <el-table-column label="缩略图" align="center">
                <template slot-scope="scope">              
                  <div class="image-tmp">
                    <el-image style="width: 100px; height: 100px"  :src="getPicsList(scope.row.pics)" :preview-src-list="getPicsBigList(scope.row.pics)">
                      <div slot="error" class="image-slot"></div>            
                    </el-image>
                  </div>    
                </template>
              </el-table-column>
              <el-table-column label="商品分类" align="center" prop="categoryName" />
              <el-table-column label="原售价" align="center" prop="price" />
              <el-table-column label="折扣价" align="center" prop="discount" />
              <el-table-column label="实付" align="center" prop="realAmount" />
              <el-table-column label="数量" align="center" prop="num" />              
            </el-table>  
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="订单日志" name="second">
          <el-table  :data="logList">
            <el-table-column label="状态" align="center" prop="status" />              
            <el-table-column label="操作" align="center" prop="operation" />
            <el-table-column label="时间" align="center" prop="createAt" />
            <el-table-column label="操作人" align="center" prop="operationUser" />
            <el-table-column label="内容" align="center" prop="content" />        
            </el-table>
        </el-tab-pane>
      </el-tabs>
      <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,getLogList } from "@/api/system/order";
import { listShop} from "@/api/system/shop";
import piovince from '@/utils/piovince';
export default {
  name: "Order",
  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,
      // 订单表格数据
      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: {},
      shopName: null,
      city:null,
      activeName:"first",
      goodsList:[],
      logList:[] ,
      orderId:null  
 
    };
  },
  created() {
    this.getList();
    this.getShopList();
  },
  methods: {
    getProvince(province){
      let list=this.piovince;
      for(let i=0;i<list.length;i++){
        if(list[i].value == province){
          return list[i].name;
        }        
      }
    },
    //退款
    refund(id){

    },
    getPicsList(str){   
      let picObj = JSON.parse(str);  
      return picObj.thumbnail
    },
    getPicsBigList(str){
      let list=[];
      let picObj = JSON.parse(str);  
      list.push(picObj.thumbnail)
      return list;
    },
    handleClick(tab) {
      if(tab.name=="first"){
        this.getOrderDetail(this.orderId);
      }else if(tab.name=="second"){
        this.getLogList();
      }
    },
    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() {
      listOrder(this.queryParams).then(response => {
        if(response.code=="200"){
          this.orderList = response.rows;
          this.total = response.total;
        }
      });
    },
    /** 查询订单日志列表 */
    getLogList() {
      //this.loading = true;
      getLogList({orderId:this.orderId}).then(response => {
        if(response.code=="200"){
          this.logList = response.data; 
        }
      });
    },
    // 取消按钮
    cancel() {
      this.activeName="first";
      this.open = false;    
    },
    /** 提交按钮 */
    submitForm() {
      this.open=false;
      this.activeName="first"     
    },
    /** 搜索按钮操作 */
    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();
    },
    /** 详情按钮操作 */
    handleUpdate(row) {
      this.goodsList=[];
      this.orderId= row.id 
      this.getOrderDetail(this.orderId);
    },
    getOrderDetail(id){
      getOrder(id).then(response => {
        if(response.code=="200"){
          this.form = response.data;
          this.form.shopName=response.data.shop.name;
          this.form.state=this.formatterState(response.data.state);  
          this.form.city=this.getProvince(response.data.shop.city);        
          if(response.data.orderDetails.length>0){
            let list=response.data.orderDetails;
            for(let i=0;i<list.length;i++){
             this.goodsList.push({"goodsName":list[i].goods.name,"pics":list[i].goods.pics,
             "categoryName":list[i].goods.categoryName,"price":list[i].goods.price,"discount":list[i].goods.discount,
             "realAmount":list[i].realAmount,"num":list[i].num})
            }
          }         
          this.open = true;
        }        
      });
    },
    /** 店铺列表 */
    getShopList() {
      listShop({pageNum:1,pageSize:10000}).then(response => {
        if(response.code=="200"){
          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},)
            }
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
