<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div class="screen-container">
      <Collapse :screen-form="screenForm">
        <template #right_btn>
          <el-button size="mini" @click="resetScreenForm">清空</el-button>
          <el-button size="mini" type="primary" @click="submitScreenForm">搜索</el-button>
        </template>
        <template #search>
          <el-form ref="screenForm" :model="screenForm" label-width="70px" size="mini" label-position="left">
            <el-row :gutter="20">
              <el-col :xs="24" :sm="12" :lg="6">
                <el-form-item label="接口名称" prop="name">
                  <el-input v-model="screenForm.name" placeholder="请输入接口名称"></el-input>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :lg="6">
                <el-form-item label="接口状态" prop="status">
                  <el-select v-model="screenForm.status" placeholder="请选择接口状态" clearable>
                    <el-option
                      :label="item.label"
                      :value="item.value"
                      v-for="(item, index) in statusList"
                      :key="index"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </template>
      </Collapse>
    </div>

    <div class="mymain-container">
      <div class="btn-group clearfix">
        <div class="fl" />
        <div class="fr">
          <ExportButton :exUrl="'admin/user/mch/export'" :exParams="exParams" />
        </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="description"></el-table-column>
          <el-table-column align="center" label="接口状态" prop="status">
            <template slot-scope="scope">
              {{ scope.row.status | statusFilter }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="更新时间" prop="syncTime"></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 { getApiList } from '@/api/setting'

let that
export default {
  filters: {
    statusFilter(val) {
      const obj = that.statusList.find(o => o.value === val)
      return obj ? obj.label : ''
    }
  },
  data() {
    return {
      dataList: null, // 列表数据
      listLoading: true, // 列表加载loading
      screenForm: {
        // 筛选表单数据
        status: '',
        name: ''
      },
      statusList: [
        { label: '未同步', value: 0 },
        { label: '已同步', value: 1 },
        { label: '同步失败', value: 2 }
      ],
      isCollapse: true,
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0 // 列表总数
    }
  },

  computed: {
    exParams() {
      return {
        name: this.screenForm.name,
        status: this.screenForm.status
      }
    }
  },

  beforeCreate() {
    that = this
  },

  created() {
    this.getList()
  },

  methods: {
    // 查询按钮权限
    checkBtnRole(value) {
      // let btnRole = this.$route.meta.roles;
      // if(!btnRole) {return true}
      // let index = btnRole.indexOf(value);
      // return index >= 0 ? true : false;
      return true
    },

    getList() {
      this.listLoading = true

      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        name: this.screenForm.name,
        status: this.screenForm.status
      }

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

    // 筛选后重新获取列表
    getListByScreen() {
      this.currentPage = 1
      this.getList()
    },

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

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

    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1
      this.getList()
    },

    // 重置筛选表单
    resetScreenForm() {
        this.$refs.screenForm.resetFields()
      this.currentPage = 1
      this.getList()
    }
  }
}
</script>

<style></style>
