<template>
  <basic-container>
    <el-dialog @close="dialog.state=false" append-to-body :close-on-click-modal="false" :visible.sync="dialog.state"
               :destroy-on-close="true"
               :title="dialog.title" width="80%">

    </el-dialog>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot-scope="scope" slot="dataInfo">
        <div style="display: flex;">
          <div>
            投票数量：
            <div
              style="background-color: #3296fa;color: white; border-radius: 5px; margin:10px; padding:0 10px 0 10px ; display:inline;">
              {{ scope.row.activeVoteCount }}
            </div>
          </div>
        </div>
        <div style="display: flex;">
          <div>
            曝光量：
            <div
              style="background-color: #1458da;color: white; border-radius: 5px; margin:10px; padding:0 10px 0 10px ; display:inline;">
              {{ scope.row.viewCount }}
            </div>
          </div>
        </div>
        <div style="display: flex;">
          <div>
            用户数：
            <div
              style="background-color: #da2414;color: white; border-radius: 5px; margin:10px; padding:0 10px 0 10px ; display:inline;">
              {{ scope.row.userCount }}
            </div>
          </div>
        </div>
        <div style="display: flex;">
          <div>
            转发数：
            <div
              style="background-color: #cf9236;color: white; border-radius: 5px; margin:10px; padding:0 10px 0 10px ; display:inline;">
              {{ scope.row.transmitCount }}
            </div>
          </div>
        </div>
        <div style="display: flex;">
          <div>
            作品数：
            <div
              style="background-color: #f9a7a7;color: white; border-radius: 5px; margin:10px; padding:0 10px 0 10px ; display:inline;">
              {{ scope.row.productNum }}
            </div>
          </div>
        </div>
      </template>
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.activerecord_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="menu" slot-scope="{row, size, type}">
        <el-button :type="type"
                   :size="size"
                   icon="el-icon-refresh-right"
                   @click="handleRefresh(row.id)">刷新活动排行榜缓存
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {add, getDetail, getList, remove, update, refreshActiveProductCache} from "@/api/sing_active/activerecord";
import {mapGetters} from "vuex";

export default {
    data() {
      return {
        dialog:{
          state: false,
          title: "活动积分商品配置",
        },
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          labelWidth: 120,
          column: [
            {
              label: "标题",
              prop: "title",
              search: true,
              rules: [{
                required: true,
                message: "请输入标题",
                trigger: "blur"
              }]
            },
            {
              label: "数据统计",
              prop: "dataInfo",
              display: false,
              rules: [{
                required: true,
                message: "请输入标题",
                trigger: "blur"
              }]
            },
            {
              label: "类型",
              prop: "type",
              type: "select",
              search: true,
              dicData:[
                {
                  label:"视频文本",
                  value:"VIDEO_TEXT"
                },
                {
                  label:"图片文本",
                  value:"IMAGE_TEXT"
                }
              ],
              rules: [{
                required: true,
                message: "请输入类型",
                trigger: "blur"
              }]
            },
            {
              label: "起始计数",
              prop: "startIndex",
              type: "number",
              hide: true,
              editDisabled: true,
              rules: [{
                required: true,
                message: "请输入起始计数",
                trigger: "blur"
              }]
            },
            {
              label: "主办方(简称)",
              prop: "sponsor",
              search: true,
              rules: [{
                required: false,
                message: "请输入活动图片地址",
                trigger: "blur"
              }]
            },
            {
              label: "上传作品开始时间",
              prop: "uploadProductStartTime",
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              search: true,
              searchRange: true,
              rules: [{
                required: true,
                message: "请输入开始时间",
                trigger: "blur"
              }]
            },
            {
              label: "上传作品结束时间",
              prop: "uploadProductEndTime",
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              searchRange: true,
              rules: [{
                required: true,
                message: "请输入开始时间",
                trigger: "blur"
              }]
            },
            {
              label: "投票开始时间",
              prop: "startTime",
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              search: true,
              searchRange: true,
              rules: [{
                required: true,
                message: "请输入开始时间",
                trigger: "blur"
              }]
            },
            {
              label: "投票结束时间",
              prop: "endTime",
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              rules: [{
                required: true,
                message: "请输入结束时间",
                trigger: "blur"
              }]
            },
            {
              label: "活动图片地址",
              prop: "imgUrl",
              type: 'upload',
              listType: 'picture-img',
              dataType: 'string',
              span: 24,
              propsHttp: {
                res: 'data',
                url: 'link'
              },
              action: '/api/blade-resource/oss/endpoint/put-file',
              rules: [{
                required: false,
                message: "请输入活动图片地址",
                trigger: "blur"
              }]
            },
            // {
            //   label: "活动视频地址",
            //   prop: "videoUrl",
            //   rules: [{
            //     required: true,
            //     message: "请输入活动视频地址",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "活动内容",
              prop: "content",
              component: 'AvueUeditor',
              options: {
                action: '/api/blade-resource/oss/endpoint/put-file',
                propsHttp: {
                  res: 'data',
                  url: 'link'
                },
              },
              hide: true,
              minRows: 6,
              span: 24,
              rules: [{
                required: true,
                message: "请输入活动内容",
                trigger: "blur"
              }]
            },

          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.activerecord_add, false),
          viewBtn: this.vaildData(this.permission.activerecord_view, false),
          delBtn: this.vaildData(this.permission.activerecord_delete, false),
          editBtn: this.vaildData(this.permission.activerecord_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        row.content = encodeURIComponent(row.content);
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        row.content = encodeURIComponent(row.content);
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
            this.form.content = decodeURIComponent(this.form.content);
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        Object.assign(params, this.query);
        if(params.startTime){
          params.beginTime = params.startTime[0];
          params.lastTime = params.startTime[1];
          params.startTime = null;
        }
        getList(page.currentPage, page.pageSize, params).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
      handleRefresh(activeId){
        this.loading = true;
        refreshActiveProductCache(activeId).then(()=>{
          this.$message.success("刷新成功")
        }).finally(()=>{
          this.loading = false;
        })
      }
    }
  };
</script>

<style>
</style>
