<template>
  <div class="app-container lyq-manage">
    <!-- 搜索 -->
    <el-form :model="state.queryParams" ref="queryRef" :inline="true">
      <el-form-item label="ip地址ID" prop="iplibraryid">
        <el-input v-model="state.queryParams['iplibraryid.value']" placeholder="ip地址ID" 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="iplibraryid" label="ip流水号" min-width="150"></el-table-column>
      <el-table-column align="center" prop="iplibraryname" label="地址池名称" min-width="150"></el-table-column>
      <el-table-column align="center" prop="iplibraryaddress" label="ip地址" min-width="150"></el-table-column>
      <el-table-column align="center" prop="iplibrarystartaddress" label="开始段" min-width="150"></el-table-column>
      <el-table-column align="center" prop="iplibraryendaddress" label="结束段" min-width="150"></el-table-column>
      <el-table-column align="center" prop="iplibrarybz" label="ip备注" min-width="150"></el-table-column>

      <!-- <el-table-column align="center" prop="iplibrarystatus" label="ip状态" min-width="150"></el-table-column> -->
      <el-table-column align="center" prop="iplibrarystatus" label="ip状态" min-width="150">
        <template #default="scope">
          <el-tag type="success" v-if="scope.row[scope.column.property] == 1">已分配</el-tag>
          <el-tag v-if="scope.row[scope.column.property] == 0">未分配</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="iplibrarcreatedate" label="ip创建时间" 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" v-if="scope.row.iplibrarystatus === 0">
            <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>

    <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 allStoreApi from '@store/modules/allStoreApi'
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 tableCelldbl from '@/hooks/tableCelldbl'

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

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

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

// 角色组信息
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 params = {
    pagevalue: 340,
    ...query
  }
  const {Total, Data} = await alleApi.list(params)
  paginaionConfig.total = Total
  state.tableData = Data
  state.tableLoading = false
}

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

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