import { connect } from "umi";
import type { ConnectState } from "@/models/connect";
import ProForm, { FormInstance, ProFormDatePicker, ProFormDigit, ProFormSelect, ProFormText, ProFormTextArea } from "@ant-design/pro-form";
import { useImperativeHandle, useRef, useState } from "react";
import { Col, Row } from "antd";
import moment from 'moment'
import { handleGetENDACCOUNTModel } from "../../list/service";
import { handleGetServerpartTree } from "@/pages/basicManage/serverpartAssets/service";
import { handleGetServerpartShopList, handleGetServiceShopList } from "../../service";

type DetailProps = {
    parentRow: any
    currentUser?: any
    onRef: any
}
const DetailForm = ({ parentRow, currentUser, onRef }: DetailProps) => {

    const formRef = useRef<FormInstance>();
    // 当前选择服务区所对应的门店列表
    const [shopList, setShopList] = useState<any>()
    // 查到的详情
    const [formDetail, setFormDetail] = useState<any>()

    // 拿到当前服务区的门店列表
    const handleGetShopList = async (id: any) => {
        const req = {
            SearchParameter: {
                SERVERPART_IDS: id,
                ISVALID: 1
            },
            PageIndex: 1,
            PageSize: 999999,
            SortStr: "SHOPREGION,SHOPTRADE,SHOPSHORTNAME,SHOPCODE"
        }
        const data = await handleGetServerpartShopList(req)
        console.log('shop', data);
        let list: any = []
        if (data && data.length > 0) {
            data.forEach((item: any) => {
                list.push({ label: item.SHOPNAME, value: `${item.SHOPNAME}-${item.SHOPCODE}` })
            })
        }
        setShopList(list)
    }

    useImperativeHandle(onRef, () => ({
        formRef,
        formDetail
    }));

    return (
        <div>
            <ProForm
                formRef={formRef}
                submitter={false}
                request={async () => {
                    console.log('parentRow', parentRow);

                    if (parentRow?.ENDACCOUNT_ID) {
                        const data = await handleGetENDACCOUNTModel({ ENDACCOUNTId: parentRow?.ENDACCOUNT_ID || '' })
                        console.log('detail', data);
                        handleGetShopList(data?.SERVERPART_ID)
                        let obj: any = {
                            ...data,
                            DESCRIPTION_DATE: data?.DESCRIPTION_DATE ? moment(data?.DESCRIPTION_DATE).format('YYYY-MM-DD HH:mm:ss') : '',
                            SERVERPART_ID: `${data?.SERVERPART_NAME}-${data?.SERVERPART_ID}`,
                            SHOPCODE: `${data?.SHOPNAME}-${data?.SHOPCODE}`,
                        }
                        setFormDetail(data)
                        return obj
                    }
                    return {}
                }}
            >
                <Row gutter={16}>
                    <Col span={8}>
                        <ProFormSelect
                            name={"SERVERPART_ID"}
                            label={"服务区名称"}
                            readonly={parentRow?.ENDACCOUNT_ID}
                            request={async () => {
                                const req: any = {
                                    ProvinceCode: currentUser?.ProvinceCode,
                                    ServerpartType: 1000,
                                    ShowWholePower: false,
                                    ShowSPRegion: false,
                                    ShowRoyalty: false,
                                    ShowCompactCount: false,
                                }
                                const data = await handleGetServerpartTree(req)
                                let list: any = []
                                // 测试服务区放最后
                                let testObj: any = {}
                                if (data && data.length > 0) {
                                    data.forEach((item: any) => {
                                        if (item.value !== 424) {
                                            if (item.value === 586) {
                                                testObj = { label: item.label, value: `${item.label}-${item.value}` }
                                            } else {
                                                list.push({ label: item.label, value: `${item.label}-${item.value}` })
                                            }
                                        }
                                    })
                                }
                                console.log('list', list);
                                list.push(testObj)
                                return list
                            }}
                            fieldProps={{
                                showSearch: true,
                                onChange: (e: any) => {
                                    if (e) {
                                        let [name, value] = e.split('-')
                                        handleGetShopList(value)
                                    } else {
                                        setShopList([])
                                    }
                                }
                            }}
                            rules={[
                                {
                                    required: true,
                                    message: '请选择服务区！'
                                }
                            ]}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormSelect
                            name={"SHOPCODE"}
                            label={"门店名称"}
                            options={shopList}
                            readonly={parentRow?.ENDACCOUNT_ID}
                            fieldProps={{
                                showSearch: true
                            }}
                            rules={[
                                {
                                    required: true,
                                    message: '请选择门店！'
                                }
                            ]}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormText
                            name={"MACHINECODE"}
                            label={"收银机号"}
                            readonly={parentRow?.ENDACCOUNT_ID}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormDatePicker
                            name={"ENDACCOUNT_STARTDATE"}
                            label={"起始时间"}
                            readonly={parentRow?.ENDACCOUNT_ID}
                            rules={[
                                {
                                    required: true,
                                    message: '请选择起始时间！'
                                }
                            ]}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormDatePicker
                            name={"ENDACCOUNT_DATE"}
                            label={"结账时间"}
                            readonly={parentRow?.ENDACCOUNT_ID}
                            rules={[
                                {
                                    required: true,
                                    message: '请选择结账时间！'
                                }
                            ]}
                        />
                    </Col>

                    <Col span={8}>
                        <ProFormDigit
                            label={"客单数量"}
                            name={"TICKETCOUNT"}
                            readonly={parentRow?.ENDACCOUNT_ID}
                            fieldProps={{
                                addonAfter: false
                            }}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormDigit
                            label={"销售数量"}
                            name={"TOTALCOUNT"}
                            readonly={parentRow?.ENDACCOUNT_ID}
                            fieldProps={{
                                addonAfter: false
                            }}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormDigit
                            label={"销售金额"}
                            readonly={parentRow?.ENDACCOUNT_ID}
                            name={"TOTALSELLAMOUNT"}
                            fieldProps={{
                                addonAfter: false
                            }}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormDigit
                            label={"优惠金额"}
                            name={"TOTALOFFAMOUNT"}
                            readonly={parentRow?.ENDACCOUNT_ID}
                            fieldProps={{
                                addonAfter: false
                            }}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormDigit
                            label={"现金支付"}
                            name={"CASH"}
                            readonly={parentRow?.ENDACCOUNT_ID}
                            fieldProps={{
                                addonAfter: false
                            }}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormText
                            label={"信用卡"}
                            name={"CREDITCARD"}
                            readonly={parentRow?.ENDACCOUNT_ID}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormText
                            label={"消费券"}
                            name={"COSTBILL"}
                            readonly={parentRow?.ENDACCOUNT_ID}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormText
                            label={"其他支付"}
                            name={"OTHERPAY"}
                            readonly={parentRow?.ENDACCOUNT_ID}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormDigit
                            label={"实收金额"}
                            name={"CASHPAY"}
                            fieldProps={{
                                addonAfter: false
                            }}
                            readonly={parentRow?.ENDACCOUNT_ID}
                            rules={[
                                {
                                    required: true,
                                    message: '请输入实收金额'
                                }
                            ]}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormDigit
                            label={"缴款金额"}
                            name={"CASHPAY_DOWNLORD"}
                            fieldProps={{
                                addonAfter: false
                            }}
                            readonly={parentRow?.ENDACCOUNT_ID}
                            rules={[
                                {
                                    required: true,
                                    message: '请输入实收金额'
                                }
                            ]}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormText
                            name={"DESCRIPTION_STAFF"}
                            label={"上传人员"}
                            disabled
                            readonly={parentRow?.ENDACCOUNT_ID}
                            initialValue={`${currentUser?.Name}【补】`}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormText
                            name={"DESCRIPTION_DATE"}
                            label={"上传时间"}
                            disabled
                            readonly={parentRow?.ENDACCOUNT_ID}
                            initialValue={moment().format('YYYY-MM-DD')}
                        />
                    </Col>
                    <Col span={24}>
                        <ProFormTextArea
                            name={"DIFFERENCE_REASON"}
                            label={"产生原因"}
                            readonly={parentRow?.ENDACCOUNT_ID}
                            rules={[
                                {
                                    required: true,
                                    message: '请输入产生原因！'
                                }
                            ]}
                        />
                    </Col>
                </Row>


            </ProForm>
        </div>
    )
}

export default connect(({ user, }: ConnectState) => ({
    currentUser: user.currentUser,
}))(DetailForm);
