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

      <!-- 按钮组件 -->
      <all-button
        :buttonType="['add', '', 'query', 'refresh']"
        @addButton="accountEditRef.openDialog()"
        @queryButton="handleQuery({initPageIndex: true})"
        @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="tableCelldbl">
      <el-table-column align="center" prop="yhinfoid" label="用户ID" min-width="150" sortable></el-table-column>
      <!-- <el-table-column align="center" prop="yhinfoisrz" label="用户是否通过验证" min-width="150">
        <template #default="scope">
          <el-tag v-if="scope.row[scope.column.property] == 0">已通过</el-tag>
          <el-tag v-if="scope.row[scope.column.property] == 1">未通过</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column align="center" prop="yhinfocustomerid" label="关联客户ID" min-width="150"></el-table-column>
      <el-table-column align="center" prop="yhinfoproductid" label="关联产品ID" 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="yhinfoproductname" label="产品名称" min-width="150"></el-table-column>
      <el-table-column align="center" prop="yhinfoqyid" label="关联企业ID" min-width="150"></el-table-column>
      <el-table-column align="center" prop="yhinfoqym" label="关联企业码" min-width="150"></el-table-column>

      <el-table-column align="center" prop="yhinfoproductorderid" label="关联订单ID" min-width="150"></el-table-column>
      <el-table-column align="center" prop="yhinfozh" label="用户帐号" min-width="150"></el-table-column>
      <el-table-column align="center" prop="yhinfoboxid" label="关联路由器设备" min-width="150"></el-table-column>
      <!-- <el-table-column align="center" prop="yhinfoisrz" label="用户是否通过验证" min-width="150"></el-table-column> -->
      <el-table-column align="center" prop="yhinfotel" label="用户手机号" min-width="150"></el-table-column>
      <el-table-column align="center" prop="yhinfostardate" label="开始时间" min-width="150"></el-table-column>
      <el-table-column align="center" prop="yhinfoenddate" label="结束时间" min-width="150"></el-table-column>
      <el-table-column align="center" prop="yhinfocreatedate" label="用户创建时间" min-width="150"></el-table-column>
      <!-- <el-table-column align="center" prop="customersfky" label="客户是否可用" min-width="150"></el-table-column> -->

      <!-- <el-table-column align="center" :width="fitWidth" label="操作" 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>
            <el-text class="operate-btn-item" type="danger" @click="handleDictDel(scope.row)">删除</el-text>
          </div>
        </template>
      </el-table-column> -->
      <el-table-column align="center" :width="fitWidth" label="操作" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <div class="operation-box">
            <el-text class="operate-btn-item" type="primary" @click="handleDictInfo(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>

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

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

const {proxy} = getCurrentInstance()
const systemManageStore = useSystemManageStore()
const {paginaionConfig, handleSizeChange, handleCurrentChange} = usePagination(handleQuery)
const {fitWidth} = useFitColumn()
// 接口封装文件
const allStoreApiRequest = allStoreApi()

const state = reactive({
  tableLoading: false,
  tableData: [],
  queryParams: {
    'yhinfoid.value': ''
  }
})

// components组件
const accountEditRef = ref(null)
const router = useRouter()
// 查看详情
const handleDictInfo = async (query) => {
  router.push({path: '/peoplesmanage/userinfo', query: {yhinfoid: query.yhinfoid}})
}
// 角色组信息
const roleList = ref([])
provide('roleList', readonly(roleList))

// 重置搜索项
const resetButton = () => {
  state.queryParams = {}
  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 allStoreApiRequest.list({pagevalue: 301, ...query})
  paginaionConfig.total = Total
  state.tableData = Data
  state.tableLoading = false
}

/** 删除 */
const handleDictDel = async (query) => {
  try {
    let params = JSON.parse(JSON.stringify(query))
    params['productid.value'] = state.editView.productid
    params['productid'] = state.editView.productid
    await proxy.$modal.confirm('请问确认删除吗？')
    await allStoreApi.del(params)
    proxy.$modal.msgSuccess('删除成功！')
    handleQuery()
  } catch (error) {}
}

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