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 { ProForm, ProFormTextArea, ProFormSelect, ProFormList, ProCard, ProFormItem, } from '@ant-design/pro-components';
import { FormUpload } from '@component/ui';
import { message } from 'antd';
import { saveEditManagementStatus, saveAddManagementStatus } from './action';
import { useRequest } from 'ahooks';
import { getUploadList, jsonString, themeMessage } from '@component/utils';
import { useServiceContext } from '../../context';
import { useState } from 'react';
export default ({ data = [], isFirst, refresh, formRef }) => {
    const { serviceId } = useServiceContext();
    const [value, setValue] = useState({});
    const { runAsync } = useRequest(isFirst ? saveAddManagementStatus : saveEditManagementStatus, {
        manual: true,
        onSuccess(result) {
            themeMessage(result);
            if (result.type === 'success') {
                refresh && refresh();
            }
        },
        onError(result) {
            message.error(result.message);
        },
    });
    function onFinish(values) {
        if (isFirst) {
            return runAsync({
                serviceId: serviceId,
                safeSituationItems: JSON.stringify(values.safeSituationItems.map((item) => (Object.assign(Object.assign(Object.assign({}, value), item), { attachments: JSON.stringify(getUploadList(item.attachments)) })))),
            });
        }
        return runAsync({
            reportSafes: JSON.stringify(values.reportSafes.map((item) => (Object.assign(Object.assign({}, item), { attachments: JSON.stringify(getUploadList(item.attachments)) })))),
        });
    }
    // name={isFirst ? 'safeSituationItems' : 'reportSafes'}
    function request() {
        return __awaiter(this, void 0, void 0, function* () {
            if (data) {
                if (isFirst) {
                    return {
                        safeSituationItems: data.map((item) => __awaiter(this, void 0, void 0, function* () {
                            setValue(item);
                            return {
                                riskItemId: item.riskItemId,
                                finalResult: item.finalResult,
                                description: item.description,
                                attachments: yield jsonString(item.attachments),
                            };
                        })),
                    };
                }
                return {
                    reportSafes: data.map((item) => __awaiter(this, void 0, void 0, function* () {
                        return ({
                            taskItemId: item.taskItemId,
                            finalResult: item.finalResult,
                            description: item.description,
                            attachments: yield jsonString(item.attachments),
                        });
                    })),
                };
            }
            return {};
        });
    }
    return (_jsx(ProForm, Object.assign({ form: formRef, style: { width: '100%' }, layout: "horizontal", request: request, onFinish: onFinish }, { 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[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[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[index]) === null || _c === void 0 ? void 0 : _c.taskItemId) || ((_d = data[index]) === null || _d === void 0 ? void 0 : _d.riskItemId),
                                type: '14',
                            } })] }));
            } })) })));
};