<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"
               :search.sync="search"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
<!--      <template slot-scope="{row,index}" slot="menu">-->
<!--        <el-button type="success"-->
<!--                   size="small"-->
<!--                   @click="handleCheck(row.id)">查看</el-button>-->
<!--      </template>-->
    </avue-crud>
    <el-dialog  append-to-body
                :visible.sync="dialogFormVisible" width="60%" height="50%">
      <avue-crud :search.sync="itemSearch"
                 :data="itemData"
                 :option="itemOption"
                 :table-loading="itemLoading"
                 :page.sync="itemPage"
                 @search-change="itemSearchChange"
                 @search-reset="itemSearchReset"
                 @selection-change="itemSelectionChange"
                 @current-change="itemCurrentChange"
                 @size-change="itemSizeChange"
                 @refresh-change="itemRefreshChange"
                 @on-load="itemOnLoad">
      </avue-crud>
    </el-dialog>
  </basic-container>
</template>

<script>
import {getList, getDetail} from "@/api/redpacket/redenvelopeManager";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      dialogFormVisible: false,
      form: {},
      query: {},
      itemQuery: {},
      loading: true,
      itemLoading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      itemPage: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      itemSelectionList: [],
      itemSearch:{
        redEnvelopeId:'',
        userName:''
      },
      itemOption: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        selection: true,
        dialogClickModal: false,
        addBtn:false,
        menu:false,
        column: [
          {
            label: "用户名",
            prop: "userName",
            search:true,
          },
          {
            label: "金额",
            prop: "amount",
          },
          {
            label: "时间",
            prop: "createTime",
          }]
      },
      search:{
        title:''
      },
      option: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: true,
        editBtn:false,
        delBtn:false,
        selection: true,
        dialogClickModal: false,
        column: [
          {
            label: "标题",
            prop: "title",
            search:true,
            rules: [{
              required: true,
              message: "请输入",
              trigger: "blur"
            }]
          },
          {
            label: "归属",
            prop: "belong",
            rules: [{
              required: true,
              message: "请输入",
              trigger: "blur"
            }]
          },
          {
            label: "总金额",
            prop: "totalAmount",
            rules: [{
              required: true,
              message: "请输入",
              trigger: "blur"
            }]
          },
          {
            label: "个数",
            prop: "num",
            rules: [{
              required: true,
              message: "请输入",
              trigger: "blur"
            }]
          },
          {
            label: "类型",
            prop: "type",
            rules: [{
              required: true,
              message: "请输入",
              trigger: "blur"
            }]
          },
          {
            label: "最大金额",
            prop: "maxAmount",
            rules: [{
              required: true,
              message: "请输入",
              trigger: "blur"
            }]
          },
          {
            label: "最小金额",
            prop: "minAmount",
            rules: [{
              required: true,
              message: "请输入",
              trigger: "blur"
            }]
          },
          {
            label: "创建时间",
            prop: "createTime",
            rules: [{
              required: true,
              message: "请输入",
              trigger: "blur"
            }]
          },
        ]
      },
      data: [],
      itemData: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: false,
        viewBtn: true,
        delBtn: false,
        editBtn: false
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    handleCheck(id){
      this.dialogFormVisible=true
      this.itemSearch.redEnvelopeId=id,
      this.itemOnLoad(this.itemPage,this.itemSearch)
    },
    beforeOpen(done, type) {
      this.dialogFormVisible=true
      if (["edit", "view"].includes(type)) {
        this.handleCheck(this.form.id)
        // getDetail(1, 10, 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;
      console.log("执行的是1");
      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();
      });
    },
    itemSearchReset() {
      this.itemQuery = {};
      this.itemOnLoad(this.itemPage);
    },
    itemSearchChange(params, done) {
      this.itemQuery = params;
      this.itemPage.currentPage = 1;
      this.itemOnLoad(this.itemPage, params);
      done();
    },
    itemSelectionChange(list) {
      this.itemSelectionList = list;
    },
    itemSelectionClear() {
      this.itemSelectionList = [];
      this.$refs.crud.toggleSelection();
    },
    itemCurrentChange(currentPage) {
      this.itemPage.currentPage = currentPage;
    },
    itemSizeChange(pageSize) {
      this.itemPage.pageSize = pageSize;
    },
    itemRefreshChange() {
      this.itemLoading(this.itemPage, this.itemQuery);
    },
    itemOnLoad(page,itemParams= {}) {
      this.itemLoading = true;
      itemParams.redEnvelopeId = this.form.id
      getDetail(page.currentPage, page.pageSize,  Object.assign(itemParams, this.itemQuery)).then(res => {
        const data = res.data.data;
        this.itemPage.total = data.total;
        this.itemData = data.records;
        console.log("itemData",this.itemData)
        this.itemLoading = false;
        this.itemSelectionClear();
      });
    }
  }
};
</script>

<style>
</style>
