import apiCargoowner from "../../cargoowner/api/cargoowner";
import { dictTypeSearchItems as cargoownerSearchItems } from "../../cargoowner/config/searchItems";
import { columns as cargoownerColumns } from "../../cargoowner/config/columns";
import apiSupplier from "../../supplier/api/custom";
import { dictTypeSearchItems as supplierSearchItems } from "../../supplier/config/searchItems";
import { columns as supplierColumns } from "../../supplier/config/columns";

const cargoownerConfig = {
    api: apiCargoowner.pageList,
    search: ['cargoOwnerCode', 'cargoOwnerName'],
    columns: ['cargoOwnerId', 'cargoOwnerCode', 'cargoOwnerName']
};

const supplierConfig = {
    api: apiSupplier.findAll,
    search: ['code', 'name'],
    columns: ['supplierId', 'supplierCode', 'supplierName']
};

export function genCargoowner(required, grid) {
    return [
        { hidden: true, type: "input", name: "cargoOwnerId", label: "货主ID", value: "", },
        { hidden: true, type: "input", name: "cargoOwnerCode", label: "货主编码", value: "", },
        {
            type: "searchModal", name: "cargoOwnerName", label: "货主名称", value: "",
            required: !!required,
            ...grid,
            title: '货主',
            rowKey: 'cargoOwnerId',
            targetValueKey: 'cargoOwnerName',
            modalWidth: 700,
            targetClick: true,
            remoteUrl: cargoownerConfig.api,
            remoteData: {},
            toolBar: {
                items: cargoownerSearchItems().filter(t => cargoownerConfig.search.some(tt => t.name == tt))
            },
            tableConfig: {
                columns: cargoownerColumns.filter(t => cargoownerConfig.columns.some(tt => t.key == tt))
            },
            destroyOnClose: true,
            on: {
                ok(event, value, rowData, form) {
                    form.setFieldsValue({
                        cargoOwnerName: rowData[0].cargoOwnerName,
                        cargoOwnerCode: rowData[0].cargoOwnerCode,
                        cargoOwnerId: rowData[0].cargoOwnerId,
                    })
                },
                change(event, value, rowData, form) {
                    if (!value) {
                        form.setFieldsValue({
                            cargoOwnerName: null,
                            cargoOwnerCode: null,
                            cargoOwnerId: null,
                        })
                    }
                }
            }
        }
    ];
}

export function genSupplier(required, grid) {
    return [
        { hidden: true, type: "input", name: "supplierId", label: "供应商ID", value: "", },
        { hidden: true, type: "input", name: "supplierCode", label: "供应商编码", value: "", },
        {
            type: "searchModal", name: "supplierName", label: "供应商名称", value: "",
            required: !!required,
            ...grid,
            title: '供应商',
            rowKey: 'supplierId',
            targetValueKey: 'supplierName',
            modalWidth: 700,
            targetClick: true,
            remoteUrl: supplierConfig.api,
            remoteData: {},
            toolBar: {
                items: supplierSearchItems().filter(t => supplierConfig.search.some(tt => t.name == tt))
            },
            tableConfig: {
                columns: supplierColumns.filter(t => supplierConfig.columns.some(tt => t.key == tt))
            },
            destroyOnClose: true,
            on: {
                ok(event, value, rowData, form) {
                    form.setFieldsValue({
                        supplierCode: rowData[0].supplierCode,
                        supplierName: rowData[0].supplierName,
                        supplierId: rowData[0].supplierId,
                    })
                },
                change(event, value, rowData, form) {
                    if (!value) {
                        form.setFieldsValue({
                            supplierName: null,
                            supplierCode: null,
                            supplierId: null,
                        })
                    }
                }
            }
        }
    ]
}