<template>
  <div class="app-container account-examine">
    <!-- 搜索 -->
    <el-form :model="state.queryParams" ref="queryRef" :inline="true">
      <el-form-item label="用户名" prop="accountname">
        <el-input v-model="state.queryParams.accountname" placeholder="账号/公司名称" clearable
          @keyup.enter="handleQuery({ initPageIndex: true })" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery({ initPageIndex: true })">搜索</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table v-loading="state.tableLoading" :data="state.tableData" row-key="id" style="flex:1;">
      <el-table-column prop="accountname" label="用户名" min-width="100"></el-table-column>
      <el-table-column prop="accountgsm" label="公司名称" min-width="150">
        <template #default="scope">
          <span>{{ scope.row[scope.column.property] ? scope.row[scope.column.property] : '-'
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="accounttype" label="用户类型" min-width="100"></el-table-column>
      <el-table-column prop="accountshtype" label="商户类型" min-width="100"></el-table-column>
      <!-- <el-table-column prop="accountjflx" label="计费类型" min-width="100"></el-table-column> -->
      <el-table-column prop="accounttxtnum" label="文本可用次数" min-width="100"></el-table-column>
      <el-table-column prop="accounttxtimgnum" label="图片可用次数" min-width="100"></el-table-column>
      <el-table-column prop="accountshstatus" label="审核状态" min-width="100"></el-table-column>
      <el-table-column prop="accountcreatedate" label="开通时间" min-width="150"></el-table-column>
      <!-- <el-table-column prop="accountenddate" label="到期时间" min-width="150">
        <template #default="scope">
          <span>{{ scope.row[scope.column.property] ? scope.row[scope.column.property] : '-'
          }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" :width="fitWidth" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <div class="operation-box">
            <el-text class="operate-btn-item" type="primary" @click="accountExamineRef.openDialog(scope.row)">审核</el-text>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-box">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="paginaionConfig.query.pageindex" :page-sizes="paginaionConfig.sizeOptions"
        :page-size="paginaionConfig.query.rows" layout="total, sizes, prev, pager, next, jumper"
        :total="paginaionConfig.total">
      </el-pagination>
    </div>

    <AccountExamine width="70%" ref="accountExamineRef" @refresh="handleQuery" />
  </div>
</template>

<script setup name="AccountExamine">
import useAccountManageStore from '@store/modules/accountManage'
import useSystemManageStore from '@store/modules/systemManage'
import AccountExamine from '../components/AccountExamine/index.vue'
import _ from 'lodash'
import usePagination from '@/hooks/usePagination'
import useFitColumn from '@/hooks/useFitColumn'

const { proxy } = getCurrentInstance()
const accountManageStore = useAccountManageStore()
const systemManageStore = useSystemManageStore()
const { paginaionConfig, handleSizeChange, handleCurrentChange } = usePagination(handleQuery)
const { fitWidth } = useFitColumn()

const state = reactive({
  tableLoading: false,
  tableData: [],
  queryParams: {
    accountname: '',
    accountshstatus: '待审核',
  },
})
const dictsData = ref(proxy.useDict('关联公司'))
const roleList = ref([])
const accountExamineRef = ref(null)

provide('roleList', readonly(roleList))
provide('dictsDataParent', readonly(dictsData))

const getRoleListTagArr = (str) => {
  let result = []
  const taglist = str.split(',')

  taglist.forEach((item) => {
    roleList.value.forEach((innerItem) => {
      if (innerItem.id === Number(item)) result.push(innerItem)
    })
  })
  return result
}

/** 查询菜单列表 */
const getList = async (query) => {
  state.tableLoading = true
  const { Total, Data } = await accountManageStore.list(query)
  paginaionConfig.total = Total
  state.tableData = Data

  state.tableLoading = false
}

const getRoleList = async () => {
  const { Total, Data } = await systemManageStore.sysusergroupLoad({ pageindex: 1, rows: 1000 })
  roleList.value = Data
}

function handleQuery(config) {
  if (config?.initPageIndex) paginaionConfig.query.pageindex = 1
  getList(Object.assign(_.cloneDeep(paginaionConfig.query), state.queryParams))
}

const availableChange = async (row, newVal) => {
  row.loading = true
  try {
    // await accountManageStore.sysuserUp({ account: row.account, available: newVal })
    // row.loading = false
    // proxy.$modal.msgSuccess('操作成功！')
  } catch (error) {
    handleQuery()
  }
}

onMounted(() => {
  handleQuery({ initPageIndex: true })
  getRoleList()
})
</script>
