<template>
  <div class="userStyle">
    <div class="formSearch">
      <el-form :inline="true" :model="querySelect">
        <el-form-item label="ID">
          <el-input v-model="querySelect.voucherId" maxlength="20" placeholder="请输入代金卷ID" />
        </el-form-item>
        <el-form-item label="名字">
          <el-input v-model="querySelect.voucherName" maxlength="20" placeholder="请输入代金卷名字" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="search">查询</el-button>
          <el-button type="info" plain @click="clear">重置</el-button>
          <el-button type="success" plain @click="add">新增代金卷</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="tableBox">
      <el-table
        ref="multipleTable" :data="dataList" border
        :header-cell-style="{ background: '#EEF3FF', color: '#333333' }" tooltip-effect="dark" style="width: 100%"
      >
        <el-table-column label="代金卷ID" width="220">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column prop="voucherName" label="代金卷名称" />
        <el-table-column prop="desc" label="描述" />
        <el-table-column label="是否开启" width="220">
          <template slot-scope="scope">{{ scope.row.enabled?'开启':'关闭' }}</template>
        </el-table-column>
        <el-table-column prop="updateTime" label="创建时间" />
        <el-table-column label="操作" show-overflow-tooltip>
          <template slot-scope="scope">
            <div class="btnList">
              <el-button type="text" @click="edit(scope.row)">编辑</el-button>
              <el-button type="text" @click="del(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye">
        <el-pagination
          :current-page="querySelect.page" :page-sizes="[10, 20, 50, 100]" :page-size="querySelect.pageSize"
          layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="sizeChange"
          @current-change="search"
        />
      </div>
    </div>
    <!-- 新增角色 -->
    <el-dialog
      :title="userState ? '新增代金卷' : '修改代金卷'" :visible.sync="addFormDialog" width="30%" center
      :close-on-click-modal="false"
    >
      <!-- 新增角色/修改角色的对话框 -->
      <div>
        <el-form ref="ruleForm" :model="createdForm" label-width="100px" :rules="createdFormRules">
          <el-form-item label="代金卷描述" prop="desc">
            <el-input v-model="createdForm.desc" maxlength="20" placeholder="请输入代金卷描述" />
          </el-form-item>
          <el-form-item label="代金卷名称" prop="voucherName">
            <el-input v-model="createdForm.voucherName" maxlength="60" placeholder="请输入代金卷名称" />
          </el-form-item>
          <el-form-item label="金卷启用状态">
            <el-radio-group v-model="createdForm.enabled">
              <el-radio :label="true">开启</el-radio>
              <el-radio :label="false">关闭</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addFormDialog = false">取 消</el-button>
        <el-button type="primary" @click="voucherDP">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getVoucherList, addVoucherList, setVoucherList, deleteVoucherList } from '@/api/marketingManagement/voucher'
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
export default {
  // eslint-disable-next-line vue/match-component-file-name
  name: 'Shabixuruili',
  // import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    // 这里存放数据
    return {
      dataList: [],
      addFormDialog: false,
      total: 0,
      querySelect: {
        page: 1,
        pageSize: 10,
        voucherId: '',
        voucherName: ''
      },
      userState: true, // 默认为创建代金卷
      createdForm: {
        desc: '', // 代金卷描述
        ratio: 2, // 代金卷比例
        voucherName: '', // 代金卷名称
        enabled: true // 是否启用
      },
      createdFormRules: {
        voucherName: [ { required: true, message: '请输入代金卷名称', trigger: 'blur' } ],
        desc: [ { required: true, message: '请输入代金卷描述', trigger: 'blur' } ]
      }
    }
  },
  created() {
    this.search()
  },
  methods: {
    search() {
      getVoucherList(this.querySelect).then((res) => {
        this.dataList = res.data.list
        this.total = res.data.total
      })
    },
    sizeChange(value) {
      this.querySelect.pageSize = value
      this.search()
    },
    clear() {
      this.querySelect = {
        voucherId: '',
        voucherName: '',
        page: 1, // 当前页
        pageSize: 10 // 每页记录数
      }
      this.search()
    },
    add() {
      this.userState = true // 将modal打开 并且切换为创建模式
      this.addFormDialog = true
    },
    del(rowData) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(() => {
          deleteVoucherList({ voucherId: rowData.id}).then(res => {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.search()
          })
      })
      .catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    edit(rowData) {
      this.userState = false // 将modal打开 并且切换为修改模式
      this.addFormDialog = true
      this.createdForm = {
        desc: rowData.desc, // 代金卷描述
        enabled: rowData.enabled, // 是否启用
        voucherName: rowData.voucherName, // 代金卷名称
        ratio: 2
      }
    },
    voucherDP() {
      this.$refs.ruleForm.validate().then((volid) => {
        if (this.userState) {  // 如果通过，则走创建 // 否则为修改
          addVoucherList(this.createdForm).then((res) => {
            if (res.code === '') {
                this.$message({
                  message: '新增成功',
                  type: 'success'
                })
              }
             this.search()
             this.addFormDialog = false
          })
        } else {
          setVoucherList(this.createdForm).then((res) => {
            if (res.code === '') {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                })
              }
            this.createdForm = {
              desc: '', // 代金卷描述
              ratio: 2, // 代金卷比例
              voucherName: '', // 代金卷名称
              enabled: true // 是否启用
            }
            this.search()
            this.addFormDialog = false
          })
        }
      })
        .catch((err) => {
          console.log('..')
        })
    }
  }
}
</script>

<style lang='scss'>
//@import url(); 引入公共css类
@import url("../../../styles/elDialog.scss");

.userStyle {
	padding: 20px;
}
</style>
