<template>
  <div class="app-container">
    <div class="mymain-container">
      <div class="btn-group clearfix">
        <div class="fl">
          <el-button type="primary" icon="el-icon-plus" size="small" @click="addOrEdit('add')">添加模块</el-button>
          <el-button type="primary" size="small" @click="editCommonModule">编辑公共模块</el-button>
        </div>
      </div>

      <div class="table">
        <el-table v-loading="listLoading" :data="dataList" element-loading-text="Loading" border fit highlight-current-row stripe>
          <el-table-column align="center" label="模块名称" prop="name"></el-table-column>
          <el-table-column align="center" label="创建时间" prop="createTime"></el-table-column>
          <el-table-column align="center" label="修改时间" prop="updateTime"></el-table-column>
          <el-table-column align="center" label="商户" prop="companyName"></el-table-column>
          <el-table-column align="center" label="操作" width="140">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="addOrEdit('edit', scope.row.commonTemplateId)">编辑</el-button>
              <el-popconfirm style="margin-left: 10px;" title="确定删除吗？" @onConfirm="delItem(scope.row.commonTemplateId)" >
                <el-button slot="reference" size="mini">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination clearfix">
        <div class="fr">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="listTotal">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getModuleList, deleteModule } from '@/api/goods'

export default {
  data() {
    return {
      dataList: null, // 列表数据
      listLoading: true, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
    }
  },

  created() {
    this.getList();
  },

  methods: {

    getList() {
      this.listLoading = true;

      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize
      };

      getModuleList(params).then(res => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
      })
    },

    // 编辑公共模块
    editCommonModule() {
      this.$router.push({
        name:"explain_common",
        query: {}
      })
    },

    // 添加模块
    addOrEdit(type, id) {
      if(type == 'add') {
        this.$router.push({
          name:"explain_add",
          query: {}
        })
      }else {
        this.$router.push({
          name:"explain_add",
          query: {
            id
          }
        })
      }
    },

    // 删除模块
    delItem(commonTemplateId) {
      deleteModule({
        commonTemplateId
      }).then(res => {
        this.getList();
        this.$successMsg();
      })
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getList();
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },

  }
}
</script>

<style scoped>

</style>