<template>
  <!-- 列表 -->
  <ContentWrap>
    <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-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column :label="t('MoldManagement.Mold.code')" align="center" prop="code" />
      <el-table-column :label="t('MoldManagement.Mold.name')" align="left" prop="name" />
      <!-- <el-table-column label="单位" align="center" prop="unitName" /> -->
      <el-table-column :label="t('MoldManagement.Mold.useTime')" align="center" prop="useTime" />
      <el-table-column :label="t('MoldManagement.Mold.inTime')" align="center" prop="inTime" :formatter="dateFormatter" />
      <el-table-column :label="t('MoldManagement.Mold.status')" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ERP_MOLD_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column :label="t('MoldManagement.Mold.machineName')" align="center" prop="machineName" />
      <!-- <el-table-column label="模具图片" align="center" prop="images" /> -->
      <el-table-column :label="t('MoldManagement.Mold.remark')" align="center" prop="remark" />
      <el-table-column :label="t('MoldManagement.Mold.isEnable')" align="center" prop="isEnable">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isEnable" />
        </template>
      </el-table-column>
      <el-table-column :label="t('MoldManagement.Mold.createTime')" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <el-table-column :label="t('MoldManagement.Mold.operate')" align="center" fixed="right" width="200px">
        <template #default="scope">
          <el-button link @click="openDetail(scope.row.id)">
            {{ t('MoldManagement.Mold.detail') }}
          </el-button>
          <!-- <el-button
            link
            type="success"
            @click="openRecordForm('create', scope.row.id, scope.row.brandId)"
            v-hasPermi="['mes:mold-record:update']"
          >
            维保
          </el-button> -->
          <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>
  <!-- 表单弹窗：添加/修改 -->
  <MoldForm ref="formRef" @success="getList" />

  <!-- 表单弹窗：添加/修改 -->
  <MoldRecordForm ref="recordFormRef" @success="getList" />
</template>
<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { MoldBrandApi } from '@/api/erp/mold'
import MoldForm from './MoldForm.vue'
import MoldRecordForm from "@/views/erp/mold/components/MoldRecordForm.vue";

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

const props = defineProps<{
  brandId?: number // 型号id（主表的关联字段）
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const exportLoading = ref(false) // 导出的加载中
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  brandId: undefined as unknown
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MoldBrandApi.getMoldPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.brandId,
  (val: number) => {
    if (!val) {
      return
    }
    queryParams.brandId = val
    handleQuery()
  },
  { immediate: true, deep: true }
)

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!props.brandId) {
    message.error('请选择一个模具型号')
    return
  }
  formRef.value.open(type, id, props.brandId)
}

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

/** 导出按钮操作 */
const handleExport = async () => {
  if (!props.brandId) {
    message.error('请选择一个模具型号')
    return
  }
  try {
    await message.exportConfirm()
    exportLoading.value = true
    const data = await MoldBrandApi.exportMold({
      ...queryParams,
      brandId: props.brandId
    })
    download.excel(data, '模具.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 添加/修改操作 */
const recordFormRef = ref()
const openRecordForm = (type: string, id?: number, brandId?: number) => {
  recordFormRef.value.open(type, id, brandId)
}
const { push } = useRouter()

const openDetail = (id: number) => {
  push({ name: 'ErpMoldDetail', params: { id } })
}

</script>
