
<template>
  <div class="couponManage app-container">

    <div class="type-box">
      <!-- <div class="type">
        <div class="title">满减券</div>
        <div class="desc">例：满100元减20元 </div>
        <div class="desc">便于合理控制活动成本</div>
         <el-button
          class="add-btn"
          type="primary"
          size="small"
          @click="handleAdd('/operationManage/couponManage/fullDiscountCoupon')"
        >
          立即添加
        </el-button>
      </div>

      <div class="type">
        <div class="title">折扣券</div>
        <div class="desc">例：满100元打8折</div>
        <div class="desc">便于合理控制活动成本</div>
         <el-button
          class="add-btn"
          type="primary"
          size="small"
          @click="handleAdd('/operationManage/couponManage/discountCoupon')"
        >
          立即添加
        </el-button>
      </div> -->

      <div class="type">
        <div class="title">小桔加油券</div>
        <div class="desc">小桔加油券</div>
        <div class="desc">丰富会员权益增加会员价值</div>
         <el-button
          class="add-btn"
          type="primary"
          size="small"
          @click="handleAdd('/operationManage/couponManage/refuelCoupon')"
        >
          立即添加
        </el-button>
      </div>

    </div>

    <div class="search">
      <div>
        <el-input
          v-model="query.couponName"
          size="small"
          placeholder="请输入优惠券名称"
          class="item-width-200"
        ></el-input>
         <el-date-picker
          v-model="date"
          size="small"
          :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd HH:mm:ss"
          class="item-width-350 ml10"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
        <!-- <el-select
          v-model="query.couponType"
          size="small"
          clearable
          class="item-width-200 ml10"
          placeholder="请选择类型"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select> -->
        <el-select
          v-model="query.status"
          size="small"
          class="item-width-200 ml10"
          clearable
          placeholder="请选择状态"
        >
          <el-option
            v-for="item in options1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-button
        class="ml10"
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="handleSearch"
      >
        搜索
      </el-button>
      </div>
    </div>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="优惠券名称"
        prop="couponName"
        show-overflow-tooltip
      >
      </el-table-column>
      
      <el-table-column
        prop="couponType"
        align="center"
        label="优惠券类型"
        show-overflow-tooltip
      >
       <template slot-scope="scope">
        {{scope.row.couponType|filterType}}
       </template>
      </el-table-column>
      <el-table-column
        prop="limitNum"
        align="center"
        label="发放数量"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="claimNum"
        align="center"
        label="已领取"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="surplusNum"
        align="center"
        label="剩余数量"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="useNum"
        align="center"
        label="已使用"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        align="center"
        label="创建时间"
        show-overflow-tooltip
      >
      </el-table-column>
     
      <el-table-column
        align="center"
        prop="status"
        label="状态"
        show-overflow-tooltip
      >
      </el-table-column>
     
      <el-table-column prop="address" width="180px" fixed="right" align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleDetail(scope.row,'detail')"
            >查看</el-button
          >
           <el-button
            type="text"
            size="small"
            @click="handleDetail(scope.row,'edit')"
            >编辑</el-button
          >
        
           <el-button type="text" size="small" v-if="scope.row.status == '进行中'" @click="handle(scope.row.id)">停止</el-button>
          <el-button type="text" size="small" v-else @click="handleDel(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="query.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
  
  <script>
import { debounce } from "@/utils/index";
import { getCouponList, delCoupon,stop } from "@/api/coupon";
export default {
  name: "couponManage",
  data() {
    return {
      tableData: [],
      options: [
        {
          value: "1",
          label: "满减券",
        },
        {
          value: "2",
          label: "打折券",
        },
        {
          value: "3",
          label: "加油券",
        },
      ],
      options1: [
        {
          value: "1",
          label: "未开始",
        },
        {
          value: "2",
          label: "进行中",
        },
         {
          value: "3",
          label: "已停止",
        },
      ],
      loading: false,
      query: {
        couponName:'',
        couponType: 3,
        status:'',
        startTime:'',
        endTime:'',
        currentPage: 1,
        pageSize: 10,
      },
      total: 0,
      date:[]
    };
  },
  watch: {
    date(val){
      if(val){
        this.query.startTime = val[0]
        this.query.endTime = val[1]
      }else{
        this.query.startTime = ''
        this.query.endTime = ''
      }
    }
  },
  filters: {
    filterType: function(value) {
      if(value == 1){
        return '满减券';
      }else if(value == 2){
        return '打折券';
      }else if(value == 3){
        return '加油券';
      }
    }
  },
  methods: {
    handleSearch(){
      this.query.currentPage = 1
      this.getList();

    },
    handleAdd(path) {
      this.$router.push({
        path: path,
      });
    },
    handle(id) {
      this.$confirm(`此操作将停止该优惠券活动, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        stop({
          id
        }).then((res) => {
          if (res.state == "Success") {
            this.$notify({
              title: "成功",
              message: "操作成功",
              type: "success",
            });
            this.handleSearch();
          }
        });
      });
    },
    handleDetail(row,mode) {
      let url
      if(row.couponType == 1){
        url = 'fullDiscountCoupon'
      }else if(row.couponType == 2){
        url = 'discountCoupon'
      }else if(row.couponType == 3){
        url = 'refuelCoupon'
      }
      this.$router.push({
        path: "/operationManage/couponManage/"+url,
        query: {
          id:row.id,
          mode,
          status:row.status
        },
      });
    },

    handleDel(id){
      this.$confirm(`此操作将删除该优惠券活动, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delCoupon({
          id
        }).then((res) => {
          if (res.state == "Success") {
            this.$notify({
              title: "成功",
              message: "操作成功",
              type: "success",
            });
            this.handleSearch();
          }
        });
      });
    },

    handleSizeChange(val) {
      this.query.currentPage = 1;
      this.query.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getList();
    },

    getList() {
      this.loading = true;
      let query = JSON.parse(JSON.stringify(this.query))
      if(!query.status){
        delete query.status
      }
      if(!query.couponType){
        delete query.couponType
      }
      
      getCouponList(query).then((res) => {
        this.loading = false;
        if (res.state == "Success") {
          this.tableData = res.content.records;
          this.total = res.content.total;
        }
      });
    },
  },
  created() {
    this.getList();
  },
};
</script>
  
<style lang="scss" scoped>
.couponManage {
  .type-box{
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    .type{
      display: flex;
      flex-direction: column;
      align-items: center;
      background: rgba(244, 248, 253, 1);
      border-radius: 8px;
      padding: 20px;
      width: 250px;
      .title{
        font-size: 20px;
        color: #222;
        font-weight: bold;
        margin-bottom: 10px;
      }
        .desc{
          color: #aaa;
          font-size: 14px;
        }
        .add-btn{
          margin-top: 10px;
        }
    }
    .type+.type{
      margin-left: 20px;
    }
  }
  .search {
    justify-content: space-between;
    .add-btn {
      // margin-right: 20px;
    }
  }
}
</style>
  