<template>
  <div class="app-container">
    <el-dialog :visible.sync="giveOpen" width="1000px" append-to-body title="赠送优惠券">
      <el-form :model="queryParams" size="small" :inline="true" label-width="68px">
        <el-form-item label="手机号" >
          <el-input v-model="queryParams.userInfo" 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>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" v-hasPermi="['coupon:user:give:list']">搜索</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  myattr="mcv" v-hasPermi="['system:customer:view']">
              <span @click="getDetial(scope.row)" style="color: blue"> {{ scope.row.userName }}</span>
            </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"
      />
      <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 {listCustomer} from "@/api/system/customer";
import {giveCoupon,effective} from "@/api/coupon/coupon";
export default {
  name: "Class",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,   
      // 是否显示弹出层
      giveOpen: false,
      // 查询参数
      queryParams: {
        userInfo: null,
        couponId: null,
      },
      customerList:[],
      couponList:[]
    };
  },
  created() {
    //this.getCouponList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.giveOpen = true; 
      this.queryParams.userInfo=null;
      this.queryParams.couponId=null; 
      this.customerList=[];  
      this.total=0; 
      this.getCouponList();
    },
    /** 查询优惠券列表 */
    getCouponList() {     
      effective().then((response) => {
        if(response.code==200){
          this.couponList = response.rows;
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(this.queryParams.userInfo==null){
        this.$message.error("请输入手机号");
        return;
      }
      if(this.queryParams.userInfo.length!=11){
        this.$message.error("请输入11位手机号");
        return;
      }
      this.loading = true;
      listCustomer(this.queryParams).then((response) => {
        if(response.code=="200"){
          this.customerList = response.rows;
          this.total = response.total;
          this.loading = false;
        }        
      });
    },
    // 取消按钮
    cancel() {
      this.giveOpen = false;
    },
    /** 提交按钮 */
    submitForm() {
      let userId=null;
      if(this.customerList.length>0){
        userId=this.customerList[0].id;       
      }else{
        this.$message.error("请查询赠送优惠券的用户!");
        return;
      }
      let couponId=this.queryParams.couponId;
      if(couponId==null){
        this.$message.error("请选择需要赠送的优惠券!");
        return;
      }
      giveCoupon({"custId":userId,"couponId":couponId}).then((response) => {
        if(response.code==200){
          this.$modal.msgSuccess("赠送成功");
          this.giveOpen = false;
        }
        });
      }
    },
};
</script>

<style lang="scss" >



</style>
