import { factorySelect, shopSelectByFactory } from "@/services/system";
import { doFetch } from "@/utils/doFetch";
import { Button, Table } from "antd";
import EditTable from "@/components/EditTable";
import { useEffect, useState } from 'react'
import { useModel } from 'umi'

const EditUpload = ({ record, fid, storeId }) => {
  const [value, setvalue] = useState({
    [record.id]: record.downloadList
  });
  const { initialState, setInitialState } = useModel("@@initialState");
  console.log('====================================');
  console.log(storeId);
  console.log('====================================');
  useEffect(() => {
    if (!value) {
      return
    }
    setInitialState(s => ({
      ...s,
      submits: {
        id: fid,
        materialList: Object.keys(value)?.map?.(it => {
          let id = it;
          return {
            id,
            downloadList: value[id]?.map(its => {
              return {
                storePositionId: its?.storePositionId,
                outstroeNum: its?.outstroeNum,
                remark: its?.remark,
                materieControlNo: its?.materieControlNo
              }
            })
          }
        })
      }
    }))
  }, [value])

  return <EditTable
    maxLength={1000}
    linkconfig={{
      urlchangeval: {
        database: (params) => doFetch({ url: "/ngic-workmanship/wmsMaterieStore/queryStoreOne", params }),
        params: { "storePositionId": "linked", "materieControlNo": "linked" },
        effectresult: {
          "supplierNo": "supplierNo",
          "supplierName": "supplierName",
          "productionNum": "productionNum",
        }
      }
    }}
    style={{ marginLeft: 48 }}
    columns={[
      {
        title: "库位名称",
        dataIndex: "storePositionName",
        key: "storePositionId",
        search: false,
        valueType: "select",
        request: async () => {
          let res = await doFetch({ url: "/ngic-auth/sysStorePosition/queryByStoreId/selection", params: { storeId: storeId } });
          return res?.data?.dataList
        },
        editable: (text, record, index) => {
          return !record.materieOutstoreDetailId
        }
      },
      {
        title: "批次号/SN号",
        dataIndex: "materieControlNo",
        key: "materieControlNo",
        search: false,
        editable: (text, record, index) => {
          return !record.materieOutstoreDetailId
        }
      },
      {
        title: "下架数量",
        dataIndex: "outstroeNum",
        key: "outstroeNum",
        search: false,
        editable: (text, record, index) => {
          return !record.materieOutstoreDetailId
        }
      },
      {
        title: "供应商编号-名称",
        dataIndex: "supplierNo",
        key: "supplierNo",
        search: false,
        readonly: true,
        render: (dom, row) => {
          return (row?.supplierNo ?? '') + " - " + (row?.supplierName ?? '')
        }
      },
      {
        title: "库存数量",
        dataIndex: "productionNum",
        key: "productionNum",
        search: false,
        "readonly": true,
      },
      {
        title: "备注",
        dataIndex: "remark",
        key: "remark",
        search: false,
        editable: (text, record, index) => {
          return !record.materieOutstoreDetailId
        }
      },
      {
        title: "操作",
        valueType: "option",
        width: 70,
        render: (text, record, _, action) => [
          <a key="delete" onClick={() => { }}>
            删除
          </a>,
        ],
      }
    ]}
    value={value[record.id]}
    onChange={(vals) => {
      setvalue(s => ({
        ...s,
        [record.id]: vals
      }));
    }}
    pagination={false}
  />
}

const one = {
  "materieOutstoreNo": {
    "value": null,
    "type": "input",
    "title": "出库单号",
    "name": [
      "materieOutstoreNo"
    ],
    "required": false
  },
  "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",
          "usableNum": "usableNum"
        }
      }
    },
    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: false,
          showSearch: true
        },
        "formItemProps": () => {
          return {
            rules: [{ required: true, message: '此项为必填项' }],
          };
        },
      },
      {
        "title": <span>出库数量 <b style={{ color: "red" }}>*</b></span>,
        "dataIndex": "outstroeNum",
        "key": "outstroeNum",
        "formItemProps": () => {
          return {
            rules: [{ required: true, message: '此项为必填项' }],
          };
        },
      },
      {
        "title": "可用库存",
        "dataIndex": "usableNum",
        "key": "usableNum",
        "readonly": 'usableNum',
        "width": 88
      },
      {
        "title": "单位",
        "dataIndex": "productionUnitName",
        "key": "productionUnitName",
        "readonly": 'productionUnitName',
        "width": 60
      },
      {
        title: "操作",
        valueType: "option",
        width: 70,
        render: (text, record, _, action) => [
          <a key="delete" onClick={() => { }}>
            删除
          </a>,
        ],
      },
    ],
    rowKey: "id",
  }
},
  two = {
    "materieOutstoreNo": {
      "value": null,
      "type": "input",
      "title": "出库单号",
      "name": [
        "materieOutstoreNo"
      ],
      "required": false
    },
    "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",
            "usableNum": "usableNum"
          }
        }
      },
      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: false,
            showSearch: true
          },
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
        },
        {
          "title": <span>出库数量 <b style={{ color: "red" }}>*</b></span>,
          "dataIndex": "outstroeNum",
          "key": "outstroeNum",
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
        },
        {
          "title": "可用库存",
          "dataIndex": "usableNum",
          "key": "usableNum",
          "readonly": 'usableNum',
          "width": 88
        },
        {
          "title": "单位",
          "dataIndex": "productionUnitName",
          "key": "productionUnitName",
          "readonly": 'productionUnitName',
          "width": 60
        },
        {
          title: "操作",
          valueType: "option",
          width: 70,
          render: (text, record, _, action) => [
            <a key="delete" onClick={() => { }}>
              删除
            </a>,
          ],
        },
      ],
      rowKey: "id",
    }
  },
  three = {
    "materieOutstoreNo": {
      "value": null,
      "type": "input",
      "title": "出库单号",
      "name": [
        "materieOutstoreNo"
      ],
      "required": false
    },
    "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",
            "usableNum": "usableNum"
          }
        }
      },
      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: false,
            showSearch: true
          },
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
        },
        {
          "title": <span>出库数量 <b style={{ color: "red" }}>*</b></span>,
          "dataIndex": "outstroeNum",
          "key": "outstroeNum",
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
        },
        {
          "title": "可用库存",
          "dataIndex": "usableNum",
          "key": "usableNum",
          "readonly": 'usableNum',
          "width": 88
        },
        {
          "title": "单位",
          "dataIndex": "productionUnitName",
          "key": "productionUnitName",
          "readonly": 'productionUnitName',
          "width": 60
        },
        {
          title: "操作",
          valueType: "option",
          width: 70,
          render: (text, record, _, action) => [
            <a key="delete" onClick={() => { }}>
              删除
            </a>,
          ],
        },
      ],
      rowKey: "id",
    }
  },
  four = {
    "materieOutstoreNo": {
      "value": null,
      "type": "input",
      "title": "出库单号",
      "name": [
        "materieOutstoreNo"
      ],
      "required": false
    },
    "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",
            "usableNum": "usableNum"
          }
        }
      },
      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: false,
            showSearch: true
          },
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
        },
        {
          "title": <span>出库数量 <b style={{ color: "red" }}>*</b></span>,
          "dataIndex": "outstroeNum",
          "key": "outstroeNum",
          "formItemProps": () => {
            return {
              rules: [{ required: true, message: '此项为必填项' }],
            };
          },
        },
        {
          "title": "可用库存",
          "dataIndex": "usableNum",
          "key": "usableNum",
          "readonly": 'usableNum',
          "width": 88
        },
        {
          "title": "单位",
          "dataIndex": "productionUnitName",
          "key": "productionUnitName",
          "readonly": 'productionUnitName',
          "width": 60
        },
        {
          title: "操作",
          valueType: "option",
          width: 70,
          render: (text, record, _, action) => [
            <a key="delete" onClick={() => { }}>
              删除
            </a>,
          ],
        },
      ],
      rowKey: "id",
    }
  },
  detail = {
    totalCard: [
      //物料详情
      {
        cardTitle: "基本信息",
        itemData: [
          {
            title: "出库单号",
            key: "materieOutstoreNo",
          },
          {
            title: "出库类型",
            key: "outstoreTypeName",
          },
          {
            title: "出库仓库",
            key: "storeName",
          },
          {
            title: "相关单号",
            key: "businessNo",
          },
          {
            title: "备注",
            key: "remark",
          },
          {
            title: "创建人",
            key: "createUserName",
          },
          {
            title: "创建时间",
            key: "createTime",
          },
        ],
      },
      {
        cardTitle: "物料信息列表",
        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: "outstroeNum",
                key: "outstroeNum",
                search: false,
              },
              {
                title: "单位",
                dataIndex: "productionUnitName",
                key: "productionUnitName",
                search: false,
              },
            ],
            expandable: {
              expandedRowRender: record => <Table
                style={{ marginLeft: 48 }}
                columns={[
                  {
                    title: "库位名称",
                    dataIndex: "storePositionName",
                    key: "storePositionName",
                    search: false,
                  },
                  {
                    title: "批次号/SN号",
                    dataIndex: "materieControlNo",
                    key: "materieControlNo",
                    search: false,
                  },
                  {
                    title: "供应商编号-名称",
                    dataIndex: "supplierNo",
                    key: "supplierNo",
                    search: false,
                    render: (dom, row) => {
                      return (row?.supplierNo ?? '') + " - " + (row?.supplierName ?? '')
                    }
                  },
                  {
                    title: "下架数量",
                    dataIndex: "outstroeNum",
                    key: "outstroeNum",
                    search: false,
                  }

                ]}
                dataSource={record.downloadList}
                pagination={false}
              />,
            }
          },
        ],
      },
      {
        cardTitle: "强制关闭信息",
        itemData: [
          {
            title: "操作人",
            key: "closeUserName",
          },
          {
            title: "操作时间",
            key: "closeTime",
          }
        ],
      },
      {
        cardTitle: "下架明细",
        noPrint: true,
        itemData: [
          {
            key: "materialDownloadList",
            type: "table",
            col: { span: 24 },
            columns: [
              {
                title: "物料编码 - 名称",
                dataIndex: "materieName",
                key: "materieName",
                search: false,
                render: (dom, row) => {
                  return (row.materieCode ?? '') + " - " + (row.materieName ?? '')
                }
              },
              {
                title: "出库数量",
                dataIndex: "outstroeNum",
                key: "outstroeNum",
                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,
                  },
                  {
                    title: "批次号/SN号",
                    dataIndex: "materieControlNo",
                    key: "materieControlNo",
                    search: false,
                  },
                  {
                    title: "供应商编号 - 名称",
                    dataIndex: "supplierNo",
                    key: "supplierNo",
                    search: false,
                    render: (dom, row) => {
                      return (row.supplierNo ?? '') + " - " + (row.supplierName ?? '')
                    }
                  },
                  {
                    title: "下架数量",
                    dataIndex: "outstroeNum",
                    key: "outstroeNum",
                    search: false,
                  }
                  ,
                  {
                    title: "备注",
                    dataIndex: "remark",
                    key: "remark",
                    search: false,
                  }
                  ,
                  {
                    title: "操作人",
                    dataIndex: "uploadUserName",
                    key: "uploadUserName",
                    search: false,
                  }
                  ,
                  {
                    title: "操作时间",
                    dataIndex: "uploadTime",
                    key: "uploadTime",
                    search: false,
                  }
                ]}
                dataSource={record.downloadList}
                pagination={false}
              />,
            },
            pagination: "false"
          },
        ],
      },
    ]
  },
  dooutside = (fid, storeId) => ({
    totalCard: [
      //物料详情
      {
        cardTitle: "基本信息",
        itemData: [
          {
            title: "出库单号",
            key: "materieOutstoreNo",
          },
          {
            title: "出库类型",
            key: "outstoreTypeName",
          },
          {
            title: "出库仓库",
            key: "storeName",
          },
          {
            title: "相关单号",
            key: "businessNo",
          },
          {
            title: "备注",
            key: "remark",
          },
          {
            title: "创建人",
            key: "createUserName",
          },
          {
            title: "创建时间",
            key: "createTime",
          },
        ],
      },
      {
        cardTitle: "物料信息列表",
        extra: true,
        extrapath: "/ngic-workmanship/wmsMaterieOutstore/download",
        extrakey: "submits",//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: "outstroeNum",
                key: "outstroeNum",
                search: false,
              },
              {
                title: "单位",
                dataIndex: "productionUnitName",
                key: "productionUnitName",
                search: false,
              },
            ],
            expandable: {
              expandedRowRender: record => <EditUpload record={record} fid={fid} storeId={storeId} />,
            }
          },
        ],
      },
    ]
  });




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