<template>
  <basic-container>
    <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="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.helprecord_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>

      <template slot="senderInfo" slot-scope="scope">
        <div style="display: flex;">
          <div style="display: flex;justify-content: center;align-items: center;">
            <el-avatar size="large" :src="scope.row.sender.avatar"></el-avatar>
          </div>
           <div style="padding: 10px;">
             <div >昵称：{{scope.row.sender.nickName}}</div>
             <div>手机：{{scope.row.sender.phone}}</div>
           </div>
        </div>
      </template>

      <template slot="receiverInfo" slot-scope="scope">
        <div style="display: flex;">
          <div style="display: flex;justify-content: center;align-items: center;">
            <el-avatar size="large" :src="scope.row.receiver.avatar"></el-avatar>
          </div>
           <div style="padding: 10px;">
             <div >昵称：{{scope.row.receiver.nickName}}</div>
             <div>手机：{{scope.row.receiver.phone}}</div>
           </div>
        </div>
      </template>

      <template slot="presentInfo" slot-scope="scope">
        <div style="display: flex;">
          <div style="display: flex;justify-content: center;align-items: center;">
            <el-image :preview-src-list="[scope.row.present.icon]" style="width: 50px;height: 50px;" :src="scope.row.present.icon"></el-image>
          </div>
           <div style="padding: 10px;margin-left: 20px;">
             <div>名称：{{scope.row.present.name}}</div>
             <div>价格：{{scope.row.present.price}}</div>
             <div>热力值：{{scope.row.present.hotValue}}</div>
             <div>积分值：{{scope.row.present.pointsValue}}</div>
           </div>
        </div>
      </template>



    </avue-crud>
  </basic-container>
</template>

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

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          menu:false,
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          menuRight:false,
          column: [
            {
              width:120,
              label: "活动名称",
              prop: "activityName",
            },
            {
              label: "作品名称",
              prop: "productionName",
              rules: [{
                required: true,
                message: "请输入作品id",
                trigger: "blur"
              }]
            },
            {
              width:188,
              label: "助力人信息",
              prop: "senderInfo",
              slot:true
            },
            {
              width:188,
              label: "被助力人信息",
              prop: "receiverInfo",
              slot:true
            },
            {
              label: "打赏类型",
              width:90,
              prop: "type",
              dicData: [{
                  label: '打赏作品',
                  value: 1
                },
                {
                  label: '打赏作者',
                  value: 3
                }
              ]
            },
            {
              width:235,
              label: "打赏礼物信息",
              prop: "presentInfo",
              slot:true
            },
            {
              label: "打赏礼物",
              hide:true,
              prop: "present",
              rules: [{
                required: true,
                message: "请输入礼物id",
                trigger: "blur"
              }]
            },
            {
              label: "礼物数量",
              width:80,
              prop: "count",
              rules: [{
                required: true,
                message: "请输入打赏的礼物数量",
                trigger: "blur"
              }]
            },
            {
              label: "礼物的总价格",
              width:100,
              prop: "totalCash",
              rules: [{
                required: true,
                message: "请输入打赏礼物的总价格",
                trigger: "blur"
              }]
            },
            {
              label: "获取积分值",
              width:100,
              prop: "totalPointsValue",
            },
            {
              label: "助力热力值",
              width:100,
              prop: "totalHotValue",
              rules: [{
                required: true,
                message: "请输入助力总热力值",
                trigger: "blur"
              }]
            },
            {
              width:130,
              label: "打赏时间",
              prop: "createTime",
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.helprecord_add, false),
          viewBtn: this.vaildData(this.permission.helprecord_view, false),
          delBtn: this.vaildData(this.permission.helprecord_delete, false),
          editBtn: this.vaildData(this.permission.helprecord_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        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;
          });
        }
        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;
        this.query.activityId = this.$route.query.id;
        this.query.typeSub=2
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
