<template>
  <div class="app-container order-manage">
    <!-- 搜索 -->
    <el-form :model="state.queryParams" ref="queryRef" :inline="true">
      <el-form-item label="订单ID" prop="accountname">
        <el-input v-model="state.queryParams.accountname" placeholder="账号/公司名称" clearable @keyup.enter="handleQuery({initPageIndex: true})" />
      </el-form-item>

      <!-- 按钮组件 -->
      <all-button
        :buttonType="['add', 'reset', 'query', 'refresh']"
        @addButton="accountEditRef.openDialog()"
        @queryButton="handleQuery({initPageIndex: true})"
        @resetButton="resetButton()"
        @refreshButton="handleQuery({initPageIndex: true})"
      />
    </el-form>
    <!-- 表格 -->
    <el-table v-loading="state.tableLoading" :data="state.tableData" row-key="id" style="flex: 1" stripe border table-layout="fixed" @cell-dblclick="tableCelldbl2">
      <el-table-column align="center" prop="accountid" label="用户ID" min-width="150" sortable></el-table-column>
      <el-table-column align="center" prop="accountname" label="用户名" min-width="150"></el-table-column>
      <el-table-column align="center" prop="accountnickname" 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 align="center" prop="accountgrouplist" label="角色组" min-width="180">
        <template #default="scope">
          <span v-if="!Boolean(scope.row[scope.column.property])">-</span>
          <p v-else style="margin: 0">
            <el-tag v-for="(item, index) in getRoleListTagArr(scope.row[scope.column.property])" :key="`role-tags-${index}`" style="margin-right: 5px">{{ item.title }}</el-tag>
          </p>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="accountcreatedate" label="开通时间" min-width="160" sortable></el-table-column>
      <el-table-column align="center" label="操作" width="100" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <div class="operation-box">
            <el-text class="operate-btn-item" type="primary" @click="accountEditRef.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>

    <AccountEdit width="50%" ref="accountEditRef" @refresh="handleQuery" />
  </div>
</template>

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

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

const state = reactive({
  tableLoading: false,
  tableData: [],
  queryParams: {
    accountname: '',
    accounttype: '',
    accountlxrphone: '',
    accountgsm: ''
    // ['accountstatus']: 1,
  }
})

// components组件
const accountEditRef = ref(null)

// 角色组信息
const roleList = ref([])
provide('roleList', readonly(roleList))

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 resetButton = () => {
  state.queryParams = {
    accountname: '',
    accounttype: '',
    accountlxrphone: '',
    accountgsm: ''
  }
  handleQuery({initPageIndex: true})
}

// 默认信息
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 getList = async (query) => {
  state.tableLoading = true
  const {Total, Data} = await accountManageStore.list(query)
  paginaionConfig.total = Total
  state.tableData = Data
  state.tableLoading = false
}

onMounted(() => {
  handleQuery({initPageIndex: true})
  // 其他信息
  getRoleList()
})
</script>
