<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="awwardTempList">
      <el-table-column label="模板id" prop="id" align="center" />
      <el-table-column label="模板名称" prop="templateName" :show-overflow-tooltip="true" width="150" align="center" />
      <el-table-column label="模板描述" prop="templateDesc" :show-overflow-tooltip="true" align="center" />
      <el-table-column label="模版小程序页面地址" prop="templateAppletPage" align="center" />
      <el-table-column label="模版奖项数量" prop="templateDrawNumber" align="center" />
      <el-table-column label="模版小程序页面图片" prop="templateDemoImg" align="center">
        <template slot-scope="scope">
          <img class="table-img" v-show="scope.row.templateDemoImg" :src="baseUrl + scope.row.templateDemoImg" alt="模板图片">
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</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="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="模板名称" prop="templateName">
          <el-input v-model="form.templateName" placeholder="请输入模板名称" />
        </el-form-item>
        <el-form-item label="模板描述" prop="templateDesc">
          <el-input v-model="form.templateDesc" placeholder="请输入模板描述" />
        </el-form-item>
        <el-form-item label="模版奖项数量" prop="templateDrawNumber">
          <el-input v-model="form.templateDrawNumber" placeholder="请输入模板数量" />
        </el-form-item>
        <el-form-item label="模版小程序页面地址" prop="templateAppletPage">
          <el-input v-model="form.templateAppletPage" placeholder="请输入模版小程序页面地址" />
        </el-form-item>
        <el-form-item label="模版小程序页面图片" prop="templateDemoImg">
          <ImageUpload :limit="1" :uploadType="'jiangpin'" :value="form.templateDemoImg" @input="getPhotoUrl"></ImageUpload>
        </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>
  </div>
</template>

<script>
import {
  getDrawTemplateList,
  addDrawTemplate,
  delAward,
  updateDrawTemplate
} from "@/api/award/awardList"

import imageUpload from "../../components/ImageUpload/index.vue";

export default {
  name: "Role",
  components: {
    imageUpload
  },
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_URL + process.env.VUE_APP_BASE_API + '/',
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      total: 0,
      awwardTempList: [],
      title: "",
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        templateName: [
          { required: true, message: "模板名称不能为空", trigger: "blur" }
        ],
        templateDesc: [
          { required: true, message: "模板描述不能为空", trigger: "blur" }
        ],
        templateDrawNumber: [
          { required: true, message: "模板数量不能为空", trigger: "blur" }
        ],
        templateAppletPage: [
          { required: true, message: "模版小程序页面地址不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getPhotoUrl(data) {
      this.form.templateDemoImg = data
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      getDrawTemplateList(this.queryParams).then((res) => {
        this.queryParams.pageNum = res.data.current
        this.total = res.data.total
        this.awwardTempList = res.data.records
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.form = {}
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row
      this.open = true
      this.title = "编辑";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.form.id) {
            addDrawTemplate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          } else {
            updateDrawTemplate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const awardName = row.awardName
      this.$modal.confirm('是否确认名称为"' + awardName + '"的奖品？').then(function() {
        return delAward({awardId: row.id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>