<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="140px"
    >
      <el-form-item label="物资规格种类" prop="propertyName">
        <el-input
          v-model="queryParams.propertyName"
          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:property:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['iscs:property:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
    <!-- 列表 -->
    <ContentWrap>
      <el-table
        v-loading="loading"
        :data="propertyList"
        @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="propertyName" />
        <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:property:update']"
            >
              修改
            </el-button>
            <el-button
              link
              type="primary"
              @click="handleValue(scope.row)"
              v-hasPermi="['iscs:property:update']"
            >
              规格设置
            </el-button>
            <el-button
              link
              type="danger"
              @click="handleDelete(scope.row)"
              v-hasPermi="['iscs:property: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>

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

<script lang="ts" setup>

import { dateFormatter } from '@/utils/formatTime'
import * as PropertyApi from '@/api/material/standard/index'
import PropertyForm from './PropertyForm.vue'

defineOptions({ name: 'MaterialProperty' })

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

// 数据定义
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const propertyList = ref([]) // 列表的数据
const ids = ref<number[]>([]) // 选中的数据
const multiple = ref(true) // 非多个禁用

// 查询参数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  propertyName: undefined
})

/** 查询物资规格列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PropertyApi.PropertyPage(queryParams)
    propertyList.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 handleValue = (row: any) => {
  router.push({
    name: 'propertyValue',
    query: {
      id: row.id,
      propertyName: row.propertyName
    }
  })
}

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

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