<template>
  <div class="mod-prod">
    <!-- 使用avue-crud构建CRUD表格 -->
    <avue-crud
      ref="crudRef"
      :page="page"
      :data="dataList"
      :option="tableOption"
      :permission="permission"
      @search-change="onSearch"
      @on-load="getDataList"
    >
      <!-- 自定义属性值列内容 -->
      <template #prodPropValues="scope">
        <el-tag v-for="item in scope.row.prodPropValues" :key="item.valueId">
          {{ item.propValue }}
        </el-tag>
      </template>

      <!-- 自定义左侧菜单按钮 -->
      <template #menu-left>
        <el-button
          v-if="isAuth('shop:pickAddr:save')"
          type="primary"
          icon="el-icon-plus"
          @click.stop="onAddOrUpdate()"
        >
          新增
        </el-button>
      </template>

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

        <el-button
          v-if="isAuth('prod:spec:delete')"
          type="danger"
          icon="el-icon-delete"
          @click.stop="onDelete(scope.row.propId)"
        >
          删除
        </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/prod/spec.js'
import { ref, reactive, nextTick } from 'vue'

// 权限控制对象
const permission = ref({
  delBtn: isAuth('prod:prod:delete') // 删除按钮权限检查
})

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

// 表格加载状态
const dataListLoading = ref(false)

// 多选框选中的数据
const dataListSelections = ref([])

// 分页信息
const page = reactive({
  total: 0, // 总记录数
  currentPage: 1, // 当前页码
  pageSize: 10 // 每页大小
})

/**
 * 获取数据列表方法
 */
const getDataList = (pageParam, params, done) => {
  dataListLoading.value = true
  http({
    url: http.adornUrl('/prod/spec/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
      dataListLoading.value = false
      if (done) done()
    })
}

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

/**
 * 新增或编辑产品规格
 * @param val - 如果存在则为编辑模式，否则为新增模式
 */
const onAddOrUpdate = (val) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(val)
  })
}

/**
 * 删除产品规格
 * @param id - 规格ID，如果不存在则表示批量删除
 */
const onDelete = (id) => {
  const ids = id ? [id] : dataListSelections.value?.map(item => item.propId)
  ElMessageBox.confirm(`确定进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      http({
        url: http.adornUrl(`/prod/spec/${ids}`),
        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)
}
</script>

<style scoped lang="scss">
:deep(.prop-value) {
  display: inline-block;
  margin: 0 3px 3px 0;
}
</style>
