<template>
  <view class="white-bg-color py-5 yw-confirm">
    <view class="flex-row items-center pt-5 pb-10">
      <u-button text="驳回" class="ml-15 mr-7" :plain="true" @click="showDlg('reject')" />
      <u-button text="同意" class="mr-15 mr-7" type="primary" @click="submit" />
    </view>
    <view class="flex-row items-center pb-5">
      <u-button text="终止" class="ml-15 mr-7" :plain="true" @click="showDlg('stop')" />
      <u-button text="转办" class="mr-15 mr-7" :plain="true" @click="showTransfer" />
    </view>
    <view class="flex-row items-center pb-5">
      <u-button text="协调代课教师" class="mr-15 mr-7" :plain="true" @click="showXtDlg" />
    </view>
		<!-- 驳回弹窗 -->
		<u-popup :show="dlgFlag" mode="center" :closeOnClickOverlay="false" @close="closeDlg">
			<view class="popup-content">
        <view class="popup-header">
          <view class="popup-title">{{ dlgTips }}</view>
        </view>
        <view class="popup-body">
          <u-input v-model="spRemark" type="textarea" :placeholder="'请填写' + dlgTips" :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="handleDlgSubmit">确定</u-button>
				</view>
			</view>
		</u-popup>
    <YwTransfer ref="transferRef" @submit="handleTransfer" />
    <XtDkJs ref="xtDkJsRef" @submit="handleXtDkJs" />
  </view>
</template>

<script lang="ts" setup>
import YwTransfer from "../YwTransfer/index.vue";
import XtDkJs from "@/pages/view/hr/jsQj/components/XtDkJs.vue";

// 接收外部传入属性并设置默认值
const props = withDefaults(defineProps<{
  spApi: any,             // 审批Api
  transferApi: any,       // 转办Api
  stopApi: any,           // 终止Api
  params: any,            // 默认的请求参数
  stausName?: string,     // 默认的状态属性名
  remarkName?: string,    // 默认的审批说明属性名
  approvedValue?: string, // 默认的同意时的状态值
  rejectValue?: string,   // 默认的拒绝时的状态值
  approvedRemark?: string // 
  autoToMessage?: boolean     // 默认自动跳转到待办页面
  showXt?: boolean
}>(), {
  spApi: async (params: any) => {},
  transferApi: async (params: any) => {},
  stopApi: async (params: any) => {},
  params: {},
  stausName: 'spStatus',
  remarkName: 'spRemark',
  approvedValue: 'approved',
  rejectValue: 'rejected',
  approvedRemark: '同意',
  autoToMessage: true,
  showXt: false
});

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

const transferRef = ref<any>(null);
const xtDkJsRef = ref<any>(null);

const dlgFlag = ref(false);
const dlgType = ref("");
const dlgTips = ref("");
const spRemark = ref("");

const goToMessage = () => {
  setTimeout(() => {
    uni.reLaunch({ url: '/pages/base/message/index' });
  }, 100);
}

const showDlg = (type: string) => {
	dlgFlag.value = true;
  dlgType.value = type;
  if (type === 'reject') {
    dlgTips.value = '驳回原因';
  } else if (type === 'stop') {
    dlgTips.value = '终止原因';
  }
};

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

const showTransfer = () => {
  transferRef.value.showDlg();
};

const showXtDlg = () => { 

};

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

// 驳回处理
const handleReject = async (params: any) => {
	uni.showLoading({ title: "正在驳回..." });
  await props.spApi(params);
	uni.hideLoading();
  emit('reject');
};

// 终止处理
const handleStop = async (params: any) => {
	uni.showLoading({ title: "正在终止..." });
  await props.stopApi(params);
	uni.hideLoading();
  emit('stop');
}

// 转办处理
const handleTransfer = async (data: any) => { 
  const params = {
    ...props.params
  };
  const newSprList = data.newSprList || [];
  const newCsrList = data.newCsrList || [];
  params.spRemark = data.spRemark;
  params.zbrIds = newSprList.map((item: any) => item.userId).join(",");
  params.csrIds = newCsrList.map((item: any) => item.userId).join(",");
	uni.showLoading({ title: "正在转办..." });
  await props.transferApi(params);
  transferRef.value.closeDlg();
	uni.hideLoading();
  emit('transfer');
  if (props.autoToMessage) {
    goToMessage();
  }
};

// 转办处理
const handleXtDkJs = async (data: any) => { 
  const params = {
    ...props.params
  };
  const newSprList = data.newSprList || [];
  const newCsrList = data.newCsrList || [];
  params.spRemark = data.spRemark;
  params.zbrIds = newSprList.map((item: any) => item.userId).join(",");
  params.csrIds = newCsrList.map((item: any) => item.userId).join(",");
	uni.showLoading({ title: "正在转办..." });
  await props.transferApi(params);
  transferRef.value.closeDlg();
	uni.hideLoading();
  emit('transfer');
  if (props.autoToMessage) {
    goToMessage();
  }
};

const handleDlgSubmit = async () => {
  if (!spRemark.value || !spRemark.value.trim()) {
    uni.showToast({ title: "请填写" + dlgTips.value, icon: "none" });
    return;
  }
  const params = {
    ...props.params
  };
  params[props.stausName] = props.rejectValue;
  params[props.remarkName] = spRemark.value;
  if (dlgType.value === 'reject') {
    await handleReject(params);
  } else if (dlgType.value === 'stop') {
    await handleStop(params);
  } else if (dlgType.value === 'transfer') {
    await handleTransfer(params);
  }
  closeDlg();
  if (props.autoToMessage) {
    goToMessage();
  }
};

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

<style lang="scss" scoped>
.yw-confirm { 
  box-shadow: 0 -0.1rem 0.5rem rgba(0, 0, 0, 0.1);
  margin-top: 8rpx;
}

.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>