<template>
  <div class="mesBusLog-container">
    <el-dialog v-model="state.isShowDialog" v-if="state.isShowDialog"width="90%" draggable="" :close-on-click-modal="false" >
      <template #header>
        <div style="color: #fff">
          <span>{{ props.title }}</span>
        </div>
      </template>
      <div style="width:100%;">
        <div>
          <el-table :data="tableData" ref="tableRef" style="width: 100%" v-loading="loading" tooltip-effect="light" row-key="id" border="" @selection-change="onSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column type="index" label="序号" width="55" align="center" />
            <el-table-column prop="produceWorkOrderNo" label="工单号" show-overflow-tooltip="" />
            <el-table-column prop="productCode" label="产品编码" show-overflow-tooltip="" />
            <el-table-column prop="productName" label="产品名称" show-overflow-tooltip="" />
            <el-table-column prop="startWorkDate" label="开工时间" show-overflow-tooltip="" />
            <el-table-column prop="endWorkDate" label="完工时间" show-overflow-tooltip="" />
            <el-table-column prop="num" label="批次数" show-overflow-tooltip="" />
            <el-table-column prop="processCode" label="工序编码" show-overflow-tooltip="" />
            <el-table-column prop="processName" label="工序名称" show-overflow-tooltip="" />
            <el-table-column prop="packCode" label="载具编码" show-overflow-tooltip="" />
            <el-table-column prop="packName" label="载具名称" show-overflow-tooltip="" />
            <el-table-column prop="equipmentCode" label="设备编码" show-overflow-tooltip="" />
            <el-table-column prop="equipmentName" label="设备名称" show-overflow-tooltip="" />
            <el-table-column prop="workShopCode" label="车间编码" show-overflow-tooltip="" />
            <el-table-column prop="workShopName" label="车间名称" show-overflow-tooltip="" />
            <el-table-column prop="factoryCode" label="工厂编码" show-overflow-tooltip="" />
            <el-table-column prop="factoryName" label="工厂名称" show-overflow-tooltip="" />
          </el-table>
          <!-- <el-pagination v-model:currentPage="tableParams.page" v-model:page-size="tableParams.pageSize" :total="tableParams.total" :page-sizes="[10, 20, 50, 100, 200, 500]" small="" background="" @size-change="handleSizeChange" @current-change="handleCurrentChange" layout="total, sizes, prev, pager, next, jumper" /> -->
        </div>
<!--        <div style="width:100%;display:flex;flex-direction: column;align-items: center;">-->
<!--          <div style="margin-top:10px;display:flex;justify-content:space-between;width:5%;">-->
<!--            <el-button type="primary" @click="submit">提 交</el-button>-->
<!--            <el-button type="primary" @click="closeDialog">关 闭</el-button>-->
<!--          </div>-->
<!--        </div>-->
      </div>
      <template #footer>
        <el-button @click="closeDialog">关 闭</el-button>
        <el-button type="primary" @click="submit">提 交</el-button>
      </template>
    </el-dialog>

  </div>
</template>
    <style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
    <script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import type { FormRules } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import {getFinishList, saveFLInsp} from "/@/api/mes/mesControlBoard";

//父级传递来的参数
var props = defineProps({
	title: {
		type: String,
		default: '',
	},
  produceWorkOrderNo: {
    type: String,
    default: '',
  },
  processCode: {
    type: String,
    default: '',
  },
  factoryCode: {
    type: String,
    default: '',
  },
  equipmentCode: {
    type: String,
    default: '',
  },
});

//自行添加其他规则
const rules = ref<FormRules>({
	busCode: [{ required: true, message: '请输入业务编码！', trigger: 'blur' }],
});

//父级传递来的函数，用于回调
const emit = defineEmits(['reloadTable']);
const ruleFormRef = ref();
const tableData = ref<any>([]);
const loading = ref(false);
const tableParams = ref({
	page: 1,
	pageSize: 10,
	total: 0,
});
const handleSizeChange = (val: number) => {
	tableParams.value.pageSize = val;
	handleQuery();
};
// 改变页码序号
const handleCurrentChange = (val: number) => {
	tableParams.value.page = val;
	handleQuery();
};
const handleQuery = () => {};

const ruleForm = ref<any>({});
const state = reactive({
	isShowDialog: false,
});

// 页面加载时
onMounted(() => {});

// 打开弹窗
const openDialog = async (row: any) => {
	// 改用detail获取最新数据来编辑
	// let rowData = JSON.parse(JSON.stringify(row));
	state.isShowDialog = true;
  // 获取报工记录
  tableData.value = [];
  getWorkRecord();
};
// 选择行变更
const tableRef = ref();
const selectionList = ref([]);
const onSelectionChange = () => {
  selectionList.value = tableRef.value.getSelectionRows();
}
const getWorkRecord = async ()=>{
  // if(!props.produceWorkOrderNo) return
  let res = await getFinishList({
    page: 1,
    pageSize: 9999,
    factoryCode:props.factoryCode,
    equipmentCode:props.equipmentCode,
    processCode:props.processCode,
  })
  tableData.value = res.data?.result?.items || [];
}

// 关闭弹窗
const closeDialog = () => {
	emit('reloadTable');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};


const submit = async () => {
	// ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
	// 	if (isValid) {
	// 	}
	// });
  if(selectionList.value.length ==0) return ElMessage.warning('请选择报工明细后操作');
  await saveFLInsp({
    "workRecordId": selectionList.value.map((item: any) => item.id),
    "inspType": 1,
    "isFirstInsp": true
  }).then(res=>{
    ElMessage.success('提交成功');
    closeDialog();

  }).catch(()=>{

  })
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>




