<template>
  <div class="comp-industry">
    <div class="content">
      <div class="tips">
        <PlanningInformation data="工业设计" />
      </div>
      <div class="form">
        <TableForm
          ref="TableFormRef"
          tableName="SIMULATION_TASK_DEMAND_INDUSTRIAL_CREATE"
          v-model:data="editFormInfo"
          :formAttrs="{
            projectId: {
              auth: false,
            },
            workspaceCode: {
              filterable: true,
            },
            endTime: {
              remarkText: endTimeRemark,
            },
            attachments: {
              remarkText: attachmentRemark,
            },
          }"
          :hideKeys="hideKeys"
          :colNum="3"
          @change="changeFun"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import PlanningInformation from '@/tenants/lyric/views/task/components/planningInformation.vue';
import TableForm from '@/components/common/table/tableForm.vue';
import {
  setPhaseListByProjectId,
  setMachineListByPhaseId,
  setWorkspaceListByMachineId,
} from './lib';

const emits = defineEmits(['submit']);
const TableFormRef = ref();
const hideKeys = ref([
  'downAttachments',
  'simulationPurpose',
  'animationPurpose',
  'fluidPurpose',
  'robotBrand',
  'axis',
  'beatDemand',
  'robotNum',
  'robotNum6',
  'isMoldMaking',
  'materialNo',
  'designExp',
]);
const endTimeRemark = ref('【需求时间】请参考发图时间或客户评审时间');
const attachmentRemark = ref('必须上传附件！');

const editFormInfo = ref<any>({});

const changeFun = async (cbData: any) => {
  const { key, data } = cbData;
  if (['projectId'].includes(key)) {
    await setPhaseListByProjectId({
      nodeId: data.projectId,
      formData: editFormInfo.value,
      ref: TableFormRef.value,
      cbData,
    });
  }
  if (['projectId', 'phaseId'].includes(key)) {
    await setMachineListByPhaseId({
      nodeId: editFormInfo.value.phaseId,
      formData: editFormInfo.value,
      ref: TableFormRef.value,
      cbData,
    });
  }
  if (['projectId', 'phaseId', 'machineId'].includes(key)) {
    await setWorkspaceListByMachineId({
      nodeId: editFormInfo.value.machineId,
      formData: editFormInfo.value,
      ref: TableFormRef.value,
      cbData,
    });
  }
};

const submitFun = async () => {
  if (await TableFormRef.value.validateFun()) {
    emits('submit', editFormInfo.value);
  }
};

defineExpose({
  submitFun,
});
</script>

<style lang="scss" scoped></style>
