<template>
  <BasicLayout>
    <JsQjDetail :qjId="qjId" :dbFlag="dbFlag" v-if="qjId && qjId.length" />
    <template #bottom>
      <view class="white-bg-color py-5">
        <view class="divider"></view>
        <view class="flex-row items-center pb-10 pt-5">
          <u-button
            text="取消"
            class="ml-15 mr-7"
            :plain="true"
            @click="navigateBack"
          />
          <u-button
            text="提交"
            class="mr-15 mr-7"
            type="primary"
            @click="submit"
          />
        </view>
      </view>
    </template>
  </BasicLayout>
</template>

<script setup lang="ts">
import JsQjDetail from "./components/jsQjDetail.vue";
import { onLoad } from "@dcloudio/uni-app";
import { navigateBack } from "@/utils/uniapp";
import { useForm } from "@/components/BasicForm/hooks/useForm";
import { jsQjSpApi } from '@/api/base/jsQjApi';
import { useUserStore } from "@/store/modules/user";
import { useDataStore } from "@/store/modules/data";
const { getJs } = useUserStore();
const { getData } = useDataStore();

const dbFlag = ref(false);

const qjId = ref<string>();

const [register, { getValue }] = useForm({
  schema: [
    {
      field: "spStatus",
      label: "审批意见",
      component: "BasicCheckbox",
      required: true,
      componentProps: {
        data: [
          { value: 2, text: "同意" },
          { value: 1, text: "拒绝" },
        ],
      },
    },
    {
      field: "spYj",
      label: "审批说明",
      component: "BasicInput",
      required: true,
      itemProps: {
        labelPosition: "top",
      },
      componentProps: {
        type: "textarea",
      },
    },
  ],
});

const submit = async () => {
  const data = await getValue();
  const params = { ...data };
  params.qjId = qjId.value;
  params.jsId = getJs.id;
  uni.showLoading({ title: "审批中..." });
  await jsQjSpApi(params);
  uni.hideLoading();
  navigateBack();
};

onLoad(async (data: any) => {
  // 从待办过来的，需要从后端获取数据
  if (data && data.from && data.from == "db") {
    qjId.value = data.id;
    dbFlag.value = true;
  } else {
    qjId.value = getData.id;
    dbFlag.value = false;
  }
});

</script>
