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

    <div class="card-change" >
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="" prop="categoryName">
          <el-input
            v-model="queryParams.categoryName"
            placeholder="分类名称"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-select v-model="queryParams.status" style="width: 217px" placeholder="全部" clearable
>
            <el-option
            v-for="dict in dict.type.zhzt"
                :key="dict.label"
                :label="dict.label"
                :value="dict.label"
            />
        </el-select>
        </el-form-item>
        <el-form-item style="float: right;margin-right: 0px">
          <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
                          <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="categoryAdd"
            v-hasPermi="['integral:giftCategory:index:add']"
          >新增分类</el-button>
       </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">

        </el-col>
<!--            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
      </el-row>
      <el-table ref="tables"
                v-loading="loading"
                :data="dataList"
                border
                :default-sort="defaultSort">
        <!-- <el-table-column label="ID" align="center" prop="id" width="50px"/> -->
        <el-table-column
        label="序号"
          type="index"
          width="50">
        </el-table-column>
        <el-table-column label="分类名称" align="center" prop="categoryName"/>
        <el-table-column label="排序" align="center" prop="sort" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
              <el-tag v-if ="scope.row.status == '启用'" size="medium">启用</el-tag>
              <el-tag v-if ="scope.row.status == '禁用'" type="danger" size="medium">禁用</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['integral:giftCategory:index:update']"

            >修改</el-button>
          </template>
<!--          v-hasPermi="['']"-->
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
<!--   新增/修改-->
     <el-dialog :title="title" :visible.sync="open" width="25%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="dataForm" :rules="rules" label-width="80px">
            <el-form-item label="分类名称" prop="categoryName">
              <el-input v-model="dataForm.categoryName" placeholder="分类名称" maxlength="30"/>
            </el-form-item>

            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="dataForm.status">
                <el-radio
                  v-for="dict in dict.type.zhzt"
                  :key="dict.value"
                  :label="dict.label"
                  @change="handleIsopenSelect"
                  :value="dict.value"/>
                </el-radio-group>
            </el-form-item>
        <el-form-item label="备注" prop="categoryName">
          <el-input type="textarea" :rows="6" v-model="dataForm.remark" placeholder="分类名称" maxlength="30"/>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>

      </div>
    </el-dialog>

  </div>
</template>

<script>

import {getGiftCategoryApi,insertGiftCategoryApi,updateGiftCategoryApi  } from "@/api/integral/category";

export default {
  name: "giftCategory",
  dicts: ['zhzt'],

  data() {
    return {
      dataList: [],

      dataForm:{
        id: null,
        categoryName: null,
        sort: null,
        status: null
      },

      rules: {
        categoryName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" },
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      },
      // 弹出框标题
      title:'',
      // 显示搜索条件
      showSearch: true,
      disableInput: false, // 默认不禁用
      // 是否显示弹出层
      open: false,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        categoryName: '',
        status: '',
        page:null,
        pageSize:null
      },
      dateRange:{},
      // 遮罩层
      loading: false,
      // 默认排序
      // defaultSort: {prop: 'createTime', order: 'descending'},
      defaultSort: {prop: 'sort', order: 'ascending'},
    }
  },
  async created() {
    await this.getList();
  },
    methods: {
    getList() {
      this.loading = true;
        getGiftCategoryApi(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.dataList = response.data.records;
        this.total = response.data.total;
      })
      this.loading = false;
    },
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.dataForm.id == null) {
            // 判断是否是新增还是修改
            insertGiftCategoryApi(this.dataForm).then(res => {
            this.getList()
            this.open = false
          })
          } else {
            // 判断是否是新增还是修改
            updateGiftCategoryApi(this.dataForm).then(res => {
              this.getList()
              this.open = false
          })
        }
      }})
    },
    handleIsopenSelect() {
      this.$forceUpdate()
    },
    // 新增
    categoryAdd() {
      this.dataForm = {}
      this.dataForm.status = '启用'
      this.open = true
      this.title = '添加分类'
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    // 修改按钮
    handleUpdate(data) {
      this.dataForm = data
      this.open = true
      this.getList();
    },
    resetQuery(){
      this.queryParams = {}
      this.getList();
    }
  }
}
</script>

<style scoped lang="scss">
.card-change{
  height: 84vh;
}
</style>
