<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="角色名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入角色名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="角色状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
    <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="awwardList">
      <el-table-column label="奖品名称" prop="awardName" :show-overflow-tooltip="true" width="150" align="center" />
      <el-table-column label="奖品数量" prop="awardNum" align="center" />
      <el-table-column label="奖品图片" prop="awardPhotoUrl" align="center">
        <template slot-scope="scope">
          <img class="table-img" v-show="scope.row.awardPhotoUrl" :src="baseUrl + scope.row.awardPhotoUrl" alt="奖品图片">
        </template>
      </el-table-column>
      <el-table-column label="奖品类型" prop="awardType" align="center" />
      <el-table-column label="奖品id" prop="id" align="center" />
      <el-table-column label="生效日期" align="center" prop="effectiveDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectiveDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="失效日期" align="center" prop="expiryDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expiryDate) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否删除" prop="isDelete" align="center" /> -->

      <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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="奖品名称" prop="awardName">
          <el-input v-model="form.awardName" placeholder="请输入奖品名称" />
        </el-form-item>
        <el-form-item label="奖品数量" prop="awardNum">
          <el-input v-model="form.awardNum" placeholder="请输入奖品数量" />
        </el-form-item>
        <el-form-item label="奖品图片" prop="awardPhotoUrl">
          <ImageUpload :limit="1" :uploadType="'jiangpin'" @input="getPhotoUrl"></ImageUpload>
        </el-form-item>
        <el-form-item label="奖品类型" prop="awardType">
          <el-input v-model="form.awardType" placeholder="请输入奖品类型" />
        </el-form-item>
        <el-form-item label="生效日期" prop="effectiveDate">
          <el-date-picker
            v-model="form.effectiveDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="生效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="失效日期" prop="expiryDate">
          <el-date-picker
            v-model="form.expiryDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="失效日期">
          </el-date-picker>
        </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 {
  awardList,
  addAwardt,
  delAward
} 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,
      awwardList: [],
      title: "",
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        awardName: [
          { required: true, message: "奖品名称不能为空", trigger: "blur" }
        ],
        awardNum: [
          { required: true, message: "奖品数量不能为空", trigger: "blur" }
        ],
        awardType: [
          { required: true, message: "奖品类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getPhotoUrl(data) {
      this.form.awardPhotoUrl = data
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      awardList(this.queryParams).then((res) => {
        this.queryParams.pageNum = res.data.current
        this.total = res.data.total
        this.awwardList = res.data.records
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加奖品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addAwardt(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const awardName = row.awardName
      this.$confirm('是否确认名称为"' + awardName + '"的抽奖活动？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delAward({id: row.id}).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg)
              this.getList()
            } else {
              this.$message.error(res.msg)
            }
          })
        }).catch(() => {
          this.$message.info('已取消删除')
        });
    },
  }
};
</script>