<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams"  :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="用户信息" prop="userInfo">
          <el-input v-model="queryParams.userInfo" placeholder="请输入姓名、手机号" clearable style="width: 240px" @keyup.enter.native="handleQuery"/>
        </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-select v-model="queryParams.buyRecords" placeholder="请选择">
            <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">搜索</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="customerList"> 
          <el-table-column label="用户" align="center"  prop="userName">
          <template slot-scope="scope">
            <a @click="getDetial(scope.row.id)" style="color: blue">{{ scope.row.userName }}</a>
          </template>
          </el-table-column>
          <el-table-column label="手机号" align="center" prop="phoneNumber"/>
          <el-table-column label="订单量" align="center" prop="orderCount"/>
          <el-table-column label="总金额" align="center" prop="amount"/>
          <el-table-column label="已取消" align="center" prop="cancelCount"/>
          <el-table-column label="取消金额" align="center" prop="cancelAmount"/>
          <el-table-column label="已退款" align="center" prop="refundCount"/>
          <el-table-column label="退款金额" align="center"  prop="refundAmount"/>   
          <el-table-column label="最近订单时间" align="center"  prop="lastOrderDate"/> 
          <el-table-column label="注册时间" align="center"  prop="createDate"/>      
          <el-table-column label="来源" align="center">
            <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>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改人员配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" 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>姓名</span>
          </div>
          <div>
            <span>电话</span>
          </div>
        </div>
        <div style="margin-left: 50px;">
          <div style="margin-bottom: 20px;">消费</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" :loading="loading"></order-table>      
        </el-tab-pane>
        <el-tab-pane label="取消订单" name="second">
          <order-table :orderList="orderList" :orderTotal="orderTotal" :loading="loading"></order-table> 
        </el-tab-pane>
        <el-tab-pane label="退款订单" name="third">
         <order-table :orderList="orderList" :orderTotal="orderTotal" :loading="loading"></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 {listCustomer,getCustomer} from "@/api/system/customer";
import OrderTable from "./orderTable.vue";
export default {
  name: "Customer",  
  components: { OrderTable },  
  data() {
    return {
      img:"/static/img/profile.473f5971.jpg",
      createdTime:[],
      // 遮罩层
      loading: true,    
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员表格数据
      customerList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,      
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userInfo:null,
        createAtStart: null,
        createAtEnd: null,
        buyRecords: null
      },
      options:[
        {value:"",label:"全部"},
        {value:"1",label:"有购买"},
        {value:"2",label:"无购买"},
        {value:"3",label:"有复购"},
      ],
      activeName:"first" ,
      customerId:null,
      orderList:[],
      orderTotal:0
      
    };
  },
  created() {
    this.getList();   
  },
  methods: {
    handleClick(){
      let tag="";
      if(this.activeName=="first"){
        tag="1";
      }else if(this.activeName=="second"){
        tag="2";
      }else if(this.activeName=="third"){
        tag="3";
      }
      this.getOrderList(this.customerId,tag);
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then((response) => {
        if(response.code=="200"){
          this.customerList = response.rows;
          this.total = response.total;
          this.loading = false;
        }        
      });
    },
    getDetial(id){
      this.customerId=id;
      this.open=true;
      this.title="用户信息";
      this.getOrderList(this.customerId,'1');
    },
    getOrderList(id,tag) {
      this.loading = true;
      getCustomer({id:id,status:tag}).then((response) => {
        if(response.code=="200"){
          this.orderList = response.rows;
          this.orderTotal = response.total;
          this.loading = false;
        }        
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(this.createdTime.length>0){
        this.queryParams.createAtStart=this.createdTime[0];
        this.queryParams.createAtEnd=this.createdTime[1];
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.userInfo=null;
      this.queryParams.buyRecords=null;
      this.createdTime=[];
      this.handleQuery();
    },


    
  },
};
</script>
