<template>
  <!-- 审核通过的modal -->
  <u-mask :show="data.mode !== 'HIDE'" @click="handleHide">
    <view class="modal-content-wrap" @click.stop>
      <!-- 通过modal的标题 -->
      <view class="modal-content-head"> {{ data.mode === 'RESOLVE' ? '审核通过' : '审核驳回' }} </view>

      <view class="modal-content-body">
        <!-- 评分 -->
        <view class="flex justify-between mb-4" v-show="data.mode === 'RESOLVE'">
          <u-number-box v-model="score" size="30" input-width="50" :max="100" :min="0" :step="1"></u-number-box>
          <view class="w-32 pt-4">
            <u-slider v-model="score" active-color="#34D399" :max="100" :min="0" :step="1"></u-slider>
          </view>
        </view>

        <u-input v-model="commit" type="textarea" :border="true" :auto-height="true" />

        <view class="common-list">
          <view v-for="item in words" class="leading-12" @click="commit = item">
            {{ item }}
          </view>
        </view>
      </view>

      <view class="modal-content-foot">
        <view class="cancel" @click="handleHide"> 取消 </view>
        <view class="confirm" @click="handleSubmit(data.mode)"> 确定 </view>
      </view>
    </view>
  </u-mask>
</template>

<script setup>
import { ref, computed, watch, inject } from 'vue';
import { useStore } from 'vuex';
import { quickWords } from '@/config/deliver';

const props = defineProps({
  data: { type: Object, default: {} },
  msgId: { default: '', type: String },
});
const emits = defineEmits(['hide', 'submit-end']);
const store = useStore();
const words = computed(() => quickWords[props.data.mode]); // 快捷用语
const projectId = computed(() => store.getters['project/projectId']);
const task = inject('task');
const commit = ref(''); // 提交的信息
const score = ref(100); // 评分

/**
 * 提交评审信息
 * 提交成功后隐藏modal 重置表单控件
 * 给父组件信息 更新值
 * @param {string} mode 'RESOLVE'|'REJECT'
 */
async function handleSubmit(mode) {
  try {
    const deliverRecordId = props.data.deliverRecordId();
    const param = {
      projectId: projectId.value,
      deliverRecordId,
      type: mode === 'RESOLVE' ? 1 : 2,
      remark: commit.value,
      score: mode === 'RESOLVE' ? score.value : '',
      msgId: task.msgId,
    };

    await uni.$u.api.checkDeliver(param);
    handleHide(); // 隐藏 + 重置
    uni.$ui.showToast('审核信息提交成功');
    // 通知父组件评审成功 更新信息
    emits('submit-end', param);
  } catch (error) {
    console.error('error: ', error);
    uni.$ui.showToast('审核信息提交失败, 请稍后重试');
  }
}

// 隐藏及 重置
function handleHide() {
  emits('hide');
  // 重置相关数据
  score.value = 100;
  commit.value = '';
}
</script>
