<template>
  <div>
    <!-- 头部导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>数据工厂</el-breadcrumb-item>
      <el-breadcrumb-item>已有数据查询</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <div class="filter">
        <!-- 借款状态下拉框 -->
        <p>
          借款状态：
          <el-select v-model="selectStatus" placeholder="请选择借款状态" @change="selectStatusChange">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </p>
        <!-- 查询数据按钮 -->
        <p>
          <el-button type="primary" style="margin-left:20px" @click="checkData">查询数据</el-button>
        </p>
      </div>
      <!-- 数据表格 -->
      <div class="dataTable">
        <el-table :data="dataTable" border style="width: 100%">
          <el-table-column prop="phoneNo" label="phone_no" width="150px"></el-table-column>
          <el-table-column prop="userId" label="user_id" width="100px"></el-table-column>
          <el-table-column prop="uuid" label="uuid" width="330px"></el-table-column>
          <el-table-column prop="enable" label="enable" width="100px">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.enable === 1" type="success">启用</el-tag>
              <el-tag v-else-if="scope.row.enable === 0" type="danger">停用</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="registeredFrom" label="注册渠道" width="100px"></el-table-column>
          <el-table-column prop="createdAt" label="创建时间"></el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="queryInfo.pageNum"
        :page-size="queryInfo.pageSize"
        layout="total, prev, pager, next"
        :total="totalNum"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  inject: ['reload'],
  data() {
    return {
      baseUrl: '',
      // namespaceOptions: [],
      statusOptions: [],
      dataTable: [],
      // selectNamespace: '',
      selectStatus: '',
      totalNum: 0,
      queryInfo: {
        namespace: window.sessionStorage.getItem('env'),
        status: '',
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.setBaseUrl()
  },
  methods: {
    setBaseUrl() {
      this.$axios.defaults.baseURL = 'https://qa-platform-pre.liangkebang.net'
    },
    // 获取状态选项
    getStatusOptions() {
      this.$axios
        .get('/allUserLoanStatus')
        .then((response) => {
          this.statusOptions = response['data']['data']
        })
        .catch((error) => console.log(error))
    },
    // 获取表格数据
    getTableData() {
      if (this.selectNamespace === '') {
        return this.$message.error('namespace不能为空！')
      }
      if (this.selectStatus === '' || this.selectStatus === null) {
        return this.$message.error('状态不能为空！')
      }
      this.dataTable = []
      this.$axios
        .get('/getLoanUserData', {
          params: this.queryInfo
        })
        .then((response) => {
          this.dataTable = response.data.data.content
          this.totalNum = response.data.data.totalNum
          console.log(this.dataTable)
        })
        .catch((error) => console.log(error))
    },
    checkData() {
      this.queryInfo.pageNum = 1
      this.queryInfo.pageSize = 10
      this.getTableData()
    },
    // 用户状态改变
    selectStatusChange() {
      this.queryInfo.status = this.selectStatus
    },
    // namespace改变
    selectNamespaceChange() {
      this.queryInfo.namespace = this.selectNamespace
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize
      this.getTableData()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage
      this.getTableData()
    }
  },
  mounted() {
    // this.getNamespaceOptions()
    this.getStatusOptions()
  }
}
</script>

<style lang="less" scoped>
.dataTable {
  margin-top: 20px;
}

.filter {
  display: flex;
  p {
    margin-right: 20px;
  }
}
</style>
