import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import styles from './index.module.scss'
import { useParams } from 'react-router-dom'
import X3auditInfo from '@/pages/X_stock/X3auditInfo'
import { MessageFu } from '@/utils/message'
import history from '@/utils/history'
import { Button, DatePicker, Input } from 'antd'
import MyPopconfirm from '@/components/MyPopconfirm'
import dayjs from 'dayjs'
import { B3aForm1 } from '../../B3_4page/B3edit/data'
import Z3upFiles from '@/components/Z3upFiles'
import ZRichTexts from '@/components/ZRichTexts'
import MyTable from '@/components/MyTable'
import { B1TableC } from '@/utils/tableData'
import { pageTitTxtObj } from '@/pages/D_storeManage/D4impStor/D4edit'
import B1MoAdd from './B1MoAdd'
import B3flowTable from '../../B3flowTable'
import B1appraisal from '../B1appraisal'
function B1edit() {
  const { key, id } = useParams<any>()

  // 滚到顶部
  const sollrDom = useRef<HTMLDivElement>(null)

  useEffect(() => {
    console.log('key:', key)

    if (sollrDom.current) sollrDom.current.scrollTop = 0
  }, [key])

  // 上传附件的ref
  const filesRef = useRef<any>(null)
  // const filesRes = filesRef.current.filesIdRes();

  // 富文本的ref
  const ZRichTextRef = useRef<any>(null)

  // 审批意见的ref
  const ZAuditRef = useRef<any>(null)

  // 设置富文本
  //  ZRichTextRef.current?.ritxtShowFu(JSON.parse(data.rtf))

  const timeChange = useCallback((e: any) => {
    console.log(123, e)
  }, [])

  // 新增的底部按钮点击
  const btnClickFu = useCallback((val: number) => {
    const rtf1 = ZRichTextRef.current?.fatherBtnOkFu() || { flag: true }
    console.log('申请信息富文本', JSON.stringify(rtf1.val || ''))

    const rtf2 = ZAuditRef.current?.resData()
    console.log('审批信息富文本', rtf2)

    // if (1 + 1 === 2) return

    if (val === 2) {
      // 存草稿 当前页保存 不跳转
      MessageFu.success('草稿保存成功')
    } else {
      // 跳到详情页
      history.push(`/collect_edit/4/111`)
    }
  }, [])

  // 点击删除
  const delTableFu = useCallback(async (id: number) => {}, [])

  const tableLastBtn = useMemo(() => {
    return [
      {
        title: '操作',
        render: (item: any) => {
          return (
            <>
              <Button size='small' type='text' onClick={() => setOpenInfo(item)}>
                {['3', '4'].includes(key) ? '查看' : '编辑'}
              </Button>
              {['3', '4'].includes(key) ? null : (
                <MyPopconfirm txtK='删除' onConfirm={() => delTableFu(item.id)} />
              )}
            </>
          )
        }
      }
    ]
  }, [delTableFu, key])

  // 审批的sta
  const [auditSta, setAuDitSta] = useState('')

  // 查看模式下的按钮 待完善
  const lookBtn = useMemo(() => {
    return (
      <>
        <Button type='primary'>创建</Button>
        <Button type='primary'>提交</Button>
        <MyPopconfirm
          txtK='撤回'
          onConfirm={() => {}}
          Dom={
            <Button type='primary' danger>
              撤回
            </Button>
          }
        />

        <Button type='primary'>审批</Button>
        <Button type='primary'>编辑</Button>
        <Button type='primary'>重新提交</Button>
        <Button type='primary'>导出</Button>
        <MyPopconfirm
          txtK='删除'
          onConfirm={() => {}}
          Dom={
            <Button type='primary' danger>
              删除
            </Button>
          }
        />
        <Button onClick={() => history.push('/collect')}>返回</Button>
      </>
    )
  }, [])

  // 征集线索
  const [openInfo, setOpenInfo] = useState<any>({ id: 0 })

  return (
    <div className={styles.B1edit}>
      <div className='pageTitle'>藏品征集-{Reflect.get(pageTitTxtObj, key)}</div>

      <div className='B1main'>
        {['3'].includes(key) ? (
          <X3auditInfo
            dirCode='待完善'
            myUrl='待完善'
            auditSta={auditSta}
            auditStaFu={val => setAuDitSta(val)}
            ref={ZAuditRef}
          />
        ) : null}

        {/* 表单字段、附件等 */}
        <div className='B1Tit'>
          {/* 待完善 */}
          申请信息
          <Button type='dashed'>草稿</Button>
        </div>
        <div className='B1rowAll'>
          <div className='B1row'>
            <div className='B1rowll'>
              <span> * </span>计划名称：
            </div>
            <div className='B1rowrr'>
              <Input
                readOnly={['3', '4'].includes(key)}
                placeholder='请输入内容'
                maxLength={30}
                showCount
              />
            </div>
          </div>

          <div className='B1row'>
            <div className='B1rowll'>
              <span> * </span>业务日期：
            </div>
            <div className='B1rowrr'>
              <DatePicker
                disabled={['3', '4'].includes(key)}
                allowClear={false}
                value={dayjs()}
                onChange={timeChange}
              />
            </div>
          </div>

          {B3aForm1.map(v => (
            <div className='B1row' key={v.name}>
              <div className='B1rowll'>{v.name}：</div>
              <div className='B1rowrr'>
                这是一段文本
                {['3', '4'].includes(key) && v.name === '发起人'
                  ? ' - 查看和审批后面显示创建时间'
                  : ''}
              </div>
            </div>
          ))}

          <div className='B1row B1row2'>
            <div className='B1rowll'>附件：</div>
            <div className='B1rowrr'>
              <Z3upFiles
                max={10}
                isLook={['3', '4'].includes(key)}
                ref={filesRef}
                fileCheck={false}
                dirCode={'xxxxxxx'}
                myUrl='xxxxxxxxxxxx'
                lookData={[]}
                size={500}
              />
            </div>
          </div>

          <div className='B1row'>
            <div className='B1rowll'>原因事由：</div>
            <div className='B1rowrr'>
              <Input
                readOnly={['3', '4'].includes(key)}
                placeholder='请输入内容'
                maxLength={30}
                showCount
              />
            </div>
          </div>

          <div className='B1row B1rowFull'>
            <div className='B1rowll'>备注：</div>
            <div className='B1rowrr'>
              <ZRichTexts
                check={false}
                dirCode={'xxxxxxxx'}
                isLook={['3', '4'].includes(key)}
                ref={ZRichTextRef}
                myUrl='xxxxxxxxxx'
                isOne={true}
                upAudioBtnNone={true}
              />
            </div>
          </div>
        </div>

        {/* 征集线索 */}
        <div className='B1googsBox'>
          <div className='B1Tit2'>
            <div className='B1Tit2ll'>征集线索</div>
            <div className='B1Tit2rr'>
              {['3', '4'].includes(key) ? null : (
                <Button type='primary' onClick={() => setOpenInfo({ id: -1 })}>
                  新增
                </Button>
              )}
            </div>
          </div>

          {/* 表格 */}
          <MyTable
            list={[{ id: 101 }]}
            columnsTemp={B1TableC}
            lastBtn={tableLastBtn}
            pagingInfo={false}
          />
        </div>

        {/* 相关鉴定 */}
        {['3', '4'].includes(key) ? <B1appraisal /> : null}

        {/* 申请流程 */}
        {['3', '4'].includes(key) ? (
          <B3flowTable
            tableArr={
              [
                //  待完善
              ]
            }
          />
        ) : null}
        {/* main-end */}
      </div>

      {/* 底部按钮 */}
      <div className='B1btn'>
        {key === '4' ? (
          lookBtn
        ) : (
          <>
            <Button type='primary' onClick={() => btnClickFu(1)}>
              {key === '1' ? '创建' : '保存'}
            </Button>

            {key === '1' ? (
              <Button type='primary' onClick={() => btnClickFu(2)}>
                存草稿
              </Button>
            ) : null}

            <MyPopconfirm txtK='取消' onConfirm={() => history.push('/collect')} />
          </>
        )}
      </div>

      {/* 征集线索 */}
      {openInfo.id ? (
        <B1MoAdd
          look={['3', '4'].includes(key)}
          sId={id}
          info={openInfo}
          closeFu={() => setOpenInfo({})}
          succFu={() => {}}
        />
      ) : null}
    </div>
  )
}

const MemoB1edit = React.memo(B1edit)

export default MemoB1edit
