import AutoTable from '@/components/AutoTable/mtable';

function ExtraTable({ dataSource }) {
  return (
    <div style={{ padding: '4px 4px 4px 36px' }}>
      <AutoTable
        dataSource={dataSource}
        pagination={false}
        resizeable={false}
        columns={[
          { title: '供应商编号', dataIndex: 'supplierNo', key: 'supplierNo', width: 200 },
          { title: '供应商名称', dataIndex: 'supplierName', key: 'supplierName', width: 200 },
          {
            title: '库存数量',
            dataIndex: 'actualStock',
            key: 'actualStock',
            hideInSearch: true,
            width: 200,
          },
          { title: '下架数量', dataIndex: 'operateNum', key: 'operateNum', hideInSearch: true },
        ]}
      />
    </div>
  );
}

function getcolumns(setdrawer) {
  const columns = [
    {
      title: '备件料号',
      dataIndex: 'sparePartNo',
      key: 'sparePartNo',
      editable: false,
      width: 235,
    },
    {
      title: '备件名称',
      dataIndex: 'sparePartName',
      key: 'sparePartName',
      editable: false,
      width: 200,
    },
    {
      title: '可用库存',
      dataIndex: 'usedStock',
      key: 'usedStock',
      editable: false,
      hideInSearch: true,
      width: 200,
    },
    {
      title: '出库数量',
      dataIndex: 'operateNum',
      key: 'operateNum',
      hideInSearch: true,
      valueType: 'digit',
      fieldProps: {
        precision: 3,
      },
      width: 200,
    },
    {
      title: '库存单位',
      dataIndex: 'unit',
      editable: false,
      hideInSearch: true,
    },
  ];

  return [
    {
      tab: '未完成',
      key: '1',
      columns: [
        {
          title: '出库单号',
          dataIndex: 'taskNo',
          key: 'taskNo',
          fieldProps: {
            placeholder: '不填系统自动生成',
          },
          render: (_, row) => {
            return (
              <a
                onClick={() => {
                  setdrawer((s) => ({
                    ...s,
                    open: true,
                    item: row,
                    title: '详情',
                    val: 'detail',
                    title: '详细信息',
                  }));
                }}
              >
                {row?.taskNo}
              </a>
            );
          },
        },
        {
          title: '出库类型',
          dataIndex: 'outTypeName',
          key: 'outType',
          formItemProps: {
            rules: [
              {
                required: true,
                message: '此项为必填项',
              },
            ],
          },
          valueType: 'select',
          options: [
            {
              label: '销售出库',
              value: '1',
            },
            {
              label: '报废出库',
              value: '2',
            },
            {
              label: '其它出库',
              value: '3',
            },
          ],
          searchOptions: [
            {
              label: '销售出库',
              value: '1',
            },
            {
              label: '报废出库',
              value: '2',
            },
            {
              label: '其它出库',
              value: '3',
            },
            {
              label: '备件申领出库',
              value: '4',
            },
          ],
          fieldProps: {
            dropdownMatchSelectWidth: 120,
          },
        },
        {
          title: '入库仓库',
          dataIndex: 'inWarehouseName',
          key: 'inWarehouseName',
          formItemProps: {
            rules: [
              {
                required: true,
                message: '此项为必填项',
              },
            ],
          },
        },
        {
          title: '出库仓库',
          dataIndex: 'nosubmits',
          initialValue: '备件仓库',
          valueType: 'nosubmit',
          hideInDescriptions: true,
        },
        {
          title: '出库仓库',
          dataIndex: 'outWarehouseName',
          key: 'outWarehouseName',
          hideInForm: true,
        },
        {
          title: '备件种类',
          dataIndex: 'spareTypeNum',
          key: 'spareTypeNum',
          hideInSearch: true,
          hideInForm: true,
        },
        {
          title: '相关单号',
          dataIndex: 'relationNo',
          key: 'relationNo',
        },
        {
          title: '备注',
          dataIndex: 'remark',
          key: 'remark',
          colProps: {
            span: 24,
          },
          hideInSearch: true,
          valueType: 'textarea',
        },
        {
          title: '创建人',
          dataIndex: 'createUserName',
          key: 'createUserId',
          hideInForm: true,
          valueType: 'select',
          fieldProps: {
            showSearch: true,
          },
          options: {
            path: '/auth/sysUser/selection',
            params: {},
          },
          hideInForm: true,
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          key: 'createTimeList',
          fieldProps: {
            showTime: {
              format: 'HH:mm',
            },
            format: 'YYYY-MM-DD HH:mm',
          },
          hideInForm: true,
          valueType: 'dateRange',
          render: (text, row) => {
            return <span>{row?.createTime}</span>;
          },
          hideInForm: true,
        },
        {
          title: '工单状态',
          dataIndex: 'statusName',
          key: 'statusName',
          span: 3,
          hideInSearch: true,
          hideInTable: true,
          hideInForm: true,
        },
        {
          title: (
            <div>
              <span style={{ color: 'red' }}>* </span>备件信息
            </div>
          ),
          dataIndex: 'details',
          key: 'details',
          valueType: 'split',
        },
        {
          dataIndex: 'taskSpareList',
          key: 'taskSpareList',
          valueType: 'formSelectList',
          colProps: {
            span: 24,
          },
          columns,
          span: 12,
          path: '/sparepart/spareStock/queryByOperate',
          hideInSearch: true,
          hideInTable: true,
          rowName: 'sparePartName',
          render: (text, row) => {
            return (
              <AutoTable
                columns={columns
                  ?.filter((it) => it.key != 'usedStock')
                  ?.map((it) => ({
                    ...it,
                    hideInSearch: true,
                  }))}
                dataSource={row?.taskSpareList}
                resizeable={false}
                expandable={{
                  expandedRowRender: (record) => (
                    <ExtraTable dataSource={record?.taskSpareSupplierList ?? []} />
                  ),
                }}
              />
            );
          },
        },
      ],
      pathconfig: {
        enableadd: true,
        enableedit: true,
        enabledelete: true,
        enabledetail: true,
        add: '/sparepart/outWarehouseTask/save',
        edit: '',
        list: '/sparepart/outWarehouseTask/queryNoComplete',
        delete: '',
        detail: '/sparepart/outWarehouseTask/queryDetails',
      },
    },
    {
      tab: '已完成',
      key: '2',
      columns: [
        {
          title: '出库单号',
          dataIndex: 'taskNo',
          key: 'taskNo',
          fieldProps: {
            placeholder: '不填系统自动生成',
          },
          render: (_, row) => {
            return (
              <a
                onClick={() => {
                  setdrawer((s) => ({
                    ...s,
                    open: true,
                    item: row,
                    title: '详情',
                    val: 'detail',
                    title: '详细信息',
                  }));
                }}
              >
                {row?.taskNo}
              </a>
            );
          },
        },
        {
          title: '出库类型',
          dataIndex: 'outTypeName',
          key: 'outType',
          formItemProps: {
            rules: [
              {
                required: true,
                message: '此项为必填项',
              },
            ],
          },
          valueType: 'select',
          options: [
            {
              label: '销售出库',
              value: '1',
            },
            {
              label: '报废出库',
              value: '2',
            },
            {
              label: '其它出库',
              value: '3',
            },
          ],
          searchOptions: [
            {
              label: '销售出库',
              value: '1',
            },
            {
              label: '报废出库',
              value: '2',
            },
            {
              label: '其它出库',
              value: '3',
            },
            {
              label: '备件申领出库',
              value: '4',
            },
          ],
          fieldProps: {
            dropdownMatchSelectWidth: 120,
          },
        },
        {
          title: '入库仓库',
          dataIndex: 'inWarehouseName',
          key: 'inWarehouseName',
          formItemProps: {
            rules: [
              {
                required: true,
                message: '此项为必填项',
              },
            ],
          },
        },
        {
          title: '出库仓库',
          dataIndex: 'nosubmits',
          initialValue: '备件仓库',
          valueType: 'nosubmit',
          hideInDescriptions: true,
        },
        {
          title: '出库仓库',
          dataIndex: 'outWarehouseName',
          key: 'outWarehouseName',
          hideInForm: true,
        },
        {
          title: '备件种类',
          dataIndex: 'spareTypeNum',
          key: 'spareTypeNum',
          hideInSearch: true,
          hideInForm: true,
        },
        {
          title: '相关单号',
          dataIndex: 'relationNo',
          key: 'relationNo',
        },
        {
          title: '备注',
          dataIndex: 'remark',
          key: 'remark',
          colProps: {
            span: 24,
          },
          hideInSearch: true,
          valueType: 'textarea',
        },
        {
          title: '创建人',
          dataIndex: 'createUserName',
          key: 'createUserId',
          hideInForm: true,
          valueType: 'select',
          fieldProps: {
            showSearch: true,
          },
          options: {
            path: '/auth/sysUser/selection',
            params: {},
          },
          hideInForm: true,
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          key: 'createTimeList',
          fieldProps: {
            showTime: {
              format: 'HH:mm',
            },
            format: 'YYYY-MM-DD HH:mm',
          },
          hideInForm: true,
          valueType: 'dateRange',
          render: (text, row) => {
            return <span>{row?.createTime}</span>;
          },
          hideInForm: true,
        },
        {
          title: '完成时间',
          dataIndex: 'completeTime',
          key: 'completeTimeList',
          hideInForm: true,
          fieldProps: {
            showTime: {
              format: 'HH:mm',
            },
            format: 'YYYY-MM-DD HH:mm',
          },
          valueType: 'dateRange',
          render: (text, row) => {
            return <span>{row?.completeTime}</span>;
          },
          hideInForm: true,
        },
        {
          dataIndex: 'statusName',
          valueType: 'select',
          fieldProps: {
            dropdownMatchSelectWidth: 120,
          },
          title: '状态',
          key: 'status',
          options: [
            {
              label: '已出库',
              value: 2,
            },
            {
              label: '已关单',
              value: 3,
            },
          ],
          hideInForm: true,
          hideInDescriptions: true,
        },
        {
          title: '工单状态',
          dataIndex: 'statusName',
          key: 'statusName',
          hideInSearch: true,
          hideInTable: true,
          hideInForm: true,
          span: 3,
        },

        {
          title: '备件信息',
          dataIndex: 'details',
          key: 'details',
          valueType: 'split',
        },
        {
          dataIndex: 'taskSpareList',
          key: 'taskSpareList',
          valueType: 'formSelectList',
          colProps: {
            span: 24,
          },
          columns,
          span: 12,
          path: '/sparepart/spareStock/queryByOperate',
          hideInSearch: true,
          hideInTable: true,
          rowName: 'sparePartName',
          render: (text, row) => {
            return (
              <AutoTable
                columns={columns
                  ?.filter((it) => it.key != 'usedStock')
                  ?.map((it) => ({
                    ...it,
                    hideInSearch: true,
                  }))}
                resizeable={false}
                dataSource={row?.taskSpareList}
                expandable={{
                  expandedRowRender: (record) => (
                    <ExtraTable dataSource={record?.taskSpareSupplierList ?? []} />
                  ),
                }}
              />
            );
          },
        },
        {
          dataIndex: 'splias',
          valueType: 'split',
          title: '出库/关单信息',
          key: 'splisa',
          hideInForm: true,
          render: (_, row) => {
            return row?.status == 2 ? '出库信息' : '关单信息';
          },
        },

        {
          title: '操作人',
          dataIndex: 'operateUserName',
          key: 'operateUserName',
          hideInSearch: true,
          hideInTable: true,
          span: 1,
          hideInForm: true,
        },
        {
          title: '操作时间',
          dataIndex: 'operateTime',
          key: 'operateTime',
          hideInSearch: true,
          hideInTable: true,
          hideInForm: true,
        },
      ],
      pathconfig: {
        enableadd: true,
        enableedit: false,
        enabledelete: false,
        enabledetail: true,
        add: '/sparepart/outWarehouseTask/save',
        edit: '',
        list: '/sparepart/outWarehouseTask/queryYesComplete',
        delete: '',
        detail: '/sparepart/outWarehouseTask/queryDetails',
      },
    },
  ];
}
export default getcolumns;