<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="类别" v-bind="validateInfos.problemItem">
            <a-input v-model:value="formData.problemItem" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="问题描述" v-bind="validateInfos.problemDes">
            <a-textarea v-model:value="formData.problemDes" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="问题来源" v-bind="validateInfos.problemSource">
            <a-input v-model:value="formData.problemSource" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否整改" v-bind="validateInfos.isReceive">
            <a-input v-model:value="formData.isReceive" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="整改计划" v-bind="validateInfos.programme">
            <a-textarea v-model:value="formData.programme" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="整改落实情况" v-bind="validateInfos.strategy">
            <a-textarea v-model:value="formData.strategy" :disabled="disabled" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { getValueType } from '/@/utils';
  import { Form } from 'ant-design-vue';
  import { useTabs } from '/@/hooks/web/useTabs';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useUserStoreWithOut } from '/@/store/modules/user';
  import { useRouter } from 'vue-router';
  import { getAuthCache, setAuthCache, removeAuthCache } from '/@/utils/auth';
  import { TOKEN_KEY } from '/@/enums/cacheEnum';
  import { useUserStore } from '/@/store/modules/user';

  const router = useRouter();
  const user = useUserStoreWithOut();
  const { closeCurrent, refreshPage, setTitle } = useTabs();

  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => {} },
    formBpm: { type: Boolean, default: true },
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    problemItem: undefined,
    problemDes: '',
    problemSource: '',
    isReceive: '',
    programme: '',
    labels: '',
    actIds: '',
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  const loading = ref(false);
  //表单验证
  const validatorRules = {};
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  const disabled = ref<boolean>(true);
  const listAll = '/problem/stProblemCheck/queryById';

  // 从URL获取参数
  const getUrlParams = () => {
    const winurl = window.location.href;
    const params = new URLSearchParams(winurl.split('?')[1]);
    return {
      cctoken: params.get('Token') || '',
      WorkID: params.get('WorkID') || '',
    };
  };

  const iniData = async () => {
    try {
      loading.value = true;
      const { cctoken, WorkID } = getUrlParams();
      console.log('Token:', cctoken, 'WorkID:', WorkID);

      const timestamp = new Date().getTime();
      const gettokeyUrl = '/api/jflow/getCCWorkTokenAndTid';
      const { tid, token } = await defHttp.get({
        url: gettokeyUrl,
        params: {
          targetKey: 'targetKey',
          flowToken: cctoken,
          WorkID: WorkID,
          _t: timestamp,
        },
      });
      console.log('问题整改tid ', tid);
      // setAuthCache(TOKEN_KEY, token);
      //params: { id: tid, token: token,  "_t": timestamp },

      defHttp.get({ url: listAll, params: { token: token, id: tid } }).then((res) => {
        console.log('res ', res);

        formData.problemItem = res.problemItem;

        formData.problemDes = res.problemDes;
        formData.problemSource = res.problemSource;
        if (res.isReceive == '1') {
          formData.isReceive = '是';
        } else {
          formData.isReceive = '否';
        }

        formData.programme = res.programme;
        formData.strategy = res.strategy;
      });
    } catch (error) {
      console.error('初始化表单数据失败:', error);
      createMessage.error('初始化表单数据失败');
    } finally {
      loading.value = false;
    }
  };

  onMounted(async () => {
    iniData();
  });

  defineExpose({});
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
