
<template>
  <div class="couponManage app-container">
    <div class="search">
      <div>
        <el-input
          v-model="query.shopNameOrPhoneNum"
          size="small"
          placeholder="请输入优惠券名称"
          class="item-width-200"
        ></el-input>
         <el-date-picker
          v-model="date"
          size="small"
          value-format="timestamp"
          class="item-width-350 ml10"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
        <el-select
          v-model="query.shopMenuId"
          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.state"
          size="small"
          class="item-width-200 ml10"
          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>

      <el-button
        class="add-btn"
        type="primary"
        size="small"
        @click="handleAdd(0)"
      >
        添加
      </el-button>
  
    </div>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="优惠券名称"
        prop="shopName"
        show-overflow-tooltip
      >
      </el-table-column>
      
      <el-table-column
        prop="shopMenuId"
        align="center"
        label="优惠券类型"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column
        prop="shopMainName"
        align="center"
        label="优惠内容"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="shopId"
        align="center"
        label="发放数量"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="applyTime"
        align="center"
        label="已领取"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="applyTime"
        align="center"
        label="剩余数量"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="applyTime"
        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="state"
        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)"
            >查看</el-button
          >
           <el-button
            type="text"
            size="small"
            @click="handleDetail(scope.row)"
            >编辑</el-button
          >
        
           <el-button type="text" size="small" @click="handle(scope.row)">{{
            scope.row.state == 1 ? "停止" : "启用"
          }}</el-button>
          <el-button type="text" size="small" @click="handleDel(scope.row.shopId)"
            >删除</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 { getList, updateState } from "@/api/shop";
import {getMenuName} from '@/api/appConfig';
import axios from 'axios'
const BASE_URL = require('@/utils/config')
export default {
  name: "couponManage",
  data() {
    return {
      tableData: [],
      options: [],
      options1: [
        {
          value: null,
          label: "全部状态",
        },
        {
          value: "1",
          label: "启用",
        },
        {
          value: "4",
          label: "禁用",
        },
      ],
      loading: false,
      query: {
        state: null,
        province: "",
        startTime:'',
        endTime:'',
        currentPage: 1,
        pageSize: 10,
      },
      total: 0,
    };
  },
  watch: {
    date(val){
      if(val){
        this.query.startTime = val[0]
        this.query.endTime = val[1]
      }else{
        this.query.startTime = ''
        this.query.endTime = ''
      }
    }
  },
  methods: {
    handleSearch(){
      this.query.currentPage = 1
      this.getList();

    },
    handleAdd(val) {
      this.$router.push({
        path: "/operationManage/couponManage/shopAdd" + (val || ""),
      });
    },
    handle(row) {
      let msg;
      if (row.state == 1) {
        msg = "禁用";
      } else {
        msg = "启用";
      }
      this.$confirm(`此操作将${msg}该商铺, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        updateState({
          shopId: row.shopId,
          state: row.state == 1 ? 4 : 1,
        }).then((res) => {
          if (res.state == "Success") {
            this.$notify({
              title: "成功",
              message: "操作成功",
              type: "success",
            });
            this.getList();
          }
        });
      });
    },
    handleEdit(id) {
      this.$router.push({
        path: "/operationManage/couponManage/shopAdd",
        query: {
          title: "店铺编辑",
          id,
        },
      });
    },
    handleDetail(id,title) {
      this.$router.push({
        path: "/operationManage/couponManage/shopAdd",
        query: {
          title: "店铺详情",
          id,
        },
      });
    },

    handleDel(shopId){
      this.$confirm(`此操作将删除该商铺, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        updateState({
          shopId,
          state: 3,
        }).then((res) => {
          if (res.state == "Success") {
            this.$notify({
              title: "成功",
              message: "操作成功",
              type: "success",
            });
            this.getList();
          }
        });
      });
    },

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

    getList() {
      this.loading = true;
      getList(this.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 {
  .search {
    justify-content: space-between;
    .add-btn {
      // margin-right: 20px;
    }
  }
}
</style>
  