<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px">
      <el-form-item label="渠道入口">
        <el-input v-model="queryParams.source" placeholder="请输入渠道入口" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="优惠券">
        <el-select v-model="queryParams.couponId" placeholder="请选择优惠券" clearable @keyup.enter.native="handleQuery" filterable>
          <el-option
            v-for="item in couponList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in options"
            :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="['coupon:user:list']">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>   

    <el-table v-loading="loading" :data="couponInfoList">     
      <el-table-column label="用户名" align="center">       
        <template slot-scope="scope">
          <a  myattr="mcv" v-hasPermi="['system:customer:view']">
            <span @click="getDetial(scope.row)" style="color: blue"> {{ scope.row.custName }}</span>
          </a>
        </template>     
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="用户手机号" align="center" prop="custPhone" />
      <el-table-column label="领取优惠券名称" align="center" prop="couponName" />
      <el-table-column label="领取入口" align="center" prop="source"/>     
      <el-table-column label="触发事件" align="center" prop="typeDesc"/> 
      <el-table-column label="领取时间" align="center" prop="receiveTime"/> 
      <el-table-column label="优惠券适用时间" align="center" >
        <template slot-scope="scope">
          {{formateTime(scope.row.startTime)}}~{{formateTime(scope.row.expiredTime)}}
        </template>
      </el-table-column> 
      <el-table-column label="优惠券状态" align="center" prop="stateDesc"/> 
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 用户详情页 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <div style="display: flex;align-items: center;">
        <div>
          <img v-bind:src="img" class="img-circle img-lg" />
        </div>
        <div style="margin-left: 20px;">
          <div style="margin-bottom: 20px;">
            <span>{{userName}}</span>
          </div>
          <div>
            <span>{{phoneNumber}}</span>
          </div>
        </div>
        <div style="margin-left: 50px;">
          <div style="margin-bottom: 20px;">共{{orderCount}}单,消费{{amount}}元</div>
          <div>地址</div>
        </div> 
      </div>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="正常订单" name="first">
          <order-table :orderList="orderList" :orderTotal="orderTotal" :orderLoading="orderLoading" @getOrderList="getOrderList"></order-table>      
        </el-tab-pane>
        <el-tab-pane label="取消订单" name="second">
          <order-table :orderList="orderList" :orderTotal="orderTotal" :orderLoading="orderLoading" @getOrderList="getOrderList"></order-table> 
        </el-tab-pane>
        <el-tab-pane label="退款订单" name="third">
         <order-table :orderList="orderList" :orderTotal="orderTotal" :orderLoading="orderLoading" @getOrderList="getOrderList"></order-table>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCoupon,getCouponInfoList,getCusOrderNum} from "@/api/coupon/coupon";
import {getCustomer} from "@/api/system/customer";
import OrderTable from "../../customer/orderTable.vue";
export default {
  components: { OrderTable },  
  data() { 
    return {
      img:"/static/img/profile.473f5971.jpg",
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      couponInfoList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        source:"",
        state:null,
        couponId:null
      },
      options: [{
        value: null,
        label: '全部'
      },{
        value: '1',
        label: '有效'
      },{
        value: '2',
        label: '被使用'
      },{
        value: '3',
        label: '过期失效'
      }],
      activeName:"first" ,
      title: "",
      userName:"",
      phoneNumber:"",
      orderCount:"",
      amount:"",
      orderLoading: true,
      orderList:[],
      orderTotal:0,
      orderPageNum:1,
      orderPageSize: 10,
      customerId:null,
      couponList:[]
    };
  },
  created() {
    this.getList();
    this.getCouponList();
  },
  methods: {
    formateTime(val){
      let index =val.indexOf(":");
      val=val.substring(0, index-3);
      return val;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      getCouponInfoList(this.queryParams.pageNum,this.queryParams.pageSize,
      {source:this.queryParams.source,state:this.queryParams.state,couponId:this.queryParams.couponId}).then((response) => {
        this.couponInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.name=null;
      this.queryParams.state=null;
      this.handleQuery();
    },
    getDetial(row){
      this.orderLoading=true;
      this.customerId=row.custId;
      this.userName=row.custName;
      this.phoneNumber=row.custPhone;
      this.open=true;
      this.title="用户信息";
      this.orderPageNum=1;
      this.pageSize=10;
      this.activeName="first";
      this.getCusOrderNum(row.custId);
      this.getOrderList(this.orderPageNum,this.pageSize,this.customerId,'1');
    },
    handleClick(){
      if(this.activeName=="first"){
        this.tag="1";
      }else if(this.activeName=="second"){
        this.tag="2";
      }else if(this.activeName=="third"){
        this.tag="3";
      }
      this.orderPageNum=1;
      this.pageSize=10;
      this.orderLoading=true;
      this.getOrderList(this.orderPageNum,this.pageSize,this.customerId,this.tag);
    },
    getOrderList(pageNum,pageSize,id,tag) {
      getCustomer({'id':id,'status':tag,'pageNum':pageNum,'pageSize':pageSize}).then((response) => {
        if(response.code=="200"){
          this.orderList = response.rows;
          this.orderTotal = response.total;
          this.orderLoading=false;
        }        
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 查询优惠券列表 */
    getCouponList() {      
      listCoupon(1,10000,{state: 1}).then((response) => {
        if(response.code==200){
          this.couponList = response.rows;          
        }
      });
    },
    //获取用户总订单量、总金额
    getCusOrderNum(custId){
      getCusOrderNum(custId).then((response) => {
        if(response.code==200){
          this.amount=response.data.orderAmount;
          this.orderCount=response.data.orderCount;
        }
      });
    }
  },
};
</script>