<template>
  <Dialog
    v-model="dialogFormVisible"
    title="线库管理"
    width="600"
    class="line-library-management-dialog"
  >
    <el-table
      :data="list"
      style="width: 100%"
      :header-cell-style="{ backgroundColor: '#EBF1FF', color: '#0D162A', padding: '13px 0' }"
      v-loading="loading"
    >
      <el-table-column type="index" width="50" align="center" />
      <el-table-column prop="laneName" label="巷道名称" align="center" />
      <el-table-column prop="laneMsg" label="巷道说明" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        size="small"
        background
        layout="prev, pager, next"
        :total="total"
        class="mt-4"
      />
    </div>
  </Dialog>
</template>

<script setup>
import { reactive, ref } from 'vue'
import * as MapApi from '@/api/map/map'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

const dialogFormVisible = ref(false) //列表的
const message = useMessage() // 消息弹窗
const props = defineProps({
  positionMapId: {
    type: String,
    default: () => ''
  }
})

const open = () => {
  dialogFormVisible.value = true
  queryParams.positionMapId = props.positionMapId
  getLineLibraryList()
}

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  positionMapId: ''
})

const getLineLibraryList = async () => {
  loading.value = true
  try {
    const data = await MapApi.getWareHouseLanePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleDelete = async (id) => {
  try {
    // 删除的二次确认
    await message.delConfirm('请确认是否删除该物料区域？')
    // 发起删除
    await MapApi.deleteWareLaneArea(id)
    message.success('删除成功')
    // 刷新列表
    await getLineLibraryList()
  } catch {}
}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>

<style lang="scss">
.line-library-management-dialog {
  padding: 0px;

  .pagination {
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
}
</style>
