<template>
  <section class="paList">
    <ai-list>
      <ai-title slot="title" title="用户账号" isShowBottomBorder/>
      <template #content>
        <ai-search-bar>
          <template #left>
            <ai-select v-model="search.userEnterpriseStatus" :selectList="dict.getDict('userEnterpriseStatus')"
                       placeholder="认证状态" @change="page.current=1,getTableData()"/>
            <ai-select v-model="search.status" :selectList="dict.getDict('portalUserStatus')" placeholder="账号状态"
                       @change="page.current=1,getTableData()"/>
          </template>
          <template #right>
            <el-input size="small" placeholder="搜索用户账号" v-model="search.phone" clearable
                      @change="page.current=1,getTableData()"/>
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size"
                  @getList="getTableData" :col-configs="colConfigs" :dict="dict">
          <el-table-column slot="options" label="操作" fixed="right" align="center">
            <template slot-scope="{row}">
              <el-button v-if="row.status==0" type="text" @click="handleEnable(row)">启用</el-button>
              <el-button v-else-if="row.status==1" type="text" @click="handleEnable(row)">禁用</el-button>
              <el-button type="text" @click="showDetail(row.id)">详情</el-button>
            </template>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>
  </section>
</template>

<script>
export default {
  name: "paList",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  data() {
    return {
      search: {name: "",},
      page: {current: 1, size: 10, total: 0},
      tableData: [],
      colConfigs: [
        {label: "用户账号", prop: "phone"},
        {label: "企业注册数量", prop: "enterpriseNumber", align: "center"},
        {label: "认证状态", prop: "userEnterpriseStatus", dict: "userEnterpriseStatus", align: "center"},
        {label: "注册时间", prop: "createTime"},
        {label: "账号状态", prop: "status", dict: "portalUserStatus", align: "center"},
        {slot: "options"}
      ]
    }
  },
  methods: {
    getTableData() {
      this.instance.post("/appportaluser/listPortalUser", null, {
        params: {...this.page, ...this.search}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records
          this.page.total = res.data.total
        }
      })
    },
    handleEnable(row) {
      let status = (Number(row.status) + 1) % 2
      this.$confirm(`是否要${this.dict.getLabel('portalUserStatus', status)}账号?`).then(() => {
        this.instance.post("/appportaluser/addOrUpdate", {...row, status}).then(res => {
          if (res?.code == 0) {
            this.$message.success(this.dict.getLabel('portalUserStatus', status) + "成功!")
            this.getTableData()
          }
        })
      }).catch(() => 0)
    },
    showDetail(id) {
      this.$router.push({query: {id}})
    }
  },
  created() {
    this.getTableData()
  }
}
</script>

<style lang="scss" scoped>
.paList {
  height: 100%;
}
</style>
