<template>
  <div>
    <el-card :body-style="{ padding: '5px' }" style="margin-bottom:5px">
      <header-box :search="search" @searchClick='getData' />
    </el-card>
    <el-card :body-style="{ padding: '5px' }">
      <div style="text-align:right;padding:5px 10px ">
        <el-button @click="diaBox('新增')" v-if="showButtons.add" size="mini" type="primary">新建</el-button>
      </div>
      <content-box :showButtons="showButtons" :tableData='tableData' :tableLoading='tableLoading' :pagination='pagination' @edit='diaBox'
        @getData='getData' />
    </el-card>
    <dia-box ref="diaBox" @cancel='cancel' :title='title' :isDiaBox='isDiaBox' @getData='getData' />
  </div>
</template>
<script>
  import {
    mapState
  } from "vuex";
  import headerBox from "./components/header.vue"
  import diaBox from "./components/diaBox.vue"
  import contentBox from "./components/content.vue"
  export default {
    data() {
      return {
        search: {
          boxName: "",
        },
        params: {},
        title: "新增",
        isDiaBox: false,
      }
    },
    computed: {
      ...mapState({
        tableData: (state) => state.fileParameters.tableData,
        pagination: (state) => state.fileParameters.pagination,
        showButtons: (state) => state.fileParameters.showButtons,
      }),
      tableLoading: {
        get() {
          return this.$store.state.fileParameters.tableLoading;
        },
        set(val) {
          this.$store.state.fileParameters.tableLoading = val;
        },
      },
    },
    components: {
      headerBox,
      contentBox,
      diaBox
    },
    methods: {
      getData() {
        this.params = {
          boxName: this.search.boxName,
          page: this.pagination.pageNum,
          limit: this.pagination.pageSize,
        }
        this.tableLoading = true
        this.$store.dispatch('fileParameters/getData', this.params).catch(err => {
          this.tableLoading = false
        })
      },
      diaBox(str, val) {
        this.title = str
        this.isDiaBox = true
        if (str != '新增') {
          for (let key in this.$refs.diaBox.form) {
            for (let index in val) {
              if (key === index) {
                this.$refs.diaBox.form[key] = val[index]
              }
            }
          }
        }
      },
      cancel() {
        this.isDiaBox = false
      }
    },
    created() {
      let btnData = {
        menuid: this.$getMenuId(sessionStorage.getItem("routerIdList"), this.$route.path),
        roleid: sessionStorage.getItem("roleid")
      }
      this.$store.dispatch("fileParameters/getButtonList", btnData);
      this.getData()
    }
  }

</script>
