import React, { useState } from 'react'
import styles from './index.module.scss'
import { baseFormData, topSearch } from '@/pages/Zother/data'
import { useSelector } from 'react-redux'
import { RootState } from '@/store'
import TableList from '@/pages/Zother/TableList'
import { D2_APIgetClueList, D2_APIgetList, D2API_obj } from '@/store/action/Dmanage/D2edit'
import { baseTableC, goodsSonTableC } from '@/utils/tableData'
import SelectGoods from '@/pages/Zother/SelectGoods'
import { GoodsType, sgBaseFormDataGood, sgTopSelectDomGood } from '@/pages/Zother/SonGoodsList/data'
import AddGoods from '@/pages/Zother/AddGoods'
import { MessageFu } from '@/utils/message'
import history from '@/utils/history'

const search = topSearch('修改')

const baseFormDataRes = baseFormData()

function D2edit() {
  // 从仓库拿数据
  const tableInfo = useSelector((state: RootState) => state.D2edit.tableInfo)

  const [editShow, setEditShow] = useState(false)

  const [goodInfo, setGoodInfo] = useState({} as GoodsType)

  return (
    <div className={styles.D2edit}>
      <div className='pageTitle'>藏品修改</div>

      <TableList
        baseFormData={baseFormDataRes}
        getListAPI={D2_APIgetList}
        pageKey='edit'
        tableInfo={tableInfo}
        columnsTemp={baseTableC('修改')}
        yHeight={585}
        searchDom={search}
        // 藏品修改特有的字段
        isEdit={() => setEditShow(true)}
      />

      {/* 打开选择藏品的弹窗-单选 */}
      {editShow ? (
        <SelectGoods
          myType='藏品'
          API_getList={D2_APIgetClueList}
          closeFu={() => setEditShow(false)}
          baseFormData={sgBaseFormDataGood}
          topSelectDom={sgTopSelectDomGood}
          columnsTemp={goodsSonTableC(true)}
          oldCheckArr={[]}
          isOne={true}
          dataResFu={res => {
            if (res && res[0] && res[0].id) {
              setGoodInfo(res[0])
            }
          }}
        />
      ) : null}

      {/* 藏品编辑弹窗 */}
      {goodInfo.id ? (
        <AddGoods
          info={goodInfo}
          closeFu={() => setGoodInfo({} as GoodsType)}
          isEdit={true}
          succFu={async (val, newObj, oldObj, keyArr) => {
            const res = await D2API_obj['创建订单']()
            if (res.code === 0) {
              const dingDanObj = res.data
              const obj = {
                ...dingDanObj,
                goodIds: newObj.id + '',
                snaps: [
                  {
                    id: null,
                    goodId: newObj.id,
                    orderId: dingDanObj.id,
                    snap: JSON.stringify({
                      new: newObj,
                      old: oldObj,
                      keyArr
                    })
                  }
                ]
              }
              const res2 = await D2API_obj['草稿'](obj)
              if (res2.code === 0) {
                MessageFu.success('藏品修改-' + val + '订单成功')
                history.push(`/edit_edit/2/${dingDanObj.id}`)
              }
            }
          }}
          moduleId={0}
          fileUpInfo={{ myUrl: 'cms/order/edit/son/upload', dirCode: 'goodEditSon' }}
          APIsave={null}
        />
      ) : null}
    </div>
  )
}

const MemoD2edit = React.memo(D2edit)

export default MemoD2edit
