<template>
  <Dialog
    v-model="dialogFormVisible"
    title="线库管理"
    width="660"
    class="line-library-management-dialog"
    @close="dialogClose"
  >
    <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" label="序号" width="80" align="center" />
      <el-table-column prop="laneName" label="线库名称" align="center" show-overflow-tooltip />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button size="small" type="primary" @click="handleEdit(scope.row)"> 编辑 </el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pagination">
      <Pagination
        class="mt-4"
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getLineLibraryList"
      />
    </div>
  </Dialog>

  <el-dialog v-model="editDialogFormVisible" title="编辑" width="400">
    <el-form :model="editForm" :rules="editRules" ref="editFormEl">
      <el-form-item label="线库名称" prop="laneName" required>
        <el-input v-model="editForm.laneName" maxlength="30" show-word-limit />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="editSubmit"> 确定 </el-button>
      </div>
    </template>
  </el-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 emit = defineEmits([
  'addEventListener',
  'lineLibraryManagementDelete',
  'lineLibraryManagementEdit'
])
const dialogClose = () => {
  emit('addEventListener')
}

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 (item) => {
  try {
    // 删除的二次确认
    await message.delConfirm('请确认是否删除该线库？')
    // 发起删除
    await MapApi.deleteWareLaneArea(item.id)
    message.success('删除成功')
    // 刷新列表
    await getLineLibraryList()
    emit('lineLibraryManagementDelete', item.mapItemIds)
  } catch {}
}

//编辑
const editForm = ref({
  laneName: ''
})
const editDialogFormVisible = ref(false)
const handleEdit = (row) => {
  editDialogFormVisible.value = true
  editForm.value = row
}
const editRules = reactive({
  laneName: [{ required: true, message: '请输入线库名称', trigger: 'blur' }]
})
const editFormEl = ref()
const editSubmit = async () => {
  await editFormEl.value.validate(async (valid, fields) => {
    if (valid) {
      await MapApi.createOrEditOrDelHouseLane(editForm.value)
      await getLineLibraryList()
      editDialogFormVisible.value = false
      message.success('编辑成功')
      emit('lineLibraryManagementEdit', editForm.value)
    }
  })
}

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

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

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