import { factorySelect, shopSelectByFactory } from "@/services/system";
import { doFetch } from "@/utils/doFetch";
import { Button, Table } from "antd";
import { EditableProTable } from "@ant-design/pro-table";
import { useEffect, useState } from 'react'
import { useModel } from 'umi'

const EditUpload = ({ record, fid, storeId }) => {
  const [value, setvalue] = useState({
    [record.id]: record.uploadList
  });
  const { initialState, setInitialState } = useModel("@@initialState");
  useEffect(() => {
    if (!value) {
      return
    }
    let newlist = Object.keys(value)?.map?.(it => {
      let id = it;
      let newArr = value[id]?.filter(it => typeof (it.id) == "number") ?? [];
      return {
        id,
        uploadList: newArr.map(its => {
          return {
            storePositionId: its?.storePositionId,
            instroeNum: its?.instroeNum,
            remark: its?.remark
          }
        })
      }
    }) ?? [];
    setInitialState(s => {
      return ({
        ...s,
        submit: {
          id: fid,
          materialList: (s.submit && s?.submit?.materialList) ? s?.submit?.materialList?.filter(it => newlist.map(item => item.id).indexOf(it.id) == -1)?.concat(newlist)?.filter(it => it.uploadList?.length > 0) : []
        }
      })
    })
  }, [value]);
  return <EditableProTable
    rowKey={"id"}
    maxLength={1000}
    style={{ marginLeft: 48 }}
    columns={[
      {
        title: <span>库位名称 <b style={{ color: "red" }}>*</b></span>,
        dataIndex: "storePositionName",
        key: "storePositionId",
        search: false,
        valueType: "select",
        request: async (params) => {
          let res = await doFetch({ url: "/ngic-auth/sysStorePosition/queryByStoreId/selection", params: { storeId: params.storeId } });
          return res?.data?.dataList
        },
        editable: (text, record, index) => {
          return !record.materieOutstoreDetailId
        },
        params: { storeId: storeId },
        "formItemProps": () => {
          return {
            rules: [{ required: true, message: '此项为必填项' }],
          };
        },
      },
      {
        title: <span>上架数量 <b style={{ color: "red" }}>*</b></span>,
        dataIndex: "instroeNum",
        key: "instroeNum",
        search: false,
        editable: (text, record, index) => {
          return !record.materieOutstoreDetailId
        },
        valueType: "digit",
        "formItemProps": () => {
          return {
            rules: [{ required: true, message: '此项为必填项' }],
          };
        },
      },
      {
        title: "备注",
        dataIndex: "remark",
        key: "remark",
        search: false,
        editable: (text, record, index) => {
          return !record.materieOutstoreDetailId
        }
      },
      {
        title: "操作",
        valueType: "option",
        width: 70,
        render: (text, record, _, action) => [
          typeof (record.id) == "number" && <a key="delete" onClick={() => { }}>
            删除
          </a>,
        ],
      }
    ]}
    value={value[record.id]}
    recordCreatorProps={{
      newRecordType: "dataSource",
      record: () => ({
        id: Date.now(),
      }),
    }}
    editable={{
      type: "multiple",
      actionRender: (row, config, defaultDoms) => {
        return [defaultDoms.delete];
      },
      onValuesChange: async (records, recordList) => {
        setvalue(s => {
          return ({
            ...s,
            [record.id]: recordList
          })
        });
      },
    }}
    pagination={false}
  />
}

const one = {
  "materieInstoreNo": {
    "value": null,
    "type": "input",
    "title": "入库单号",
    "name": [
      "materieInstoreNo"
    ],
    "required": false,
    placeholder: "不填写系统自动生成"
  },
  "storeId": {
    "value": null,
    "type": "select",
    "title": "入库仓库",
    "name": [
      "storeId"
    ],
    "required": true,
    "options": {
      database: () => doFetch({ url: "/ngic-auth/sysStore/selectionBox", params: { factoryIdList: [] } }),
      params: {}
    },
  },
  "supplierId": {
    "value": null,
    "type": "select",
    "title": "供应商",
    "name": [
      "supplierId"
    ],
    "required": true,
    "options": {
      database: () => doFetch({ url: "/ngic-auth/sysSupplier/query/selection", params: {} }),
      params: {}
    },
  },
  "businessNo": {
    "value": null,
    "type": "input",
    "title": "相关单号",
    "name": [
      "businessNo"
    ],
    "required": false
  },
  "remark": {
    "value": null,
    "type": "textarea",
    "title": "备注",
    "name": [
      "remark"
    ],
    "required": false,
    "col": {
      "span": 24
    }
  },
  "materialList": {
    value: [],
    title: "物料信息",
    type: "table",
    col: { span: 24 },
    name: ["materialList"],
    required: true,
    linkconfig: {
      urlchangeval: {//根据url接口 改变某个value
        database: (params) => doFetch({ url: "/ngic-workmanship/pmMaterie/queryUnitByMaterieId", params }),
        params: { "materieId": "linked" },
        effectresult: {
          "productionUnit": "productionUnit",//key 为列表更新值  value为response 返回值
          "productionUnitName": "productionUnitName",
          "materieControlNo": "materialCode",
          "instroeNum": "instroeNum"
        }
      }
    },
    columns: [
      {
        "title": <span>物料编码-名称 <b style={{ color: "red" }}>*</b></span>,
        "dataIndex": "materieId",
        "key": "materieId",
        "valueType": "select",
        "width": 300,
        "request": async () => {
          let res = await doFetch({ url: "/ngic-workmanship/pmMaterie/query/selectbox", params: {} })
          return res?.data?.dataList
        },
        "fieldProps": {
          allowClear: true,
          showSearch: true
        },
        "formItemProps": () => {
          return {
            rules: [{ required: true, message: '此项为必填项' }],
          };
        },
      },
      {
        "title": "批次号/SN号",
        "dataIndex": "materieControlNo",
        "key": "materieControlNo"
      },
      {
        "title": <span>入库数量 <b style={{ color: "red" }}>*</b></span>,
        "dataIndex": "instroeNum",
        "key": "instroeNum",
        valueType: "digit",
        "formItemProps": () => {
          return {
            rules: [{ required: true, message: '此项为必填项' }],
          };
        }
      },
      {
        "title": "库存单位",
        "dataIndex": "productionUnitName",
        "key": "productionUnitName",
        "readonly": 'productionUnitName',
        "width": 88
      },
      {
        title: "操作",
        valueType: "option",
        width: 70,
        render: (text, record, _, action) => [
          <a key="delete" onClick={() => { }}>
            删除
          </a>,
        ],
      },
    ],
    rowKey: "id",
  }
},
  two = {
    "materieInstoreNo": {
      "value": null,
      "type": "input",
      "title": "入库单号",
      "name": [
        "materieInstoreNo"
      ],
      "required": false,
      placeholder: "不填写系统自动生成"
    },
    "storeId": {
      "value": null,
      "type": "select",
      "title": "入库仓库",
      "name": [
        "storeId"
      ],
      "required": true,
      "options": {
        database: () => doFetch({ url: "/ngic-auth/sysStore/selectionBox", params: { factoryIdList: [] } }),
        params: {}
      },
    },
    "supplierId": {
      "value": "2022070124566476657",
      "type": "select",
      "title": "供应商",
      "name": [
        "supplierId"
      ],
      "required": true,
      "disabled": true,
      "options": [
        {
          value: "2022070124566476657",
          label: "南高(默认)"
        }
      ],
    },
    "businessNo": {
      "value": null,
      "type": "input",
      "title": "相关单号",
      "name": [
        "businessNo"
      ],
      "required": false
    },
    "remark": {
      "value": null,
      "type": "textarea",
      "title": "备注",
      "name": [
        "remark"
      ],
      "required": false,
      "col": {
        "span": 24
      }
    },
    "materialList": {
      value: [],
      title: "物料信息",
      type: "table",
      col: { span: 24 },
      name: ["materialList"],
      required: true,
      linkconfig: {
        urlchangeval: {//根据url接口 改变某个value
          database: (params) => doFetch({ url: "/ngic-workmanship/pmMaterie/queryUnitByMaterieId", params }),
          params: { "materieId": "linked" },
          effectresult: {
            "productionUnit": "productionUnit",//key 为列表更新值  value为response 返回值
            "productionUnitName": "productionUnitName",
            "materieControlNo": "materialCode",
            "instroeNum": "instroeNum"
          }
        }
      },
      columns: [
        {
          "title": <span>物料编码-名称 <b style={{ color: "red" }}>*</b></span>,
          "dataIndex": "materieId",
          "key": "materieId",
          "valueType": "select",
          "width": 300,
          "request": async () => {
            let res = await doFetch({ url: "/ngic-workmanship/pmMaterie/query/selectbox", params: {} })
            return res?.data?.dataList
          },
          "fieldProps": {
            allowClear: true,
            showSearch: true
          },
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
        },
        {
          "title": "批次号/SN号",
          "dataIndex": "materieControlNo",
          "key": "materieControlNo"
        },
        {
          "title": <span>入库数量 <b style={{ color: "red" }}>*</b></span>,
          "dataIndex": "instroeNum",
          "key": "instroeNum",
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
          valueType: "digit"
        },
        {
          "title": "库存单位",
          "dataIndex": "productionUnitName",
          "key": "productionUnitName",
          "readonly": 'productionUnitName',
          "width": 88
        },
        {
          title: "操作",
          valueType: "option",
          width: 70,
          render: (text, record, _, action) => [
            <a key="delete" onClick={() => { }}>
              删除
            </a>,
          ],
        },
      ],
      rowKey: "id",
    }
  },
  three = {
    "materieInstoreNo": {
      "value": null,
      "type": "input",
      "title": "入库单号",
      "name": [
        "materieInstoreNo"
      ],
      "required": false,
      placeholder: "不填写系统自动生成"
    },
    "storeId": {
      "value": null,
      "type": "select",
      "title": "入库仓库",
      "name": [
        "storeId"
      ],
      "required": true,
      "options": {
        database: () => doFetch({ url: "/ngic-auth/sysStore/selectionBox", params: { factoryIdList: [] } }),
        params: {}
      },
    },
    "businessNo": {
      "value": null,
      "type": "input",
      "title": "相关单号",
      "name": [
        "businessNo"
      ],
      "required": false
    },
    "remark": {
      "value": null,
      "type": "textarea",
      "title": "备注",
      "name": [
        "remark"
      ],
      "required": false,
      "col": {
        "span": 24
      }
    },
    "materialList": {
      value: [],
      title: "物料信息",
      type: "table",
      col: { span: 24 },
      name: ["materialList"],
      required: true,
      linkconfig: {
        urlchangeval: {//根据url接口 改变某个value
          database: (params) => doFetch({ url: "/ngic-workmanship/pmMaterie/queryUnitByMaterieId", params }),
          params: { "materieId": "linked" },
          effectresult: {
            "productionUnit": "productionUnit",//key 为列表更新值  value为response 返回值
            "productionUnitName": "productionUnitName",
            "materieControlNo": "materialCode",
            "instroeNum": "instroeNum",
            "supplierId": "supplierId"
          }
        }
      },
      columns: [
        {
          "title": <span>物料编码-名称 <b style={{ color: "red" }}>*</b></span>,
          "dataIndex": "materieId",
          "key": "materieId",
          "valueType": "select",
          "width": 300,
          "request": async () => {
            let res = await doFetch({ url: "/ngic-workmanship/pmMaterie/query/selectbox", params: {} })
            return res?.data?.dataList
          },
          "fieldProps": {
            allowClear: true,
            showSearch: true
          },
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
        },
        {
          "title": <span>供应商编号-名称 <b style={{ color: "red" }}>*</b></span>,
          "dataIndex": "supplierId",
          "key": "supplierId",
          "valueType": "select",
          "width": 300,
          "request": async () => {
            let res = await doFetch({ url: "/ngic-auth/sysSupplier/query/selection/withNo", params: {} })
            return res?.data?.dataList
          },
          "fieldProps": {
            allowClear: true,
            showSearch: true
          },
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
        },
        {
          "title": "批次号/SN号",
          "dataIndex": "materieControlNo",
          "key": "materieControlNo"
        },
        {
          "title": <span>入库数量 <b style={{ color: "red" }}>*</b></span>,
          "dataIndex": "instroeNum",
          "key": "instroeNum",
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
          valueType: "digit"
        },
        {
          "title": "库存单位",
          "dataIndex": "productionUnitName",
          "key": "productionUnitName",
          "readonly": 'productionUnitName',
          "width": 88
        },
        {
          title: "操作",
          valueType: "option",
          width: 70,
          render: (text, record, _, action) => [
            <a key="delete" onClick={() => { }}>
              删除
            </a>,
          ],
        },
      ],
      rowKey: "id",
    }
  },
  four = {
    "materieInstoreNo": {
      "value": null,
      "type": "input",
      "title": "入库单号",
      "name": [
        "materieInstoreNo"
      ],
      "required": false,
      placeholder: "不填写系统自动生成"
    },
    "storeId": {
      "value": null,
      "type": "select",
      "title": "入库仓库",
      "name": [
        "storeId"
      ],
      "required": true,
      "options": {
        database: () => doFetch({ url: "/ngic-auth/sysStore/selectionBox", params: { factoryIdList: [] } }),
        params: {}
      },
    },
    "businessNo": {
      "value": null,
      "type": "input",
      "title": "相关单号",
      "name": [
        "businessNo"
      ],
      "required": false
    },
    "remark": {
      "value": null,
      "type": "textarea",
      "title": "备注",
      "name": [
        "remark"
      ],
      "required": false,
      "col": {
        "span": 24
      }
    },
    "materialList": {
      value: [],
      title: "物料信息",
      type: "table",
      col: { span: 24 },
      name: ["materialList"],
      required: true,
      linkconfig: {
        urlchangeval: {//根据url接口 改变某个value
          database: (params) => doFetch({ url: "/ngic-workmanship/pmMaterie/queryUnitByMaterieId", params }),
          params: { "materieId": "linked" },
          effectresult: {
            "productionUnit": "productionUnit",//key 为列表更新值  value为response 返回值
            "productionUnitName": "productionUnitName",
            "materieControlNo": "materialCode",
            "instroeNum": "instroeNum",
            "supplierId": "supplierId"
          }
        }
      },
      columns: [
        {
          "title": <span>物料编码-名称 <b style={{ color: "red" }}>*</b></span>,
          "dataIndex": "materieId",
          "key": "materieId",
          "valueType": "select",
          "width": 300,
          "request": async () => {
            let res = await doFetch({ url: "/ngic-workmanship/pmMaterie/query/selectbox", params: {} })
            return res?.data?.dataList
          },
          "fieldProps": {
            allowClear: true,
            showSearch: true
          },
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
        },
        {
          "title": <span>供应商编号-名称 <b style={{ color: "red" }}>*</b></span>,
          "dataIndex": "supplierId",
          "key": "supplierId",
          "valueType": "select",
          "width": 300,
          "request": async () => {
            let res = await doFetch({ url: "/ngic-auth/sysSupplier/query/selection/withNo", params: {} })
            return res?.data?.dataList
          },
          "fieldProps": {
            allowClear: true,
            showSearch: true
          },
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
        },
        {
          "title": "批次号/SN号",
          "dataIndex": "materieControlNo",
          "key": "materieControlNo"
        },
        {
          "title": <span>入库数量 <b style={{ color: "red" }}>*</b></span>,
          "dataIndex": "instroeNum",
          "key": "instroeNum",
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
          valueType: "digit"
        },
        {
          "title": "库存单位",
          "dataIndex": "productionUnitName",
          "key": "productionUnitName",
          "readonly": 'productionUnitName',
          "width": 88
        },
        {
          title: "操作",
          valueType: "option",
          width: 70,
          render: (text, record, _, action) => [
            <a key="delete" onClick={() => { }}>
              删除
            </a>,
          ],
        },
      ],
      rowKey: "id",
    }
  },
  detail = (setselected, dom) => ({
    totalCard: [
      //物料详情
      {
        cardTitle: "基本信息",
        itemData: [
          {
            title: "入库单号",
            key: "materieInstoreNo",
          },
          {
            title: "入库类型",
            key: "instoreTypeName",
          },
          {
            title: "入库仓库",
            key: "storeName",
          },
          {
            title: "相关单号",
            key: "businessNo",
          },
          {
            title: "创建人",
            key: "createUserName",
          },
          {
            title: "创建时间",
            key: "createTime",
          }, {
            title: "状态",
            key: "statusName",
          },
          {
            title: "完成时间",
            key: "finishTime",
          },
          {
            title: "备注",
            key: "remark",
            noshow: "100%",
          },
          {
            title: "工单条形码",
            noshow: true,
            key: "qrCodeUrl",
            type: "img",
            width: 100,
          },
        ],
      },
      {
        cardTitle: "物料信息列表",
        extraContent: dom,
        itemData: [
          {
            key: "materialList",
            type: "table",
            col: { span: 24 },
            columns: [
              {
                title: "物料编码 - 名称",
                dataIndex: "materieName",
                key: "materieName",
                search: false,
                render: (dom, row) => {
                  return (row.materieCode ?? '') + " - " + (row.materieName ?? '')
                }
              },
              {
                title: "供应商编号 - 名称",
                dataIndex: "supplierNo",
                key: "supplierNo",
                search: false,
                render: (dom, row) => {
                  return (row.supplierNo ?? '') + " - " + (row.supplierName ?? '')
                }
              },
              {
                title: "批次号/SN号",
                dataIndex: "materieControlNo",
                key: "materieControlNo",
                search: false,
              },
              {
                title: "入库数量",
                dataIndex: "instroeNum",
                key: "instroeNum",
                search: false,
              },
              {
                title: "未上架数量",
                dataIndex: "remainderNums",
                key: "remainderNums",
                search: false,
              },
              {
                title: "库存单位",
                dataIndex: "productionUnitName",
                key: "productionUnitName",
                search: false,
              },
            ],
            expandable: {
              defaultExpandAllRows: true,
              expandedRowRender: record => <Table
                style={{ marginLeft: 48 }}
                columns={[
                  {
                    title: "库位名称",
                    dataIndex: "storePositionName",
                    key: "storePositionName",
                    search: false,
                    "formItemProps": () => {
                      return {
                        rules: [{ required: true, message: '此项为必填项' }],
                      };
                    },
                  },
                  {
                    title: "上架数量",
                    dataIndex: "instroeNum",
                    key: "instroeNum",
                    search: false,
                    "formItemProps": () => {
                      return {
                        rules: [{ required: true, message: '此项为必填项' }],
                      };
                    },
                  }
                ]}
                dataSource={record.uploadList}
                pagination={false}
              />,
            },
            pagination: "false",
            rowKey: "id",
            rowSelection: {
              onChange: (selectedRowKeys, selectedRows) => {
                setselected(selectedRows)
              },
            }
          },
        ],
      },
      {
        cardTitle: "强制关单信息",
        itemData: [
          {
            title: "关单人",
            key: "closeUserName",
          },
          {
            title: "关单时间",
            key: "closeTime",
          }
        ],
      },
      {
        cardTitle: "上架明细",
        noPrint: true,
        itemData: [
          {
            key: "materialUploadList",
            type: "table",
            col: { span: 24 },
            columns: [
              {
                title: "物料编码 - 名称",
                dataIndex: "materieName",
                key: "materieName",
                search: false,
                render: (dom, row) => {
                  return (row.materieCode ?? '') + " - " + (row.materieName ?? '')
                }
              },
              {
                title: "供应商编号 - 名称",
                dataIndex: "supplierNo",
                key: "supplierNo",
                search: false,
                render: (dom, row) => {
                  return (row.supplierNo ?? '') + " - " + (row.supplierName ?? '')
                }
              },
              {
                title: "批次号/SN号",
                dataIndex: "materieControlNo",
                key: "materieControlNo",
                search: false,
              },
              {
                title: "入库数量",
                dataIndex: "instroeNum",
                key: "instroeNum",
                search: false,
              },
              {
                title: "库存单位",
                dataIndex: "productionUnitName",
                key: "productionUnitName",
                search: false,
              },
            ],
            expandable: {
              defaultExpandAllRows: true,
              expandedRowRender: record => <Table
                style={{ marginLeft: 48 }}
                columns={[
                  {
                    title: "库位名称",
                    dataIndex: "storePositionName",
                    key: "storePositionName",
                    search: false,
                    "formItemProps": () => {
                      return {
                        rules: [{ required: true, message: '此项为必填项' }],
                      };
                    },
                  },
                  {
                    title: "上架数量",
                    dataIndex: "instroeNum",
                    key: "instroeNum",
                    search: false,
                    "formItemProps": () => {
                      return {
                        rules: [{ required: true, message: '此项为必填项' }],
                      };
                    },
                  }
                  ,
                  {
                    title: "备注",
                    dataIndex: "remark",
                    key: "remark",
                    search: false,
                  }
                  ,
                  {
                    title: "操作人",
                    dataIndex: "uploadUserName",
                    key: "uploadUserName",
                    search: false,
                  }
                  ,
                  {
                    title: "操作时间",
                    dataIndex: "uploadTime",
                    key: "uploadTime",
                    search: false,
                  }
                ]}
                dataSource={record.uploadList}
                pagination={false}
              />,
            },
            pagination: "false"
          },
        ],
      },
    ]
  }), doinsert = (fid, storeId) => ({
    totalCard: [
      //物料详情
      {
        cardTitle: "基本信息",
        itemData: [
          {
            title: "入库单号",
            key: "materieInstoreNo",
          },
          {
            title: "入库类型",
            key: "instoreTypeName",
          },
          {
            title: "入库仓库",
            key: "storeName",
          },
          {
            title: "相关单号",
            key: "businessNo",
          },
          {
            title: "备注",
            key: "remark",
          },
          {
            title: "创建人",
            key: "createUserName",
          },
          {
            title: "创建时间",
            key: "createTime",
          },
          {
            title: "状态",
            key: "statusName",
          }
        ],
      },
      {
        cardTitle: "物料信息列表",
        extra: true,
        extrapath: "/ngic-workmanship/wmsMaterieInstore/upload",
        extrakey: "submit",//redux key
        itemData: [
          {
            key: "materialList",
            type: "table",
            col: { span: 24 },
            columns: [
              {
                title: "物料编码 - 名称",
                dataIndex: "materieName",
                key: "materieName",
                search: false,
                render: (dom, row) => {
                  return (row.materieCode ?? '') + " - " + (row.materieName ?? '')
                }
              },
              {
                title: "供应商编号 - 名称",
                dataIndex: "supplierNo",
                key: "supplierNo",
                search: false,
                render: (dom, row) => {
                  return (row.supplierNo ?? '') + " - " + (row.supplierName ?? '')
                }
              },
              {
                title: "批次号/SN号",
                dataIndex: "materieControlNo",
                key: "materieControlNo",
                search: false,
              },
              {
                title: "入库数量",
                dataIndex: "instroeNum",
                key: "instroeNum",
                search: false
              },
              {
                title: "未上架数量",
                dataIndex: "remainderNums",
                key: "remainderNums",
                search: false,
              },
              {
                title: "库存单位",
                dataIndex: "productionUnitName",
                key: "productionUnitName",
                search: false,
              },
            ],
            expandable: {
              expandedRowRender: record => <EditUpload record={record} fid={fid} storeId={storeId} />,
            },
            rowKey: "id"
          },
        ],
      },
    ]
  });




export default {
  one,
  two,
  three,
  four,
  detail,
  doinsert
};
