<template>
  <div class="mod-pickAddr">
    <!-- 使用avue-crud构建CRUD表格 -->
    <avue-crud
      ref="crudRef"
      :page="page"
      :data="dataList"
      :option="tableOption"
      :permission="permission"
      @search-change="onSearch"
      @selection-change="selectionChange"
      @on-load="getDataList"
    >
      <!-- 自定义左侧菜单按钮 -->
      <template #menu-left>
        <el-button
          v-if="isAuth('shop:pickAddr:save')"
          type="primary"
          icon="el-icon-plus"
          @click.stop="onAddOrUpdate()"
        >
          新增
        </el-button>

        <el-button
          v-if="isAuth('shop:pickAddr:delete')"
          type="danger"
          :disabled="dataListSelections.length <= 0"
          @click="onDelete()"
        >
          批量删除
        </el-button>
      </template>

      <!-- 自定义行操作菜单 -->
      <template #menu="scope">
        <el-button
          v-if="isAuth('shop:pickAddr:update')"
          type="primary"
          icon="el-icon-edit"
          @click.stop="onAddOrUpdate(scope.row.addrId)"
        >
          编辑
        </el-button>

        <el-button
          v-if="isAuth('shop:pickAddr:delete')"
          type="danger"
          icon="el-icon-delete"
          @click.stop="onDelete(scope.row.addrId)"
        >
          删除
        </el-button>
      </template>
    </avue-crud>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="getDataList"
    />
  </div>
</template>

<script setup>
import { isAuth } from '@/utils'
import { ElMessage, ElMessageBox } from 'element-plus'
import AddOrUpdate from './add-or-update.vue'
import { tableOption } from '@/crud/shop/pickAddr.js'
import { ref, reactive, nextTick } from 'vue'

// 定义权限控制对象
const permission = {
  delBtn: isAuth('prod:prod:delete')
}

// 数据列表引用
const dataList = ref([])

// 已选中的数据项
const dataListSelections = ref([])

// 分页信息
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})

/**
 * 获取数据列表方法
 */
const getDataList = (pageParam, params, done) => {
  http({
    url: http.adornUrl('/shop/pickAddr/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.pageSize : pageParam.pageSize
        },
        params
      )
    )
  })
    .then(({ data }) => {
      dataList.value = data.records
      page.total = data.total
      if (done) done()
    })
}

// 控制添加或更新对话框显示/隐藏
const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)

/**
 * 新增 / 修改
 * @param id - 如果存在则为编辑模式，否则为新增模式
 */
const onAddOrUpdate = (id) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(id)
  })
}

/**
 * 删除单个或多个自提点地址
 * @param id - 单个自提点地址ID，若未提供则执行批量删除
 */
const onDelete = (id) => {
  const ids = id ? [id] : dataListSelections.value?.map(item => item.addrId)
  ElMessageBox.confirm(
    '确定进行删除操作?', '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
    .then(() => {
      http({
        url: http.adornUrl('/shop/pickAddr'),
        method: 'delete',
        data: http.adornData(ids, false)
      })
        .then(() => {
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              getDataList(page)
            }
          })
        })
    })
    .catch(() => {})
}

/**
 * 条件查询触发的数据获取
 * @param params - 查询参数
 * @param done - 完成回调
 */
const onSearch = (params, done) => {
  getDataList(page, params, done)
}

/**
 * 多选变化时触发
 * @param val - 当前选择的数据项数组
 */
const selectionChange = (val) => {
  dataListSelections.value = val
}
</script>
