<template>
  <cms-modal
    v-model="visible"
    v-if="visible"
    @close="close"
    @open="open"
    :modal-config="modalConfig"
    :modal-title="modalTitle"
    :destroy-on-close="true"
    :modal-width="modalWidth"
    :modal-height="modalHeight"
  >
    <el-row :gutter="10" class="el-card-content" v-if="result.rows.length > 0">
      <div v-if="result.rows.length === 0">
        <el-empty description="暂时无数据"></el-empty>
      </div>
      <el-col :span="span" v-for="(o, index) in result.rows" :key="index" style="margin-bottom: 10px">
        <el-card :body-style="{ padding: '0px' }" shadow="hover">
          <div @click="rowClick(o)">
            <slot :row="o"></slot>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <template slot="footer">
      <el-pagination
        :page-size="20"
        :total="pageConfig.total"
        :current-page="pageConfig.page"
        :background="true"
        :page-count="pageCount"
        @current-change="currentChange"
        @prev-click="currentChange"
        @next-click="currentChange"
        layout="prev, pager, next, total"
      />
    </template>
  </cms-modal>
</template>

<script>
export default {
  name: "CmsDialogCardPage",
  data() {
    return {
      pageConfig: {
        total: 0,
        page: 1,
        rows: 18,
      },
      result: {
        rows: [],
        extraMap: {}
      }
    };
  },
  created() {
    this.loadPageData();
  },
  methods: {
    currentChange(page) {
      this.pageConfig.page = page;
      this.loadPageData();
    },
    rowClick(o) {
      this.$emit('rowClick', o);
    },
    loadPageData() {
      if (typeof this.requestCall === 'function') {
        this.requestCall({ ...this.pageConfig, ...this.extraData }).then(result => {
          if (result && result.code === 200) {
            this.result = Object.assign({}, this.result, result || {});
            this.pageConfig.total = result.total;
            console.log('----------', this.pageConfig);
          } else this.$messageTips.error(result.msg);
        })
      }
      if (Array.isArray(this.requestCall)) {
        this.result.rows = this.requestCall;
      }
    },
    close() {
      this.visible = false
      this.$emit('close');
    },
    open() {
      this.$emit('open');
    }
  },
  computed: {
    pageCount() {
      return (this.pageConfig.total + this.pageConfig.page - 1) / this.pageConfig.pageSize
    },
    visible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("input", val); // 通过事件实现组件属性到父组件的传递
      },
    },
  },
  props: {
    value: { // modal默认是否显示
      type: Boolean,
      default: () => {
        return false
      }
    },
    extraData: {
      type: Object,
      default: () => {
        return {}
      }
    },
    requestCall: {
      type: [ Array, Function, Number, String ],
      default: []
    },
    span: {
      type: Number,
      default: 4,
    },
    // 弹窗相关配置
    modalConfig: {
      type: Object,
      default: () => {
        return {
          showClose: true,
          fullscreen: false
        }
      }
    },
    modalTitle: { // modal 标题
      type: String,
      default: () => {
        return ''
      }
    },
    modalWidth: { // modal 宽度
      type: [ String, Number ],
      default: () => {
        return '50%'
      }
    },
    modalHeight: { // modal 高度
      type: [ String, Number ],
      default: '50%'
    },
    destroyOnClose: {
      type: Boolean,
      default: () => {
        return true
      }
    }
  }
}
</script>

<style lang="scss">
.image {
  width: 100%;
  display: block;
}

.cms-dialog-card-page {
  .el-card:hover {
    cursor: pointer;
    border: 1px solid #6610f2;
  }

  .time {
    font-size: 13px;
    color: #999;
  }

  .bottom {
    margin-top: 13px;
    line-height: 12px;
  }

  .button {
    padding: 0;
    float: right;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both
  }
}
</style>