<template>
  <view class="p-3">
    <!-- 交付物名称 -->
    <view class="flex justify-between" @click="collapsed = !collapsed">
      <view class="relative">
        {{ deliverData ? deliverData.deliverName : '' }}审核状态

        <!-- 有提交记录 有检查人 检查人里有我 且状态是null或者0 才显示小红点 -->
        <!-- <u-badge
          size="mini"
          :is-dot="true"
          style="transform: translate3d(2em, -1em, 0)"
          v-show="
            deliverData.details &&
            deliverData.details.length &&
            deliverData.checkerList &&
            deliverData.checkerList.length &&
            deliverData.checkerList.find(item => item.isMine && !item.status)
          "
        ></u-badge> -->
      </view>
      <!-- 展开折叠按钮 -->
      <u-icon :name="collapsed ? 'arrow-up' : 'arrow-down'"></u-icon>
    </view>

    <view v-show="collapsed" class="mt-1">
      <view class="flex justify-between items-center">
        <!-- 提交人和时间信息 -->
        <view class="text-gray-400 text-xs">
          <text class="mr-4" v-if="deliverData.submitMemberName">{{ deliverData.submitMemberName }}</text>
          <text v-if="deliverData.submitTime"> {{ dayjs(+deliverData.submitTime).format('MM-DD HH:mm') }}</text>
        </view>

        <text class="text-blue-400 text-xs text-right my-2" @click="openDeliverHistory">历史交付物</text>
      </view>

      <!-- 提交的链接信息 -->
      <DeliverLink :link="deliverData.details[0]" v-if="deliverData.details && deliverData.details[0]" />

      <!-- 审核人 标题 -->
      <view class="text-gray-400 flex justify-between mt-3">
        <text>审核</text>
        <text class="text-blue-400 text-xs" @click="openMoreRecords">更多审核记录</text>
      </view>

      <!-- 审核人 列表 -->
      <view v-if="deliverData.checkerList">
        <!-- 遍历审核人信息 -->
        <view class="mt-2 text-sm flex justify-between" v-for="item in deliverData.checkerList">
          <view>
            <view class="font-semibold">{{ item.checkerName }}</view>
            <view class="text-xs text-gray-400">{{ item.remark }}</view>
            <view class="text-xs text-gray-400" v-if="+item.checkTime > 0">{{ dayjs(+item.checkTime).format('MM-DD HH:mm') }}</view>
          </view>

          <!-- 不是自己 显示审核状态 -->
          <view v-show="item.isMine !== 1" class="text-xs">
            <text v-if="item.status === 1" class="text-green-600"> 已通过 </text>
            <text v-else-if="item.status === 2" class="text-red-600"> 已驳回 </text>
            <text v-else class="text-gray-400"> 待审核 </text>
          </view>

          <!-- 自己是当前审核人 且未审核状态 -->
          <view v-show="item.isMine === 1 && (item.status === null || item.status === 0)">
            <u-button size="mini" shape="circle" class="mr-4 h-1-4 leading-1-4" type="primary" @click="checkModal.mode = 'RESOLVE'">
              通过
            </u-button>
            <u-button size="mini" shape="circle" class="h-1-4 leading-1-4" type="error" @click="checkModal.mode = 'REJECT'">
              驳回
            </u-button>
          </view>

          <!-- 自己是审核人 且审核过 当前审核人的审核状态并展示得分情况 -->
          <view v-show="item.isMine === 1 && item.status > 0" class="text-xs">
            <view class="mb-1">
              <text v-if="item.status === 1" class="text-green-600"> 已通过 </text>
              <text v-else-if="item.status === 2" class="text-red-600"> 已驳回 </text>
            </view>
            <zwp-ring-timing mode="chart" :value="item.score" active-color="#F59E0B" :radius="30" :bar-width="4" v-if="item.score">
              <text class="text-yellow-500 font-medium">{{ item.score }}</text>
            </zwp-ring-timing>
          </view>
        </view>
      </view>
    </view>

    <checkFormModal :data="checkModal" @hide="checkModal.mode = 'HIDE'" @submit-end="$emit('check-success')" />
  </view>
</template>

<script setup>
import { ref, reactive, inject } from 'vue';
import dayjs from 'dayjs';
import checkFormModal from './check-form-modal.vue';

const deliverData = inject('deliver');
defineEmits(['check-success']);
const collapsed = ref(false); // 展开/关闭

const checkModal = reactive({
  mode: 'HIDE', // HIDE->隐藏 RESOLVE->通过 REJECT->驳回
  deliverRecordId: () => (deliverData.value ? deliverData.value.deliverRecordId : ''), // 交付物记录id
});

// 查看历史记录
function openDeliverHistory() {
  const { deliverId } = deliverData.value;
  // console.log(deliverId)
  uni.navigateTo({ url: `/pages/submitLog/submitLog?deliverId=${deliverId}` });
}

// 跳转到审核记录页面
function openMoreRecords() {
  const { deliverRecordId } = deliverData.value;
  uni.navigateTo({ url: `/pages/checkLog/checkLog?deliverRecordId=${deliverRecordId}` });
}
</script>
