<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit" width="1000px">
    
    <BasicForm @register="registerForm" v-show="false" />

    <a-descriptions title="" bordered>

      <a-descriptions-item label="任务名称" :span="2">{{taskInfo.taskName}}</a-descriptions-item>
      
      
      <a-descriptions-item label="备注/说明" :span="6">{{taskInfo.remark}}</a-descriptions-item>
      
      <a-descriptions-item label="AI解释成果" :span="2">
        {{ tifInfo.shpInfo.shpPath }}
        <a-button type="link" @click="copyToClipboard(tifInfo.shpInfo.shpPath)">复制</a-button>
      </a-descriptions-item>
     
      <a-descriptions-item label="前时相" :span="2">
        <p
          v-for="(prePhase,index) in tifInfo.previousPhaseInfo"
          :key="index"
          :style="{ 'margin-bottom': 0 }"
        >
          {{ prePhase.tifPath }}
          <a-button type="link" @click="copyToClipboard(prePhase.tifPath)">复制</a-button>
        </p>
      </a-descriptions-item>

      <a-descriptions-item label="后时相" :span="2">
        <p
          v-for="(prePhase,index) in tifInfo.laterPhaseInfo"
          :key="index"
          :style="{ 'margin-bottom': 0 }"
        >
          {{ prePhase.tifPath }}
          <a-button type="link" @click="copyToClipboard(prePhase.tifPath)">复制</a-button>
        </p>
      </a-descriptions-item>

      <a-descriptions-item label="图斑数量" :span="2">{{taskInfo.shpCount}}</a-descriptions-item>
      <a-descriptions-item label="已领取数量" :span="2">{{taskInfo.receiveShpCount}}</a-descriptions-item>
      <a-descriptions-item label="未领取数量" :span="2">{{taskInfo.noReceiveShpCount}}</a-descriptions-item>

      <a-descriptions-item label="任务状态" :span="2">
        <a-tag color="orange" v-if="taskInfo.state == 0">待领取</a-tag>
          <a-tag color="blue" v-if="taskInfo.state == 1">领取中</a-tag>
          <a-tag color="green" v-if="taskInfo.state == 2">已领完</a-tag>
      </a-descriptions-item>

      <a-descriptions-item label="创建人" :span="2">{{taskInfo.createUserName}}</a-descriptions-item>
      <a-descriptions-item label="创建时间" :span="2">{{taskInfo.createTime}}</a-descriptions-item>
      <a-descriptions-item label="更新人" :span="2">{{taskInfo.updateUserName}}</a-descriptions-item>
      <a-descriptions-item label="更新时间" :span="2">{{taskInfo.updateTime}}</a-descriptions-item>
    

      <a-descriptions-item label="任务进度" :span="2">
        <a-tag color="green" v-if="taskInfo.isCompleted">已完成</a-tag>
        <a-tag color="red" v-else>未完成</a-tag>
      </a-descriptions-item>
      
      <a-descriptions-item label="完成时间" :span="2" v-show="taskInfo.isCompleted">{{taskInfo.completeTime}}</a-descriptions-item>
      
    </a-descriptions>

  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { formSchema } from './dept.data';

  import { getDeptList, addDept, updateDept } from '@/api/demo/system';
  import { useMessage } from '@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  defineOptions({ name: 'DeptModal' });

  const emit = defineEmits(['success', 'register']);

  const isUpdate = ref(true);

  const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
    labelWidth: 120,
    baseColProps: { span: 24 },
    schemas: formSchema,
    showActionButtonGroup: false,
  });

  const taskInfo = ref({})
  const tifInfo = ref({});
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
   
    taskInfo.value = data.record.record;
    tifInfo.value = data.record.info;

    console.log("taskInfo.value",data.record)

    resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;

    if (unref(isUpdate)) {
      setFieldsValue({
        ...data.record,
      });
    }

    const treeData = await getDeptList();
    updateSchema({
      field: 'parentId',
      componentProps: { treeData },
    });
  });

  const getTitle = computed(() => (!unref(isUpdate) ? '创建任务' : '任务详情'));

  async function handleSubmit() {
    try {
      const values = await validate();
      let query = values;
      if (query.id) {
        query.id = Number(query.id);
      } else {
        query.id = 0;
      }
      // 调用接口
      if (!unref(isUpdate)) {
        const data = await addDept(query);
        if (data) {
          setModalProps({ confirmLoading: true });
          closeModal();
          emit('success');
          return createMessage.success('创建成功');
        } else {
          return createMessage.error('创建失败');
        }
      } else {
        const data = await updateDept(query);
        if (data) {
          setModalProps({ confirmLoading: true });
          closeModal();
          emit('success');
          return createMessage.success('编辑成功');
        } else {
          return createMessage.error('编辑失败');
        }
      }
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }

    // 复制到剪贴板
  const copyToClipboard = async (record) => {
    try {
      await navigator.clipboard.writeText(record);
      createMessage.success('文本已复制到剪贴板');
    } catch (err) {
      createMessage.error('无法复制文本');
    }
  };
</script>
