<script setup>
import { ref, defineProps, watch, inject } from "vue";
import useSearchParam from "@/hooks/useSearchParam";
import dayjs from "dayjs";
import {
  postOperationRecord,
  postOperationCompany,
  updateOperationCompany,
} from "@/apis/operation";
import * as yup from "yup";
import "yup-phone-lite";
import useFormErrorMessage from "@/hooks/useFormErrorMessage";
import Select from "@/components/customUI/Select.vue";

const FILE_BASEURL = import.meta.env.VITE_FILE_API_BASEURL;

const props = defineProps({
  editRecord: Object,
});
const { openToast } = inject("app_toast");
const { model_data, updateEditRecord, search } = inject("operation_modal") || {
  model_data: [],
  updateEditRecord: null,
  search: null,
};

const { searchParams } = useSearchParam();

const formState = ref([
  {
    formId: "",
    work_type: "",
    work_type_name: "",
    fix_do: "",
    fix_do_code: "",
    fix_firm: "",
    status: "",
    work_person_id: "",
    start_time: "",
    finish_time: "",
    notice: "",
    description: "",
    lorf: [],
  },
  {
    formId: "",
    work_type: "",
    work_type_name: "",
    fix_do: "",
    fix_do_code: "",
    fix_firm: "",
    status: "",
    work_person_id: "",
    start_time: "",
    finish_time: "",
    notice: "",
    description: "",
    lorf: [],
  },
  {
    contact_person: "",
    email: "",
    name: "",
    phone: "",
    remark: "",
    tax_id_number: "",
    city: "",
    address: "",
  },
]);

let companySchema = yup.object({
  name: yup.string().required("必填"),
  contact_person: yup.string().nullable(true),
  email: yup.string().email().nullable(true),
  phone: yup.string().phone("TW", "請輸入正確電話號碼格式").nullable(true),
  city: yup.string().nullable(true),
  address: yup.string().nullable(true),
  tax_id_number: yup.string().nullable(true),
  remark: yup.string().nullable(true),
});

let maintainSchema = yup.object({
  formId: yup.string().required("必填"),
  work_type: yup.string().required("必填"),
  fix_do: yup.string().required("必填"),
  fix_do_code: yup.string().required("必填"),
  fix_firm: yup.string().required("必填"),
  status: yup.number().required("必填"),
  work_person_id: yup.string().required("必填"),
  start_time: yup.date().required("必填"),
  notice: yup.string().nullable(true),
  description: yup.string().nullable(true),
});

const updateFileList = (files) => {
  formState.value[searchParams.value?.work_type - 1].lorf = files;
};

const dateItem = ref([
  {
    key: "start_time",
    name: "start_time",
    value: dayjs(),
    dateFormat: "yyyy-MM-dd",
    placeholder: "請輸入預計開始日期",
  },
]);

const dateItem2 = ref([
  {
    key: "finish_time",
    name: "finish_time",
    value: dayjs(),
    dateFormat: "yyyy-MM-dd",
    placeholder: "請輸入完成日期",
  },
]);

const form = ref(null);
const {
  formErrorMsg: maintainFormErrorMsg,
  handleSubmit: handleMaintainSubmit,
  handleErrorReset: handleMaintainErrorReset,
} = useFormErrorMessage(maintainSchema);

const saveMaintain = async () => {
  // TODO: 新增及編輯表單的作業
  const formData = new FormData(form.value);
  formData.delete("oriFile");

  formState.value[searchParams.value.work_type - 1]?.lorf.forEach(
    (file, index) => {
      formData.append(`lorf[${index}].id`, file.id ? file.id : "");
      formData.append(`lorf[${index}].file`, file.id ? null : file);
      formData.append(
        `lorf[${index}].save_file_name`,
        file.id ? file.save_file_name : ""
      );
      formData.append(`lorf[${index}].ori_file_name`, file.name);
    }
  );

  formData.append("work_type", searchParams.value.work_type);
  formData.append(
    "start_time",
    dayjs(dateItem.value[0].value).format("YYYY-MM-DD")
  );
  formData.append(
    "finish_time",
    dayjs(dateItem2.value[0].value).format("YYYY-MM-DD")
  );
  props.editRecord.id && formData.append("id", props.editRecord.id);
  props.editRecord.id && formData.append("fix_do_code", props.editRecord?.fix_do_code);
  const value = await handleMaintainSubmit(
    maintainSchema,
    formState.value[searchParams.value.work_type - 1]
  );
  const res = await postOperationRecord(formData);
  if (res.isSuccess) {
    search?.();
    onCancel();
  } else {
  }
};

const {
  formErrorMsg: companyFormErrorMsg,
  handleSubmit: handleCompanySubmit,
  handleErrorReset: handleCompanyErrorReset,
} = useFormErrorMessage(companySchema);
const saveCompany = async () => {
  const value = await handleCompanySubmit(
    companySchema,
    formState.value[searchParams.value?.work_type - 1]
  );
  let res;
  if (props.editRecord) {
    res = await updateOperationCompany(value);
  } else {
    res = await postOperationCompany(value);
  }
  if (res.isSuccess) {
    search();
    onCancel();
  } else {
    openToast("error", res.msg, "#operation_action_item");
  }
};

const onOk = async () => {
  if (searchParams.value.work_type < 3) {
    saveMaintain();
  } else {
    saveCompany();
  }
};

const reset = () => {
  for (let [key, value] of Object.entries(
    formState.value[searchParams.value?.work_type - 1]
  )) {
    if (Array.isArray(value)) {
      formState.value[searchParams.value?.work_type - 1][key] = [];
    } else {
      formState.value[searchParams.value?.work_type - 1][key] = "";
    }
  }
};

const onCancel = () => {
  for (let [key, value] of Object.entries(
    formState.value[searchParams.value?.work_type - 1]
  )) {
    formState.value[searchParams.value?.work_type - 1][key] = "";
  }
  updateEditRecord?.(null);
  reset();
  handleMaintainErrorReset();
  handleCompanyErrorReset();
  operation_action_item.close();
};

watch(
  () => props.editRecord,
  (newVal) => {
    if (newVal) {
      for (let [key, value] of Object.entries(newVal)) {
        formState.value[searchParams.value?.work_type - 1][key] = value;
        if (
          Object.hasOwn(
            formState.value[searchParams.value?.work_type - 1],
            "start_time"
          )
        ) {
          dateItem.value[0].value = newVal.start_time;
        }

        if (
          Object.hasOwn(
            formState.value[searchParams.value?.work_type - 1],
            "finish_time"
          )
        ) {
          dateItem2.value[0].value = newVal.finish_time;
        }
      }
      if (
        Object.hasOwn(
          formState.value[searchParams.value?.work_type - 1],
          "work_type"
        )
      ) {
        const work_types = ["保養", "維修"];
        formState.value[searchParams.value?.work_type - 1].work_type =
          searchParams.value.work_type;
        formState.value[searchParams.value?.work_type - 1].work_type_name =
          work_types[searchParams.value.work_type - 1];
      }
    }
  }
);
</script>

<template>
  <Modal
    id="operation_action_item"
    :title="editRecord?.id ? '編輯' : '新增'"
    :onCancel="onCancel"
    width="710"
  >
    <template #modalContent>
      <form ref="form" class="mt-5 w-full flex flex-wrap justify-between">
        <template v-if="searchParams?.work_type < 3">
          <Input
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(
                formState[searchParams?.work_type - 1],
                'work_type_name'
              )
            "
            :value="formState[searchParams?.work_type - 1]"
            class="my-2"
            name="work_type_name"
            readonly
          >
            <template #topLeft>項目</template>
          </Input>
          <Input
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(formState[searchParams?.work_type - 1], 'formId')
            "
            class="my-2"
            :value="formState[searchParams?.work_type - 1]"
            name="formId"
            readonly
          >
            <template #topLeft>表單編號</template>
          </Input>
          <DateGroup
            class="my-2"
            :items="dateItem"
            inputClass="w-full shadow-none"
            :required="true"
          >
            <template #topLeft>預計開始時間</template>
            <template #bottomLeft
              ><span class="text-error text-base">
                {{ maintainFormErrorMsg.start_time }}
              </span></template
            >
          </DateGroup>
          <Input
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(formState[searchParams?.work_type - 1], 'fix_do')
            "
            class="my-2"
            :value="formState[searchParams?.work_type - 1]"
            name="fix_do"
            :required="true"
          >
            <template #topLeft>維修項目</template>
            <template #bottomLeft
              ><span class="text-error text-base">
                {{ maintainFormErrorMsg.fix_do }}
              </span></template
            >
          </Input>
          <Select
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(
                formState[searchParams?.work_type - 1],
                'fix_do_code'
              )
            "
            :value="formState[searchParams?.work_type - 1]"
            class="my-2"
            selectClass="border-info focus-within:border-info"
            name="fix_do_code"
            Attribute="full_name"
            :options="model_data.model_devList"
            :disabled="props.editRecord?.id!==''"
          >
            <template #topLeft>維修項目代碼(設備編號)
            </template>
            <template #bottomLeft
              ><span class="text-error text-base">
                {{ maintainFormErrorMsg.fix_do_code }}
              </span></template
            >
          </Select>
          <Select
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(formState[searchParams?.work_type - 1], 'fix_firm')
            "
            :value="formState[searchParams?.work_type - 1]"
            class="my-2"
            selectClass="border-info  focus-within:border-info"
            name="fix_firm"
            Attribute="name"
            :options="model_data.model_companyList"
            :required="true"
          >
            <template #topLeft>負責廠商</template>
            <template #bottomLeft
              ><span class="text-error text-base">
                {{ maintainFormErrorMsg.fix_firm }}
              </span></template
            >
          </Select>
          <RadioGroup
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(formState[searchParams?.work_type - 1], 'status')
            "
            class="my-2"
            name="status"
            :value="formState[searchParams?.work_type - 1]"
            :items="[
              {
                key: 1,
                value: 1,
                title: '已完成',
              },
              {
                key: 0,
                value: 0,
                title: '未完成',
              },
            ]"
            :required="true"
          >
            <template #topLeft>狀態</template>
          </RadioGroup>
          <DateGroup
            class="my-2"
            :items="dateItem2"
            inputClass="w-full shadow-none"
            :required="true"
          >
            <template #topLeft>完成時間</template>
            <template #bottomLeft
              ><span class="text-error text-base">
                {{ maintainFormErrorMsg.finish_time }}
              </span></template
            >
          </DateGroup>
          <Select
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(
                formState[searchParams?.work_type - 1],
                'work_person_id'
              )
            "
            :value="formState[searchParams?.work_type - 1]"
            class="my-2"
            selectClass="border-info focus-within:border-info"
            name="work_person_id"
            Attribute="full_name"
            :options="model_data.model_userList"
            :required="true"
          >
            <template #topLeft>工作人員編號</template>
            <template #bottomLeft
              ><span class="text-error text-base">
                {{ maintainFormErrorMsg.work_person_id }}
              </span></template
            >
          </Select>
          <Textarea
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(formState[searchParams?.work_type - 1], 'notice')
            "
            :value="formState[searchParams?.work_type - 1]"
            name="notice"
            class="w-full my-2"
          >
            <template #topLeft>注意事項</template>
          </Textarea>
          <Textarea
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(
                formState[searchParams?.work_type - 1],
                'description'
              )
            "
            :value="formState[searchParams?.work_type - 1]"
            name="description"
            class="w-full my-2"
          >
            <template #topLeft>結果描述</template>
          </Textarea>
          <Upload
            class="my-2"
            name="oriFile"
            :fileList="formState[searchParams?.work_type - 1]?.lorf"
            :getFileList="updateFileList"
            :multiple="true"
            :baseUrl="`${FILE_BASEURL}/upload/operation`"
          >
            <template #topLeft>上傳檔案</template>
          </Upload>
        </template>
        <template v-else>
          <Input
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(formState[searchParams?.work_type - 1], 'name')
            "
            :value="formState[searchParams?.work_type - 1]"
            class="my-2"
            name="name"
          >
            <template #topLeft>姓名</template>
            <template #bottomLeft
              ><span class="text-error text-base">
                {{ companyFormErrorMsg.name }}
              </span></template
            >
          </Input>
          <Input
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(
                formState[searchParams?.work_type - 1],
                'contact_person'
              )
            "
            :value="formState[searchParams?.work_type - 1]"
            class="my-2"
            name="contact_person"
          >
            <template #topLeft>聯絡人</template>
            <template #bottomLeft
              ><span class="text-error text-base">
                {{ companyFormErrorMsg.contact_person }}
              </span></template
            ></Input
          >
          <Input
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(formState[searchParams?.work_type - 1], 'phone')
            "
            :value="formState[searchParams?.work_type - 1]"
            class="my-2"
            name="phone"
          >
            <template #topLeft>電話</template>
            <template #bottomLeft
              ><span class="text-error text-base">
                {{ companyFormErrorMsg.phone }}
              </span></template
            ></Input
          >
          <Input
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(formState[searchParams?.work_type - 1], 'email')
            "
            :value="formState[searchParams?.work_type - 1]"
            class="my-2"
            name="email"
          >
            <template #topLeft>Email</template>
            <template #bottomLeft
              ><span class="text-error text-base">
                {{ companyFormErrorMsg.email }}
              </span></template
            >
          </Input>
          <Input
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(formState[searchParams?.work_type - 1], 'city')
            "
            :value="formState[searchParams?.work_type - 1]"
            class="my-2 w-[250px]"
            name="city"
          >
            <template #topLeft>城市</template>
            <template #bottomLeft
              ><span class="text-error text-base">
                {{ companyFormErrorMsg.city }}
              </span></template
            ></Input
          >
          <Input
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(formState[searchParams?.work_type - 1], 'address')
            "
            :value="formState[searchParams?.work_type - 1]"
            class="my-2 w-4/5"
            name="address"
          >
            <template #topLeft>地址</template>
            <template #bottomLeft
              ><span class="text-error text-base">
                {{ companyFormErrorMsg.address }}
              </span></template
            ></Input
          >
          <Input
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(
                formState[searchParams?.work_type - 1],
                'tax_id_number'
              )
            "
            :value="formState[searchParams?.work_type - 1]"
            class="my-2"
            name="tax_id_number"
          >
            <template #topLeft>統一編號</template>
            <template #bottomLeft
              ><span class="text-error text-base">
                {{ companyFormErrorMsg.tax_id_number }}
              </span></template
            ></Input
          >
          <Input
            v-if="
              searchParams?.work_type &&
              Object.hasOwn(formState[searchParams?.work_type - 1], 'remark')
            "
            :value="formState[searchParams?.work_type - 1]"
            class="my-2"
            name="remark"
          >
            <template #topLeft>備注</template></Input
          >
        </template>
      </form>
    </template>
    <template #modalAction>
      <button
        type="reset"
        class="btn btn-outline-success mr-2"
        @click.prevent="onCancel"
      >
        取消
      </button>
      <button
        type="submit"
        class="btn btn-outline-success"
        @click.stop.prevent="onOk"
      >
        確定
      </button>
    </template>
  </Modal>
</template>

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