<script setup>
  import { ref } from 'vue';
  import { createIotRyDailyReport } from '@/api/ruiying';
  import Form from './components/form.vue';

  const formRef = ref(null);

  const formLoading = ref(false);

  const submitForm = async () => {
    if (!formRef.value) return;

    try {
      await formRef.value.formRef.validate();
      const form = formRef.value.form;

      if (Number(form.otherNptTime) && !form.otherNptReason) {
        uni.showToast({
          title: '其他非生产时间大于 0 时，必须填写其他非生产时间原因',
          icon: 'none',
        });
        return;
      }

      formLoading.value = true;

      const { createTime, ...other } = form;
      const data = { ...other, fillOrderCreateTime: createTime, projectClassification: '2' };

      await createIotRyDailyReport(data);

      formRef.value.loadDetail(form.id);

      uni.showToast({
        title: '修改成功',
        icon: 'success',
      });
    } catch (error) {
      console.log('error :>> ', error);
    } finally {
      formLoading.value = false;
    }
  };
</script>

<template>
  <view class="page">
    <scroll-view scroll-y="true" class="segmented-content">
      <Form ref="formRef" type="edit"></Form>
    </scroll-view>
    <view class="segmented-footer">
      <view class="footer-btn">
        <button
          :disabled="formLoading || formRef?.form.status !== 0"
          :loading="formLoading"
          class="mini-btn"
          type="primary"
          @click="submitForm">
          确定
        </button>
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
  @import '@/style/work-order-segmented.scss';
  .page {
    padding-bottom: 0;
  }

  .footer-btn {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding: 0 32px;
    height: 100%;

    gap: 0 32px;

    & > uni-button {
      margin: 0;
    }
  }

  :deep(.mini-btn) {
    height: 38px !important;
    font-size: 16px !important;
  }
</style>
