<template>
  <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="showDlg" />
      <u-button text="转办" class="mr-15 mr-7" :plain="true" @click="submit" />
      <u-button text="驳回" class="ml-15 mr-7" :plain="true" @click="showDlg" />
      <u-button text="同意" class="mr-15 mr-7" type="primary" @click="submit" />
    </view>
		<!-- 驳回弹窗 -->
		<u-popup :show="dlgFlag" mode="center" :closeOnClickOverlay="false" @close="closeDlg">
			<view class="popup-content">
        <view class="popup-header">
          <view class="popup-title">驳回原因</view>
        </view>
        <view class="popup-body">
          <u-input v-model="rejectReason" type="textarea" placeholder="请填写驳回原因" :autoHeight="true" maxlength="200" class="reject-reason-input" />
        </view>
				<view class="popup-actions flex-row justify-end mt-4">
					<u-button class="mr-2" @click="closeDlg">取消</u-button>
					<u-button type="primary" @click="handleReject">确定</u-button>
				</view>
			</view>
		</u-popup>
  </view>
</template>

<script lang="ts" setup>
// 接收外部传入属性并设置默认值
const props = withDefaults(defineProps<{
  api: any,               // 请求API
  params: any,            // 默认的请求参数
  stausName?: string,     // 默认的状态属性名
  remarkName?: string,    // 默认的审批说明属性名
  approvedValue?: string, // 默认的同意时的状态值
  rejectValue?: string,   // 默认的拒绝时的状态值
  approvedRemark?: string // 默认的同意说明文字
}>(), {
  api: async (params: any) => {},
  params: {},
  stausName: 'spStatus',
  remarkName: 'spRemark',
  approvedValue: 'approved',
  rejectValue: 'rejected',
  approvedRemark: '同意'
});



// 定义一个上级传入的emit响应事件用于接收数据变更
const emit = defineEmits(['submit', 'reject'])

const dlgFlag = ref(false);
const rejectReason = ref("");
const showDlg = () => {
	dlgFlag.value = true;
};

const closeDlg = () => {
	dlgFlag.value = false;
};

const submit = async () => {
  const params = {
    ...props.params
  };
  params[props.stausName] = props.approvedValue;
  params[props.remarkName] = "";
	uni.showLoading({ title: "确认中..." });
  await props.api(params);
	uni.hideLoading();
  closeDlg();
  emit('submit');
};

// 驳回处理
const handleReject = async () => {
	if (!rejectReason.value.trim()) {
		uni.showToast({ title: "请填写驳回意见", icon: "none" });
		return;
	}
  const params = {
    ...props.params
  };
  params[props.stausName] = props.rejectValue;
  params[props.remarkName] = rejectReason.value;
	uni.showLoading({ title: "正在驳回..." });
  await props.api(params);
	uni.hideLoading();
  closeDlg();
  emit('reject');
};

defineExpose({
  showDlg,
  closeDlg
});
</script>

<style lang="scss" scoped>
.popup-content {
  .popup-header {
    padding: 20rpx 30rpx;
    border-bottom: 1px solid $u-border-color;
  }
  .popup-body {
    padding: 20rpx 30rpx;
    
    .reject-reason-input {
      min-height: 160rpx;
      min-width: 500rpx;
      align-items: flex-start;
    }
  }
  .popup-actions {
    padding: 20rpx 30rpx;
  }
}

</style>