<template>
  <a-modal
    :width="600"
    :maskClosable="false"
    :visible="visible"
    :confirm-loading="loading"
    :forceRender="true"
    title="任务转交"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
    @close="updateVisible(false)"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }">
      <a-form-item label="转交目标人:" name="userNames">
        <a-input v-model:value="form.userNames" @focus="inputClick" placeholder="请选择转交目标人"></a-input>
      </a-form-item>
      <a-form-item label="转交意见:" name="suggestContent">
        <a-textarea v-model:value="form.suggestContent" placeholder="请输入转交意见" :rows="4" />
      </a-form-item>
    </a-form>

    <!-- 选择人员 -->
    <Selection
      v-model:visible="visibleFlowPick"
      v-if="visibleFlowPick"
      :data="selectedData"
      :showTab="['user']"
      title="选择转交目标人"
      :isRadio="true"
      @done="closeFlowPick"
    ></Selection>
  </a-modal>
</template>

<script setup name="DelegateOperate">
import { ref, onMounted, reactive } from 'vue';
import { FlowTaskApi } from '../../api/FlowTaskApi';
import { message } from 'ant-design-vue';

const props = defineProps({
  visible: Boolean,
  data: Array
});

const emits = defineEmits(['update:visible', 'done']);

// 弹框加载状态
const loading = ref(false);
// 表单数据
const form = ref({
  userIdList: [],
  userNames: ''
});
// 验证规则
const rules = reactive({
  suggestContent: [{ required: true, message: '请输入转交意见', type: 'string', trigger: 'blur' }],
  userNames: [{ required: true, message: '请选择转交目标人', type: 'string', trigger: 'change' }]
});
// ref
const formRef = ref(null);

// 是否显示选择人员弹框
const visibleFlowPick = ref(false);

// 总数据
const selectedData = ref({
  selectUserList: []
});

onMounted(() => {
  form.value.taskId = props.data[0].id;
});

// 切换弹框显示隐藏状态
const updateVisible = value => {
  emits('update:visible', value);
};

// 点击保存
const save = async () => {
  await formRef.value.validate();
  form.value.userId = form.value.userIdList[0].bizId;
  FlowTaskApi.ruTaskDelegate(form.value).then(res => {
    if (res.code == '00000') {
      message.success(res.message);
      emits('update:visible', false);
      emits('done');
    }
  });
};

// 选人
const inputClick = () => {
  selectedData.value.selectUserList = [...form.value.userIdList];
  visibleFlowPick.value = true;
};

// 关闭选择弹框
const closeFlowPick = data => {
  form.value.userIdList = [...data.selectUserList];
  form.value.userNames = form.value.userIdList.map(item => item.name).join('；');
};
</script>

<style scoped></style>
