<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="80%">
    <ContentWrap class="borderxx">
      <el-form class="-mb-15px" :model="queryReqVo" :inline="true" label-width="108px">
        <el-form-item label=" 物料编码：" label-width="150px">
          <el-input v-model="queryReqVo.materialCode" placeholder="物料编码" class="!w-240px"  />
        </el-form-item>
        <el-form-item label=" 物料名称：" label-width="150px">
          <el-input v-model="queryReqVo.materialName" placeholder="物料名称" class="!w-240px"  />
        </el-form-item>
        <el-form-item label=" 子项目编号：" label-width="150px">
          <el-input v-model="queryReqVo.projectSubCode" placeholder="子项目编号" class="!w-240px"  />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery" type="primary" >
            <Icon icon="ep:search" class="mr-5px" /> 搜索
          </el-button><el-button @click="resetQuery" >
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <el-card class="hl-table">
      <el-table ref="multipleTable" :data="list" v-loading="formLoading" class="hl-table" @selection-change="handleSelectionChange" :reserve-selection="true" :row-key="getRowKeys" min-width="1800" @row-click="clickRow">
        <el-table-column type="selection" width="55" :reserve-selection="true" :selectable="row=>chkboxEnable" />
        <!-- <el-table-column prop="no" label="序号" min-width="120" align="center" /> -->
        <el-table-column label="序号" type="index" min-width="120" align="center" />
        <el-table-column prop="materialCode" label="物料编码" min-width="120" align="center" />
        <el-table-column prop="materialName" label="物料名称" min-width="120" align="center" />
        <el-table-column prop="materialType" label="物料类型" min-width="120" align="center" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HELI_MATERIAL_TYPE" :value="scope.row.materialType" />
        </template>
      </el-table-column>
        <el-table-column prop="spec" label="规格/型号" min-width="140" align="center" />
        <el-table-column prop="amount" label="需求数量" min-width="120" align="center" />
        <el-table-column prop="unit" label="系统单位" min-width="120" align="center" >
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_MATERIAL_UNIT" :value="scope.row.unit" />
          </template>
        </el-table-column>
        <el-table-column prop="projectSubCode" label="子项目编号" min-width="120" align="center" />
      </el-table>
      <!-- 分页 -->
      <Pagination :total="total" v-model:page="queryReqVo.pageNo" v-model:limit="queryReqVo.pageSize" @pagination="handleQuery" />
    </el-card>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import * as MaterialPlanBoomApi from '@/api/heli/materialplanboom'
import { ElTable } from 'element-plus'
import * as PlanTaskBomApi from "@/api/heli/plantaskbom";

const chkboxEnable = ref(true)

const { t } = useI18n() // 国际化

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const planIdInit = ref()

const multipleTable = ref<InstanceType<typeof ElTable>>()
const multipleSelection = ref([])
const handleSelectionChange = (val: []) => {
  multipleSelection.value = val
}

const clickRow = (row: any) => {
  if (chkboxEnable.value) {
    if (row) {
      multipleTable.value!.toggleRowSelection(row, undefined)
    } else {
      multipleTable.value!.clearSelection()
    }
  }
}
//指定key值,数据更新之后保留之前选中的数据
const getRowKeys = (row) => {
  return row.id
}

const queryReqVo: any = reactive({
  pageNo: 1,
  pageSize: 10,
  projectSubCode:undefined,
  materialName:undefined,
  materialCode:undefined,
  taskId:planIdInit.value
})
/** 搜索按钮操作 */
const handleQuery = async () => {
  const data = await PlanTaskBomApi.getMaterialPlanTaskBom(queryReqVo);
  list.value = data.list
  total.value = data.total
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryReqVo.pageNo = 1
  queryReqVo.pageSize = 10
  queryReqVo.projectSubCode = ''
  queryReqVo.materialName = ''
  queryReqVo.materialCode = ''
  handleQuery()
}
/** 打开弹窗 */

const open = async (planId: number) => {
  dialogVisible.value = true
  dialogTitle.value = '物料列表'
  planIdInit.value = planId
  queryReqVo.taskId = planId

  formLoading.value = true
  const data = await PlanTaskBomApi.getMaterialPlanTaskBom(queryReqVo)
  list.value = data.list
  total.value = data.total

  formLoading.value = false
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 提交请求
  formLoading.value = true
  try {
    dialogVisible.value = false
    // 发送操作成功的事件
    // multipleSelection.value.forEach(item=> item.boomAmount = item.amount)
    emit('success', multipleSelection.value)
  } finally {
    formLoading.value = false
  }
}
</script>
