<template>
  <div class="app-container calendar-list-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input clearable class="filter-item" style="width: 200px;" placeholder="积分规则名称"
        v-model="listQuery.content"></el-input>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button class="filter-item" type="primary" @click="handleCreate" icon="el-icon-edit">添加</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit
      highlight-current-row>
      <el-table-column type="index" label="序号" header-align="center" align="center">
      </el-table-column>
      <el-table-column align="center" label="标识编码" prop="code">
      </el-table-column>
      <el-table-column align="center" label="规则类型" prop="type">
      </el-table-column>
      <el-table-column align="center" label="规则名称" prop="content">
      </el-table-column>
      <el-table-column align="center" label="过期单位" prop="dateField">
        <template slot-scope="props">
          <span v-if="props.row.dateField == 0">年</span>
          <span v-if="props.row.dateField == 1">月</span>
          <span v-if="props.row.dateField == 2">日</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="过期时间" prop="expire">
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template slot-scope="props">
          <el-tag type="success" v-if="props.row.expireFalg == 1">启用</el-tag>
          <el-tag type="danger" v-if="props.row.expireFalg == 0">关闭</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="240px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.expireFalg == 0" type="success" size="small"
            @click="changeState(scope.row.id, 1)">开启</el-button>
          <el-button v-if="scope.row.expireFalg == 1" type="warning" size="small"
            @click="changeState(scope.row.id, 0)">关闭</el-button>
          <!-- <el-button type="danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="listQuery.page" :page-sizes="[10, 20, 30, 50]" :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible"
      width="40%">
      <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px"
        style='width:700px; margin-left:50px;'>
        <el-form-item label="标识编码" prop="code">
          <el-input v-model="dataForm.code" style="width: 350px" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')" placeholder='标识编码'></el-input>
        </el-form-item>
        <el-form-item label="规则类型" prop="type">
          <el-input maxlength="50" v-model="dataForm.type" style="width: 350px" placeholder='规则类型'></el-input>
        </el-form-item>
        <el-form-item label="规则名称" prop="content">
          <el-input maxlength="50" v-model="dataForm.content" style="width: 350px" placeholder='规则名称'></el-input>
        </el-form-item>
        <el-form-item label="过期单位" prop="dateField">
          <el-select v-model="dataForm.dateField" clearable style="width: 350px;" placeholder="选择过期单位">
            <el-option label="年" value="0"></el-option>
            <el-option label="月" value="1"></el-option>
            <el-option label="日" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="过期时间" prop="expire">
          <el-input v-model="dataForm.expire" style="width: 350px" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')" placeholder="过期时间"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus == 'create'" type="primary" @click="createData">确定</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 200px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
}
</style>

<script>
import { getPointIndateList, pointIndateCreateItem, pointIndateUpdateItem, pointIndateState, pointIndateDel } from "@/api/pointManage";
import { dataTypeList } from "@/api/public";
import waves from "@/directive/waves"; // 水波纹指令
import Tinymce from '@/components/Tinymce'

export default {
  name: 'pointRulesList',
  components: { Tinymce },
  directives: { waves },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 10,
        content: '',
      },
      dataForm: {
        code: undefined,
        type: undefined,
        content: undefined,
        dateField: undefined,
        expire: undefined,
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: "编辑",
        create: "创建",
      },
      rules: {
        code: [{ required: true, message: "标识编码不能为空", trigger: "blur" }],
        type: [{ required: true, message: "规则类型不能为空", trigger: "blur" }],
        content: [{ required: true, message: "规则名称不能为空", trigger: "blur" }],
        dateField: [{ required: true, message: "请选积分过期单位", trigger: "blur" }],
        expire: [{ required: true, message: "过期时间不能为空", trigger: "blur" }],
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    changeState(id, index) {
      pointIndateState({ id: id, expireFalg: index }).then(response => {
        this.$notify({
          title: '成功',
          message: '状态修改成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    },
    handleDelete(id) {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        pointIndateDel({ id: id }).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList();
        })
      }).catch(() => { })

    },
    resetForm() {
      this.dataForm = {
        code: undefined,
        type: undefined,
        content: undefined,
        dateField: undefined,
        expire: undefined,
      };
    },
    handleCreate() {

      this.resetForm();
      this.dialogFormVisible = true;
      this.dialogStatus = "create";
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          console.log(this.dataForm);
          pointIndateCreateItem(this.dataForm)
            .then((response) => {
              this.dialogFormVisible = false;
              this.$notify({
                title: "成功",
                message: "创建成功",
                type: "success",
                duration: 2000,
              });
              this.getList();
            })
            .catch(() => { });
        }
      });
    },

    getList() {
      this.listLoading = true
      getPointIndateList(this.listQuery).then(response => {
        this.list = response.data.data.items
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {

      })
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },

    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },

    handleUpdate(row) {
      this.dataForm = Object.assign({}, row);
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          pointIndateUpdateItem(this.dataForm).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
  }
}
</script>
<style>
.ad-avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.ad-avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.ad-avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.ad-avatar {
  display: block;
}
</style>