<template>
  <BasicLayout>
    <view class="qj-detail">
      <!-- 请假信息卡片 -->
      <view class="info-card">
        <view class="card-header">
          <text class="applicant-name" v-if="dbFlag">{{ dbData.dbZy }}</text>
          <text class="applicant-name" v-else>学生{{ qjData.xsxm }}的请假申请</text>
        </view>
        <view class="divider"></view>
        <view class="card-body">
          <view class="info-row">
            <text class="label">请假类型:</text>
            <text class="value">{{ qjData.qjlx }}</text>
          </view>
          <view class="info-row">
            <text class="label">开始时间:</text>
            <text class="value">{{ qjData.qjkstime }}</text>
          </view>
          <view class="info-row">
            <text class="label">结束时间:</text>
            <text class="value">{{ qjData.qjjstime }}</text>
          </view>
          <view class="info-row">
            <text class="label">请假时长:</text>
            <text class="value">{{ qjData.qjsc }}</text>
          </view>
          <view class="info-row">
            <text class="label">是否离校:</text>
            <text class="value">{{ qjData.sflx === 1 ? '是' : '否' }}</text>
          </view>
          <view class="info-column">
            <text class="label">请假事由:</text>
            <text class="value">{{ qjData.qjsy }}</text>
          </view>
        </view>
      </view>
      <!-- 请假信息卡片 -->
      <view class="info-card">
        <view class="card-header">
          <text class="applicant-name">审批意见</text>
        </view>
        <view class="divider"></view>
        <BasicForm @register="register" />
      </view>

    </view>
    <template #bottom>
      <view class="white-bg-color py-5">
        <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 { onLoad } from "@dcloudio/uni-app";
import { useForm } from "@/components/BasicForm/hooks/useForm";
import { navigateBack } from "@/utils/uniapp";
import { xsQjFindByIdApi, xsQjSpApi, dbBlApi } from "@/api/base/server";
import { useDataStore } from "@/store/modules/data";
const { getData, getDb, setData } = useDataStore();

const dbFlag = ref(false);

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

// 请假基础数据
const qjData = computed(() => getData || {});
const dbData = computed(() => getDb || {});

const submit = async () => { 
  const data = await getValue();
  const params = { ...qjData.value };
  params.flag = data.flag;
  params.comment = data.comment;
  uni.showLoading({
    title: "提交中...",
  });
  const res = await xsQjSpApi(params);
  // 如果是待办项，更新待办项状态
  if (dbFlag.value) {
    await dbBlApi({ id: dbData.value.id });
  }
  uni.hideLoading();
  navigateBack();
};

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

</script>

<style lang="scss" scoped>
.qj-detail {
  background-color: #f5f7fa;
}

.info-card {
  margin: 15px;
  background-color: #fff;
  border-radius: 8px;
  padding: 15px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);

  .card-header {
    font-size: 16px;
    font-weight: bold;
    color: #333;
    margin-bottom: 10px;

    .applicant-name {
      font-size: 16px;
      font-weight: bold;
      color: #333;
    }
  }

  .divider {
    height: 1px;
    background-color: #eee;
    margin-bottom: 15px;
  }


  .card-body {
    padding: 15px;

    .info-row {
      display: flex;
      margin-bottom: 10px;

      .label {
        font-size: 14px;
        color: #bbb;
        width: 70px;
        flex-shrink: 0;
        margin-right: 8px;
      }

      .value {
        font-size: 14px;
        color: #333;
        flex: 1;
      }
    }
    .info-column {
      display: flex;
      flex-direction: column;

      .label {
        font-size: 14px;
        color: #bbb;
        flex-shrink: 0;
        margin-right: 8px;
        width: 100%;
        margin-bottom: 5px;
      }
            
      .value {
        font-size: 14px;
        color: #333;
        flex: 1;
      }
    }
  }

}

.bottom-action {
  padding: 15px;
  margin-top: 20px;

  .back-btn {
    width: 100%;
    height: 44px;
    line-height: 44px;
    background-color: #fff;
    color: #333;
    border-radius: 22px;
    font-size: 16px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  }
}
</style>
