<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="物资规格种类" prop="propertyName">
        <el-input
          disabled
          v-model="queryParams.propertyName"
          placeholder="请输入物资规格种类"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="物资规格" prop="valueName">
        <el-input
          v-model="queryParams.valueName"
          placeholder="请输入物资规格"
          clearable
          @keyup.enter="handleQuery"
          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-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['iscs:propvalue:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['iscs:propvalue:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
        <el-button type="warning" plain @click="goBack">
          <Icon icon="ep:back" class="mr-5px" /> 返回
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
    <!-- 列表 -->
    <ContentWrap>
      <el-table
        v-loading="loading"
        :data="propertyValueList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="编号" align="center" prop="id" />
        <el-table-column label="物资规格" align="center" prop="valueName" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" />
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="openForm('update', scope.row.id)"
              v-hasPermi="['iscs:propvalue:update']"
            >
              修改
            </el-button>
            <el-button
              link
              type="danger"
              @click="handleDelete(scope.row)"
              v-hasPermi="['iscs:propvalue:delete']"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        v-model:total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

    <!-- 表单弹窗：添加/修改 -->
    <PropertyValueForm ref="formRef" @success="getList" />

</template>

<script lang="ts" setup>

import { dateFormatter } from '@/utils/formatTime'
import * as PropertyValueApi from '@/api/material/standard/propertyValue'
import PropertyValueForm from './PropertyValueForm.vue'

defineOptions({ name: 'MaterialPropertyValue' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const router = useRouter()
const route = useRoute()

// 数据定义
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const propertyValueList = ref([]) // 列表的数据
const ids = ref<number[]>([]) // 选中的数据
const multiple = ref(true) // 非多个禁用
const queryFormRef=ref()
// 查询参数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  propertyName: '',
  valueName: '',
  propertyId: undefined
})

/** 查询物资规格值列表 */
const getList = async () => {
  loading.value = true
  try {
    queryParams.propertyId = route.query.id as string
    queryParams.propertyName = route.query.propertyName as string
    const data = await PropertyValueApi.PropertyValuePage(queryParams)
    propertyValueList.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: any[]) => {
  ids.value = selection.map(item => item.id)
  multiple.value = !selection.length
}

/** 新增/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 返回按钮操作 */
const goBack = () => {
  router.push('/material/material/standard')
}

/** 删除按钮操作 */
const handleDelete = async (row?: any) => {
  const recordIds = row?.id || ids.value
  try {
    await message.confirm('是否确认删除所选数据项？')
    await PropertyValueApi.deletePropertyValue(recordIds)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>
