<template>
  <div class="app-container" ref="main">
    <div ref="search" class="search">
      <MyInput v-model="queryParams.name" @clickSearch="handleSearch" />
      <el-button type="primary" size="small" @click="handleAdd">新增</el-button>
    </div>

    <ul class="book-main" :style="listStyle" v-if="certificatesList.length > 0">
      <li
        v-for="(item, index) in certificatesList"
        :key="item.id"
        :style="{ marginBottom: isLastRow(index) ? '0px' : '' }"
      >
        <div class="integral-num">积分：{{ item.integral }}</div>

        <el-image
          style="width: 100%; height: 87%"
          :src="baseUrl + item.cover"
          fit="fill"
        >
        </el-image>

        <div class="operate">
          <div class="integral">
            {{ item.name }}
          </div>
          <div class="operate-child">
            <el-button
              type="text"
              size="mini"
              style="color: #909399"
              @click="handleInfo(item)"
              >查看</el-button
            >
            <el-button
              type="text"
              size="mini"
              style="color: #e6a23c; margin: 0 10px"
              @click="handleUpdate(item)"
              >修改</el-button
            >
            <el-button
              type="text"
              size="mini"
              style="color: #f56c6c; margin: 0"
              @click="handleDelete(item)"
              >删除</el-button
            >
          </div>
        </div>
      </li>
    </ul>
    <div v-if="certificatesList.length == 0" :style="listStyle" class="noData">
      <el-empty description="暂无证书"></el-empty>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改证书管理对话框 -->
    <el-dialog :visible.sync="open" width="950px" append-to-body>
      <div slot="title" class="dialog-title">
        <span class="title-line"></span>
        {{ title }}
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="证书名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入证书名称" />
        </el-form-item>
        <el-form-item label="积分" prop="integral">
          <el-input v-model="form.integral" placeholder="请输入积分" />
        </el-form-item>
        <el-form-item label="证书封面" prop="cover">
          <ImageUpload :limit="1" v-model="form.cover" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog :visible.sync="openInfo" width="950px" append-to-body>
      <div slot="title" class="dialog-title">
        <span class="title-line"></span>
        详情
      </div>
      <div class="info-box">
        <div>
          <div class="info-item">
            <div class="item-lable">证书名称:</div>
            <div class="item-value">{{ form.name }}</div>
          </div>
          <div class="info-item">
            <div class="item-lable">积分:</div>
            <div class="item-value">{{ form.integral }}</div>
          </div>
          <div class="info-item">
            <div class="item-lable">证书封面:</div>
            <div class="item-value">
              <ImagePreview :src="form.cover" width="269px" height="192px" />
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCertificates,
  getCertificates,
  delCertificates,
  addCertificates,
  updateCertificates,
} from "@/api/volunteer/gxhzs/gxhzsgl/index.js";
export default {
  data() {
    return {
      listStyle: {
        height: 0,
        overflowY: "auto",
      },
      baseUrl: process.env.VUE_APP_BASE_API,
      certificatesList: [],
      loading: false,
      tableData: [],
      total: 0,
      queryParams: {
        type: 0, //个性化证书
        name: undefined,
        pageNum: 1,
        pageSize: 10,
      },
      form: {},
      rules: {
        name: [{ required: true, message: "请输入证书名称", trigger: "blur" }],
        integral: [{ required: true, message: "请输入积分", trigger: "blur" }],
        cover: [
          { required: true, message: "请上传证书封面", trigger: "change" },
        ],
      },
      open: false,
      openInfo: false,
      title: "",
    };
  },
  created() {
    // //给表格一个固定值
    this.$nextTick(() => {
      this.listStyle.height =
        this.$refs.main.offsetHeight -
        40 -
        this.$refs.search.offsetHeight -
        42 +
        "px";
      this.getList();
    });
  },
  methods: {
    isLastRow(index) {
      const rowCount = Math.ceil(this.certificatesList.length / 5);
      const row = Math.floor(index / 5);
      return row === rowCount - 1;
    },
    /**搜索 */
    handleSearch(keyWord) {
      this.queryParams = {
        name: keyWord,
        pageNum: 1,
        pageSize: 10,
      };
      this.getList();
    },
    /** 查询证书管理列表 */
    getList() {
      this.loading = true;
      listCertificates(this.queryParams).then((response) => {
        this.certificatesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        cover: null,
        content: null,
        datetime: null,
        integral: null,
        createId: null,
        createBy: null,
        createTime: null,
        updateId: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        userId: null,
        deptId: null,
      };
      this.resetForm("form");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加证书管理";
    },
    /**详情 */
    handleInfo(row) {
      this.reset();
      const id = row.id;
      getCertificates(id).then((response) => {
        this.form = response.data;
        this.openInfo = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCertificates(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改证书管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.type = 0;
          if (this.form.id != null) {
            updateCertificates(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCertificates(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除证书管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCertificates(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/styles/myTable.scss";
</style>
