<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
      <el-form-item label="优惠券类别" >
        <el-input v-model="queryParams.name" clearable @keyup.enter.native="handleQuery"></el-input>        
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable @keyup.enter.native="handleQuery">
          <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" v-hasPermi="['coupon:category:query']">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['coupon:category:add']">新增</el-button>
      </el-col>
   </el-row>

    <el-table v-loading="loading" :data="couponTypeList">     
      <el-table-column label="类别" align="center" prop="name">       
        <template slot-scope="scope">
          <a @click="handleUpdate(scope.row)" style="color: blue">{{ scope.row.name }}</a>
        </template>     
      </el-table-column>
      <el-table-column label="折扣形式" align="center" prop="typeDesc" />
      <el-table-column label="优惠券数量" align="center" prop="couponCnt" />
      <el-table-column label="通用简述" align="center" prop="desc" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1">生效中</span>
          <span v-if="scope.row.state == 2">未生效</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-show="scope.row.state == 1" size="mini" type="text" icon="el-icon-edit" @click="handleStatus(scope.row,'1')"
            v-hasPermi="['coupon:category:off']">下线</el-button>
          <el-button v-show="scope.row.state == 2" size="mini" type="text" icon="el-icon-edit" @click="handleStatus(scope.row,'2')"
            v-hasPermi="['coupon:category:online']">上线</el-button> 
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['coupon:category:edit']">编辑</el-button>   
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['coupon:category:remove']">删除</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" />
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类别名" prop="name">
          <el-input v-model="form.name" placeholder="请输入类别名" minlength="1" maxlength="7"/>
        </el-form-item>
        <el-form-item label="类别" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio :label="1">抵扣</el-radio>
            <el-radio :label="2">折扣</el-radio>
            <el-radio :label="3">免单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="通用简述" prop="desc">
          <el-input v-model="form.desc" type="textarea" placeholder="请输入内容" maxlength="17"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-hasPermi="['coupon:category:edit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCouCategory,updateCouCategory,getCouCategoryList,offlineCouCategory,onlineCouCategory,delCouCategory
  
} from "@/api/coupon/couponType";

export default {
  name: "Class",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      couponTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        state: null
      },
      // 表单参数
      form: {
        id: null,
        name: null,
        type: null,
        desc : null,
        state:null
      },
      // 表单校验
      rules: {
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        type: [{ required: true, message: "不能为空", trigger: "blur" }],
        desc: [{ required: true, message: "不能为空", trigger: "blur" }]
      },
      options: [{
        value: null,
        label: '全部'
      },{
        value: '1',
        label: '生效中'
      },{
        value: '2',
        label: '未生效'
      }],
      nameList:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getCouCategoryList(this.queryParams).then((response) => {
        this.couponTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        desc : null,       
      };     
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.name=null;
      this.queryParams.state=null;
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "新增类别";
    },
    /** 上线、下线按钮操作 */
    handleStatus(row,tag) {
      let mess="";    
      if(tag=='1'){
        mess="下线";      
      }else if(tag=='2'){
        mess="上线";        
      }
      this.$modal
        .confirm('是否确认' + mess + '该数据项？')
        .then(function () {
          if(tag=="1"){
            return offlineCouCategory(row.id);
          }else if(tag=="2"){
            return onlineCouCategory(row.id);
          }
        }).then((response) => {
          if(response.code="200"){
            this.getList();
            this.$modal.msgSuccess('' + mess + '成功');
          }          
        })
        .catch(() => { });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      let obj=Object.assign({},row);
      this.form.id=obj.id;
      this.form.name=obj.name;
      this.form.type=obj.type;
      this.form.desc=obj.desc;
      this.open = true;
      this.title = "修改类别";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            if(this.form.state == "1"){
              this.$message.error("修改类别需先下架类别!");
              return;
            }
            updateCouCategory(this.form).then((response) => {
              if(response.code=="200"){
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCouCategory(this.form).then((response) => {
              if(response.code=="200"){
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if(row.state=="1"){
        this.$message.error("未生效状态才能删除!");
        return;
      }
      const ids = row.id;
      this.$modal
        .confirm('是否确定删除?')
        .then(function () {
          return delCouCategory(ids);
        })
        .then((response) => {
          if(response.code=="200"){
            this.getList();
            this.$modal.msgSuccess("删除成功");
          }
        })
        .catch(() => { });
    },
  },
};
</script>