var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { CardForm, FormUpload } from '@component/ui';
import { getManagementStatusList, getAllItem, saveAddManagementStatus, saveEditManagementStatus } from './action';
import { useRequest } from 'ahooks';
import { isEmpty, head, hasIn, omit } from 'lodash';
import { Form, message } from 'antd';
import { getUploadList, jsonString } from "@component/utils";
import { ProCard, ProForm, ProFormList, ProFormSelect, ProFormTextArea } from "@ant-design/pro-components";
import { useImperativeHandle } from "react";
import { useServiceContext } from '../../context';
export default ({ formRef, title }) => {
    const { serviceId } = useServiceContext();
    const [form] = Form.useForm();
    function f(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const data = yield getManagementStatusList(params);
            if (!isEmpty(data)) {
                const processedData = yield Promise.all(data.map(item => jsonString(item.attachments).then(attachments => (Object.assign(Object.assign({}, item), { taskItemId: item.taskItemId, attachments })))));
                return {
                    managementStatus: processedData,
                };
            }
            const a = yield getAllItem(params);
            if (!isEmpty(a)) {
                const processedA = yield Promise.all(a.map(item => jsonString(item.attachments).then(attachments => (Object.assign(Object.assign({}, item), { riskItemId: item.riskItemId, attachments })))));
                return {
                    managementStatus: processedA,
                };
            }
            return null;
        });
    }
    const { data = {}, runAsync } = useRequest(f, {
        manual: true,
    });
    function onFinish(required) {
        return __awaiter(this, void 0, void 0, function* () {
            const values = required ? yield form.validateFields() : form.getFieldsValue();
            const data = values.managementStatus;
            function getFormData(data, k) {
                return isEmpty(data) ? '' : JSON.stringify(data.map((item) => (Object.assign(Object.assign({}, item), { [k]: item[k], attachments: JSON.stringify(getUploadList(item.attachments)) }))));
            }
            if (hasIn(head(data), 'riskItemId')) {
                return saveAddManagementStatus({ safeSituationItems: getFormData(data, 'riskItemId'), serviceId });
            }
            if (hasIn(head(values.managementStatus), 'taskItemId')) {
                return saveEditManagementStatus({ reportSafes: getFormData(data, 'taskItemId') });
            }
        });
    }
    useImperativeHandle(formRef, () => {
        return {
            onFinish
        };
    });
    return (_jsx(CardForm, Object.assign({ gutter: [10, 10], title: title }, { children: _jsx(ProForm, Object.assign({ form: form, style: { width: '100%' }, layout: "horizontal", request: runAsync, params: { serviceId }, submitter: {
                render: () => null
            } }, { children: _jsx(ProFormList, Object.assign({ name: "managementStatus", style: { marginBottom: 0 }, creatorButtonProps: {
                    style: {
                        display: 'none',
                    },
                }, min: 1, copyIconProps: false, deleteIconProps: false, itemRender: ({ listDom, action }, { index }) => {
                    var _a;
                    return (_jsx(ProCard, Object.assign({ bordered: true, size: "small", style: { marginBottom: 8 }, title: (_a = data === null || data === void 0 ? void 0 : data.managementStatus[index]) === null || _a === void 0 ? void 0 : _a.question, extra: action, bodyStyle: { paddingBottom: 0 } }, { children: listDom })));
                } }, { children: (meta, index) => {
                    var _a, _b, _c, _d;
                    return (_jsxs(_Fragment, { children: [_jsx(ProFormSelect, { labelCol: {
                                    span: 4,
                                }, label: "\u4F01\u4E1A\u73B0\u72B6", name: "finalResult", rules: [{ required: true }], placeholder: "\u8BF7\u9009\u62E9\u4F01\u4E1A\u73B0\u72B6", options: (_b = (_a = data === null || data === void 0 ? void 0 : data.managementStatus[index]) === null || _a === void 0 ? void 0 : _a.answer) === null || _b === void 0 ? void 0 : _b.split('&') }), _jsx(ProFormTextArea, { labelCol: {
                                    span: 4,
                                }, placeholder: "\u8BF7\u8F93\u5165\u4E13\u5BB6\u5EFA\u8BAE", label: "\u4E13\u5BB6\u5EFA\u8BAE", name: "description" }), _jsx(FormUpload, { labelCol: {
                                    span: 4,
                                }, label: "\u9644\u4EF6", name: "attachments", title: null, max: 20, fieldProps: {
                                    accept: 'image/*',
                                }, params: {
                                    bizNo: ((_c = data === null || data === void 0 ? void 0 : data.managementStatus[index]) === null || _c === void 0 ? void 0 : _c.taskItemId) || ((_d = data === null || data === void 0 ? void 0 : data.managementStatus[index]) === null || _d === void 0 ? void 0 : _d.riskItemId),
                                    type: '14',
                                } })] }));
                } })) })) })));
};