<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item :label="t('MoldManagement.MoldBrand.code')" prop="code">
        <el-input
          v-model="queryParams.code"
          :placeholder="t('MoldManagement.MoldBrand.placeholderCode')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item :label="t('MoldManagement.MoldBrand.name')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="t('MoldManagement.MoldBrand.placeholderName')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item :label="t('MoldManagement.MoldBrand.orgType')" prop="orgType">
        <el-select
          v-model="queryParams.orgType"
          :placeholder="t('MoldManagement.MoldBrand.placeholderOrgType')"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in orgTypeOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="维保模式" prop="maintainType">
        <el-select
          v-model="queryParams.maintainType"
          placeholder="请选择维保模式"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.ERP_MAINTAIN_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> {{ t('common.query') }}</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> {{ t('common.reset') }}</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['erp:mold-brand:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> {{ t('action.add') }}
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['erp:mold-brand:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      ref="tableRef"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      row-key="id"
      highlight-current-row
      @current-change="handleCurrentChange"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" fixed="left" reserve-selection /> -->
      <el-table-column :label="t('MoldManagement.MoldBrand.code')" align="center" prop="code" />
      <el-table-column :label="t('MoldManagement.MoldBrand.name')" align="left" prop="name" />
      <el-table-column :label="t('MoldManagement.MoldBrand.moldType')" align="center" prop="moldType" />
      <el-table-column :label="t('MoldManagement.MoldBrand.orgType')" align="center" prop="orgType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MES_ORG_TYPE" :value="scope.row.orgType" />
        </template>
      </el-table-column>
      <el-table-column :label="t('MoldManagement.MoldBrand.useTime')" align="center" prop="useTime" />
      <!-- <el-table-column label="维保模式" align="center" prop="maintainType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ERP_MAINTAIN_TYPE" :value="scope.row.maintainType" />
        </template>
      </el-table-column>
      <el-table-column label="维保周期" align="center" prop="maintainTime" /> -->
      <el-table-column :label="t('MoldManagement.MoldBrand.moldSize')" align="center" prop="moldSize" />
      <el-table-column :label="t('MoldManagement.MoldBrand.remark')" align="center" prop="remark" />
      <el-table-column
        :label="t('MoldManagement.MoldBrand.createTime')"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column :label="t('MoldManagement.MoldBrand.operate')" align="center" fixed="right" width="150px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:mold-brand:update']"
          >
            {{ t('action.edit') }}
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:mold-brand:delete']"
          >
            {{ t('action.del') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <MoldBrandForm ref="formRef" @success="getList" />
  <!-- 子表的列表 -->
  <!-- <ContentWrap v-if="currentBrandId">
    <div class="mb-10px text-14px font-600">
      {{ currentBrandDisplay }}
    </div>
    <el-tabs model-value="mold">
      <el-tab-pane :label="t('MoldManagement.MoldBrand.tabMold')" name="mold">
        <MoldList :brand-id="currentBrandId" />
      </el-tab-pane> -->
      <!-- <el-tab-pane label="产品" name="moldBrandProduct">
        <MoldBrandProductList :brand-id="currentBrandId" />
      </el-tab-pane> -->
    <!-- </el-tabs>
  </ContentWrap> -->
  <!-- <ContentWrap v-else>
    <el-empty :description="t('MoldManagement.MoldBrand.emptyTip')" />
  </ContentWrap> -->
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { MoldBrandApi, MoldBrandVO } from '@/api/erp/mold'
import MoldBrandForm from './MoldBrandForm.vue'
import MoldList from './components/MoldList.vue'
import MoldBrandProductList from './components/MoldBrandProductList.vue'

/** 模具型号 列表 */
defineOptions({ name: 'MoldBrand' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
const list = ref<MoldBrandVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: undefined,
  name: undefined,
  orgType: undefined,
  moldType: undefined,
  productId: undefined,
  useTime: [],
  maintainType: undefined,
  maintainTime: [],
  moldSize: undefined,
  remark: undefined,
  isEnable: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const orgTypeOptions = computed(() => getStrDictOptions(DICT_TYPE.MES_ORG_TYPE))

const tableRef = ref()
const selectedIds = ref<Array<number | string>>([])
const handleSelectionChange = (rows: any[]) => {
  selectedIds.value = rows?.map((row) => row.id).filter((id) => id !== undefined && id !== null && id !== '') ?? []
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MoldBrandApi.getMoldBrandPage(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 formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MoldBrandApi.deleteMoldBrand(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await MoldBrandApi.exportMoldBrand({
      ...queryParams,
      ...(selectedIds.value.length ? { ids: selectedIds.value.join(',') } : {})
    })
    download.excel(data, '模具型号.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 选中行操作 */
const currentRow = ref<MoldBrandVO | null>(null) // 选中行
const currentBrandId = computed(() => currentRow.value?.id)
const currentBrandDisplay = computed(() => {
  if (!currentRow.value) return ''
  return `${currentRow.value.name}-${currentRow.value.code}`
})
const handleCurrentChange = (row: MoldBrandVO | null) => {
  currentRow.value = row
}

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