<template>
  <div class="app-container">
    <el-form ref="queryForm" size="small" :inline="true" label-width="94px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-circle-plus-outline" size="mini" @click="handleAdd">添加优惠券</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="index" width="50" label="序号" align="center">
      </el-table-column>
      <el-table-column prop="couponType" label="券类型" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          {{ scope.row.couponType | filtersList }}
        </template>
      </el-table-column>
      <el-table-column prop="fullSubtraction" label="优惠内容" :show-overflow-tooltip="true" align="center">
      </el-table-column>
      <el-table-column prop="minUsed" label="使用门槛" :show-overflow-tooltip="true" align="center">
      </el-table-column>
      <el-table-column prop="effectiveTime" label="生效时间" :show-overflow-tooltip="true" align="center">
      </el-table-column>

      <el-table-column prop="failureTime" label="失效时间" :show-overflow-tooltip="true" align="center">
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="modify(scope.row, '1')">修改</el-button>
          <el-button type="text" size="small" @click="clickDel(scope.row)">删除</el-button>
          <el-button type="text" size="small" @click="modify(scope.row, '2')">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 弹出框 -->
    <dialogFile :dialogVisible="dialogVisible" :disableds="disableds" :disabled="disabled" :title="title" @cancle="handleCancleDialog" @onSubmit="handleSave"
      :isEdit="isEdit" ref="refDialogFile"></dialogFile>
  </div>
</template>

<script>
import dialogFile from "./components/detailFile.vue";
import { couponGET, couponPOST, coupontGET,couponlePUT } from '@/api/orderform/index'

export default {
  name: "orderform",
  components: { dialogFile },
  data() {
    return {
      dialogVisible: false,
      control: "0",
      total: 0,
      title: "添加优惠券",
      queryParams: {
        pageNum: 0,
        pageSize: 10,
      },
      disabled:false,
      disableds:false,
      value: "",
      tableData: [],
      isEdit: true,//判断添加修改详情的保存按钮是否显示
    };
  },
  filters:{
    filtersList(val) {
      switch (val) {
        case '0':
          return '满减卷'
        case '1':
          return '新用户'
      }
    }
  },
  created() {
    this.coupon()
  },
  methods: {
    coupon() {
      couponGET({ ...this.queryParams }).then((res) => {
        this.tableData = res.rows
        this.total = res.total
      })
    },
    // 页码切换
    getList() {
      this.coupon()
    },
    handleAdd() {
      let that = this;
      that.title = "添加优惠券";
      this.$refs['refDialogFile'].form = {}
      this.disabled = false
      this.disableds = false
      that.isEdit = true
      that.dialogVisible = true;
    },
    clickDel(val) {
      coupontGET(val.id).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.coupon()
        }
      })
    },
    // 修改详情
    modify(sta, type) {
      let that = this;
      if (type == "1") {
        that.title = "修改优惠券";
        this.$refs['refDialogFile'].couponts(sta.id)
        this.disabled = false
        this.disableds = true
        that.isEdit = true
      } else if (type == "2") {
        this.$refs['refDialogFile'].couponts(sta.id)
        this.disabled = true
        this.disableds = true
        that.title = "查看优惠券";
        that.isEdit = false
      }
      that.dialogVisible = true;
    },
    //保存
    handleSave(e) {
      console.log('表单值', e)
      if (this.title == "添加优惠券") {
        couponPOST({ ...e }).then((res) => {
          if (res.code == 200) {
            this.$refs['refDialogFile'].form.couponType = ''
            this.$refs['refDialogFile'].form.fullSubtraction = ''
            this.$refs['refDialogFile'].form.minUsed = ''
            this.$refs['refDialogFile'].form.effectiveTime = ''
            this.$refs['refDialogFile'].form.failureTime = ''
            this.dialogVisible = false;
            this.coupon()
          }
        })
      }else if(this.title == "修改优惠券"){
        couponlePUT({ ...e }).then((res)=>{
          if (res.code == 200) {
            this.$refs['refDialogFile'].form.couponType = ''
            this.$refs['refDialogFile'].form.fullSubtraction = ''
            this.$refs['refDialogFile'].form.minUsed = ''
            this.$refs['refDialogFile'].form.effectiveTime = ''
            this.$refs['refDialogFile'].form.failureTime = ''
            this.dialogVisible = false;
            this.coupon()
          }
        })
      }

    },
    // 其他 弹窗关闭
    handleCancleDialog() {
      this.$refs['refDialogFile'].form.couponType = ''
      this.$refs['refDialogFile'].form.fullSubtraction = ''
      this.$refs['refDialogFile'].form.minUsed = ''
      this.$refs['refDialogFile'].form.effectiveTime = ''
      this.$refs['refDialogFile'].form.failureTime = ''
      this.dialogVisible = false;
    },
  },
};
</script>
+