<template>
  <ContentWrap>
    <Search
      :schema="schema"
      :model="queryParams"
      :expand="true"
      :expandDefault="false"
      expand-field="supplierName"
      :inline="true"
      @search="handleQuery"
      @reset="resetQuery"
    >
      <template #actionMore>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['contract:quality-notice:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          v-hasPermi="['contract:quality-notice:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>
        <el-button
          type="danger"
          plain
          @click="handleDeleteBatch"
          v-hasPermi="['contract:quality-notice:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" />
          批量删除
        </el-button>
      </template>
    </Search>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table ref="myTable" :data="tableData">
        <el-table-column
          v-for="column in tableColumns"
          :key="column.prop"
          :label="column.label"
          :prop="column.prop"
          align="center"
          :width="column.width"
        />
      </el-table>

      <!-- 操作列 -->
      <el-table-column label="操作" align="center" fixed="right" width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['contract:quality-notice:update']"
          >
            详情
          </el-button>
          <!--          <el-button-->
          <!--            link-->
          <!--            type="danger"-->
          <!--            @click="handleDelete(scope.row.id)"-->
          <!--            v-hasPermi="['contract:quality-notice:delete']"-->
          <!--          >-->
          <!--            删除-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

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

<script setup lang="ts">
import download from '@/utils/download'
import { QualityNoticeApi, QualityNoticeVO } from '@/api/contract/qualitynotice'
import QualityIssueForm from './QualityIssueForm.vue'
import { Search } from '@/components/Search'
import { FormSchema } from '@/types/form'
import { QualityIssueApi } from '@/api/qualityissue'

// 响应式的表头数组
const tableColumns = ref([])
// 表格数据
const tableData = ref([])


/** 质量通知单 列表 */
defineOptions({ name: 'QualityQualityissuee' })
// 搜索表单配置
const schema: FormSchema[] = [
  // 第一行：主要编号信息
  {
    field: 'id',
    label: '表单编号',
    component: 'Input',
    componentProps: {
      placeholder: '请输入送检单号',
      clearable: true,
      class: '!w-200px'
    }
  },
  {
    field: 'name',
    label: '表单名称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入批次号',
      clearable: true,
      class: '!w-200px'
    }
  },
  {
    field: 'status',
    label: '表单状态',
    component: 'Input',
    componentProps: {
      placeholder: '请输入批次号',
      clearable: true,
      class: '!w-200px'
    }
  }
]
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const currentRow = ref()
const loading = ref(true) // 列表的加载中
const list = ref<QualityNoticeVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  formName: '质量问题单',
  formId: 39,

})
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    //获取表头
    const data  = await QualityIssueApi.getHeaders()
    tableColumns.value = data.headers

    // const data = await QualityIssueApi.getPage(queryParams.value)
    // list.value = data.list
    // total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = (model) => {
  // 必须手动更新 queryParams！
  queryParams.value = { ...queryParams.value, ...model } // 合并数据
  getList()
}

/** 重置按钮操作 */
const resetQuery = (model) => {
  queryParams.value = { ...queryParams.value, ...model } // 合并数据
  getList()
}

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



/** 批量删除质量通知单 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await QualityNoticeApi.deleteQualityNoticeList(checkedIds.value)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: QualityNoticeVO[]) => {
  checkedIds.value = records.map((item) => item.id)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await QualityNoticeApi.exportQualityNotice(queryParams.value)
    download.excel(data, '质量通知单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

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