<template>
  <Dialog title="选择产品" v-model="dialogVisible" width="1200px">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="产品名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入产品名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="产品分类" prop="categoryId">
          <el-tree-select
            v-model="queryParams.categoryId"
            :data="productCategoryList"
            :props="defaultProps"
            check-strictly
            placeholder="请选择产品分类"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="产品名称" align="center" prop="name" />
        <el-table-column label="产品分类" align="center" prop="categoryName" />
        <el-table-column label="产品条码" align="center" prop="barCode" />
        <el-table-column label="产品单位" align="center" prop="unitName" />
        <el-table-column label="销售价格" align="center" prop="salePrice">
          <template #default="scope">
            <span>{{ erpPriceInputFormatter(scope.row.salePrice) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="selectionList.length === 0">
        确 定
      </el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { ProductApi,ProductVO }from '@/api/erp/product/product'
import {ProductCategoryApi ,ProductCategoryVO} from '@/api/erp/product/category'
import { erpPriceInputFormatter } from '@/utils'

defineOptions({ name: 'ProductEnableList' })

const dialogVisible = ref(false)
const loading = ref(true)
const total = ref(0)
const list = ref([])
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  categoryId: undefined,
  status: 0
})
const queryFormRef = ref()
const selectionList = ref<ProductVO[]>([])

const productCategoryList = ref([])
const defaultProps = {
  children: 'children',
  label: 'name',
  value: 'id'
}

const getList = async () => {
  loading.value = true
  try {
    const data = await ProductApi.getProductPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleSelectionChange = (selection: ProductVO[]) => {
  selectionList.value = selection
}

const open = async () => {
  dialogVisible.value = true
  await getList()
}

const submitForm = () => {
  emit('success', selectionList.value)
  dialogVisible.value = false
}

const emit = defineEmits(['success'])

onMounted(async () => {
  productCategoryList.value = await ProductCategoryApi.getProductCategoryList({})
})

defineExpose({ open })
</script>
