import { getURLInformation, isEmpty } from '@/utils/CommonUtils';
import ExtendUpload from '@/utils/ExtendUpload';
import { Card, Collapse, Descriptions } from 'antd';
import React, { useEffect, useState } from 'react';
import BasicInformation from '../BasicInformation/BasicInformation';
import {
  changeMedias,
  selectDefaultNameByBidMethodDict,
  selectNameByBidMethodDict,
} from '../utils';
import { getFailAnnoData } from './service';
import '@/utils/lq.style.less';
import moment from "moment";
import ApprovalAttachment from '../ApprovalAttachment/ApprovalAttachment';
import { getEditStatus } from '../Announcement/service';
const { Panel } = Collapse;

/**
 * 失败公告
 * @returns
 */
const ProcurementRequirementsArePublicized: React.FC = () => {
  //公告id
  const annoId = getURLInformation('id');
  //url可编辑参数
  const edit = getURLInformation('switches');
  //上传组件是否可编辑
  const isEdit = isEmpty(edit) ? false : edit?.split(',').findIndex(item => item == '020') != -1 ? true : false;
  //审批单id
  const [processInstanceId, setProcessInstanceId] = useState<string>('');
  //weboffice
  const [contentWithStyle, setContentWithStyle] = useState('');

  const [processName, setProcessName] = useState<any>(selectDefaultNameByBidMethodDict());

  //列表展示信息
  const [listDisplayInfo, setListDisplayInfo] = useState<any>({
    annoTitle: '',
    annoStartTime: '',
    annoEndTime: '',
    sectionNames: '',
    attDatasetId: '',
    medias: '',
    id: '',
    approvalFileId: 'empty',
  });
  //是否可编辑状态
  const [editState, setEditState] = useState<boolean>(isEdit)

  useEffect(() => {
    getFailAnnoData(annoId).then((res) => {
      if (res?.code === 200) {
        let data = res?.data;
        //自定义编辑状态
        if (isEmpty(edit)) {
          getEditStatus().then(res => {
            if (res?.code == 200) {
              setEditState(res?.data)
            }
          })
        }
        setListDisplayInfo({
          annoTitle: data?.annoTitle,
          annoStartTime: moment(data?.annoStartTime).format('YYYY-MM-DD HH:mm'),
          annoEndTime: moment(data?.annoEndTime).format('YYYY-MM-DD HH:mm'),
          sectionNames: data?.sectionNames,
          attDatasetId: data?.attDatasetId,
          medias: data?.medias,
          id: data?.id,
          approvalFileId: data?.approvalFileId,
        });
        setContentWithStyle(data?.contentWithStyle);
        setProcessName(selectNameByBidMethodDict(data.annoType));
        setProcessInstanceId(data.approvalInstanceId);
      }
    });
    window.parent.postMessage("{'height':" + document.documentElement.scrollHeight + "}", '*');
  }, []);

  return (
    <Card bordered={false} bodyStyle={{ padding: '0 200px', borderRadius: 6 }}>
      <h2 style={{ textAlign: 'center', marginTop: 10 }}>失败公告审批单</h2>
      <Collapse defaultActiveKey={['1', '2', '3']} style={{ marginTop: 30 }}>
        <Panel header="基本信息" key="1">
          <BasicInformation processInstanceId={processInstanceId} />
        </Panel>
        <Panel header="失败公告信息" key="2">
          <Descriptions bordered column={1}>
            <Descriptions.Item label="失败公告标题" labelStyle={{ textAlign: 'center', width: '40%' }}>
              {listDisplayInfo.annoTitle}
            </Descriptions.Item>
            <Descriptions.Item label="失败公告发布时间" labelStyle={{ textAlign: 'center' }}>
              {listDisplayInfo.annoStartTime}
            </Descriptions.Item>
            <Descriptions.Item label="失败公告结束时间" labelStyle={{ textAlign: 'center' }}>
              {listDisplayInfo.annoEndTime}
            </Descriptions.Item>
            <Descriptions.Item label="附件" labelStyle={{ textAlign: 'center' }}>
              <ExtendUpload
                bid={listDisplayInfo.attDatasetId}
                btnName="附件"
                uploadProps={{ disabled: true }}
              />
            </Descriptions.Item>
            <Descriptions.Item
              label={'关联' + processName.name4}
              labelStyle={{ textAlign: 'center' }}
            >
              {listDisplayInfo.sectionNames}
            </Descriptions.Item>
            <Descriptions.Item label="发布媒体" labelStyle={{ textAlign: 'center' }}>
              {changeMedias(listDisplayInfo.medias)}
            </Descriptions.Item>
          </Descriptions>
          <h3 className="first-title">失败公告内容</h3>
          <p className="table-border" dangerouslySetInnerHTML={{ __html: contentWithStyle }}></p>
        </Panel>
        {!editState && (isEmpty(listDisplayInfo.approvalFileId) || listDisplayInfo.approvalFileId == "empty")
          ? null
          : <Panel header="附件信息" key="3">
            <Descriptions bordered column={1}>
              <Descriptions.Item label="审批单附件" labelStyle={{ textAlign: 'center', width: '40%' }}>
                {
                  listDisplayInfo.approvalFileId != "empty" ?
                    <ApprovalAttachment
                      primaryKey={listDisplayInfo.id}
                      bid={listDisplayInfo.approvalFileId}
                      uploadProps={{ disabled: !editState }}
                      maxCount={1}
                    /> : null
                }
              </Descriptions.Item>
            </Descriptions>
          </Panel>}
      </Collapse>
    </Card>
  );
};
export default ProcurementRequirementsArePublicized;
