import React, { Component } from 'react'

import { api } from 'common/api'

import FormBody from './form'
import DetailBody from './detail'
import { Spin } from 'antd'
import { AntIcon } from 'components'

const uploads = [
    {
        key: 'applicationFormFiles',
        label: '名录申请表',
        required: true,
    },
    {
        key: 'orgBusinessLicenseFiles',
        label: '机构营业执照和法人证书',
        required: true,
    },
    {
        key: 'inspectionQualificationCertificateFiles',
        label: '建设工程质量检测资质证书或检验检测机构资质认定证书',
        required: true,
    },
    {
        key: 'officeInformationFiles',
        label: '办公场所资料(房产证、不动产登记证或房屋租赁合同等)',
        required: true,
    },
    {
        key: 'employeeCertificateFiles',
        label: '符合条件的从业人员职称证明、学历证明、劳动合同、社保证明等',
        required: true,
    },
    {
        key: 'calibrationCertificateFiles',
        label: '开展房屋安全鉴定工作必要的设备计量检定证书、校准证书',
        required: true,
    },
    {
        key: 'otherFiles',
        label: '其他资料',
    },
]

export default class index extends Component {
    state = {
        loading: true,
        data: {},
    }

    componentDidMount() {
        this.fillData()
    }

    async fillData() {
        this.setState({ loading: true })
        const id = this.props.param && this.props.param.id
        const { data } = await api.inspectionOrgDetail({ id })
        this.setState({
            loading: false,
            data,
        })
    }

    render() {
        const { loading, data } = this.state

        const { param } = this.props

        const editable =
            !data ||
            (!data.record && !data.updateRecord) || // 没有记录时需登记
            ([0, 1].includes(data.status) && data.updateStatus === -1) || // 登记中或已登记时,被退回的状态
            (param && ['register', 'edit', 'reRegistration'].includes(param.action)) // 参数标识可修改

        return (
            <Spin spinning={loading} indicator={<AntIcon type="loading" />} style={{ height: 400 }}>
                {!loading &&
                    (editable ? (
                        <FormBody data={data} uploads={uploads} action={param && param.action} />
                    ) : (
                        <DetailBody
                            data={data}
                            uploads={uploads}
                            action={param && param.action}
                            list={param && param.list}
                        />
                    ))}
            </Spin>
        )
    }
}
