<template>
  <Dialog
    v-model="dialogVisible"
    :diaTitle="$t('工位时间维护.批量修改工位时间')"
    width="500"
    showFooter
  >
    <TableForm
      ref="tableFormRef"
      v-model:data="formData"
      :tableName="TABLE_NAME.BATCH_UPDATE_WORKSPACE_TIME"
      @change="tableFormChangeFun"
    />

    <template #footer>
      <div>
        <el-button @click="dialogVisible = false">{{ $t('通用.取消') }}</el-button>
        <el-button type="primary" :loading="loading" @click="confirmFun">{{ $t('通用.确定') }}</el-button>
      </div>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
import { ref, computed, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import Dialog from '@/components/common/dialog/index.vue';
import TableForm from '@/components/common/table/tableForm.vue';
import { TABLE_NAME } from '@/utils/enum/tableName';
import { batchUpdateWorkspaceExtraApi } from '@/api/project/node';
import { ElMessage } from 'element-plus';

const { t } = useI18n();

interface Props {
  visible: boolean;
  selectedRows: any[];
  currentPhase: any;
}

const props = withDefaults(defineProps<Props>(), {
  visible: false,
  selectedRows: () => [],
  currentPhase: () => null,
});

const emit = defineEmits(['update:visible', 'confirm']);

const dialogVisible = computed({
  get: () => props.visible,
  set: (val) => emit('update:visible', val),
});

const tableFormRef = ref();
const formData = ref<any>({});
const loading = ref(false);

const tableFormChangeFun = (_data: any) => {};

watch(
  () => props.visible,
  (val) => {
    if (val) {
      formData.value = {};
    }
  }
);

const confirmFun = async () => {
  if (props.selectedRows.length === 0) {
    ElMessage.warning(t('工位时间维护.请先选择要修改的工位'));
    return;
  }

  const data = formData.value;

  const workspaceExtras = props.selectedRows.map((row: any) => {
    const extras = [];
    if (data.structureRefinementCompletionTime) {
      extras.push({
        propertyName: 'structureRefinementCompletionTime',
        propertyValue: data.structureRefinementCompletionTime,
      });
    }
    if (data.stationUpgradeTime) {
      extras.push({
        propertyName: 'stationUpgradeTime',
        propertyValue: data.stationUpgradeTime,
      });
    }
    if (data.listReleaseTime) {
      extras.push({
        propertyName: 'listReleaseTime',
        propertyValue: data.listReleaseTime,
      });
    }
    return {
      workspaceUuid: row.workspaceUuid || row.uuid,
      extras,
    };
  });

  if (workspaceExtras.every((item: any) => item.extras.length === 0)) {
    ElMessage.warning(t('工位时间维护.请至少填写一个时间字段'));
    return;
  }

  const isDesignPhase = props.currentPhase?.label?.includes('设计') || false;
  const syncToTask = !isDesignPhase;

  loading.value = true;
  try {
    const res: any = await batchUpdateWorkspaceExtraApi({
      workspaceExtras,
      syncToTask,
    });
    if (res && res.code === 200) {
      ElMessage.success(t('工位时间维护.批量修改成功'));
      dialogVisible.value = false;
      emit('confirm');
    }
  } catch (error) {
    console.error('批量修改失败', error);
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped></style>
