import { device } from './../../../api/deviceManage';
import { dateUtil } from '/nerv-lib/util/date-util';
import { device } from '/@/api/deviceManage';
import { ref } from 'vue';
import { http } from '/nerv-lib/util';

export const tableConfig = (orgId) => {
  return ref({
    title: '设备信息',
    api: device.queryDevicePage,
    params: { orgId },
    treeConfig: {
      header: {
        icon: 'deviceType',
        title: '设备类别',
      },
      params: { orgId },
      dynamicParams: { deviceCode: 'code' },
      defaultExpandAll: true,
      api: device.queryDeviceTree,
      fieldNames: { title: 'deviceType', key: 'code' },
      formConfig: {
        schemas: [
          {
            field: 'deviceType',
            label: '设备名称',
            component: 'NsInput',
            autoSubmit: true,
            componentProps: {
              placeholder: '请输入设备类型',
            },
          },
        ],
      },
    },
    rowSelection: null,
    // scroll: { x: 2000 },
    columns: [
      {
        title: '设备名称',
        dataIndex: 'deviceName',
      },
      {
        title: '设备型号',
        dataIndex: 'deviceModel',
      },
      {
        title: 'SN码',
        dataIndex: 'snCode',
        textNumber: 5,
        textEllipsis: true,
      },
      {
        title: '设备一级区域',
        dataIndex: 'device1Area',
        textWidth: 88,
        // width: 130,
        textEllipsis: true,
      },
      {
        title: '设备二级区域',
        dataIndex: 'device2Area',
        textWidth: 88,
        // width: 130,
        textEllipsis: true,
      },
      {
        title: '设备详细位置',
        dataIndex: 'deviceAddress',
        textNumber: 5,
        textEllipsis: true,
      },
      {
        title: '设备规格',
        dataIndex: 'deviceTp',
        textNumber: 4,
      },
      {
        title: '设备厂商纳税人识别号',
        dataIndex: 'deviceRatepay',
        textNumber: 9,
        textEllipsis: true,
      },
      {
        title: '设备厂商',
        textNumber: 4,
        dataIndex: 'manufacturer',
      },
      {
        title: '厂商联系人',
        dataIndex: 'contactPerson',
      },
      {
        title: '设备描述',
        dataIndex: 'deviceDesc',
        textWidth: 88,

        textEllipsis: true,
      },
      {
        title: 'IP地址',
        dataIndex: 'ipAddress',
      },
      {
        title: '生产日期',
        dataIndex: 'manufactureDate',
      },
      {
        title: '采购日期',
        dataIndex: 'purchaseDate',
      },
      {
        title: '启用日期',
        dataIndex: 'startDate',
      },
      {
        title: '设备成本（元）',
        dataIndex: 'equipmentCost',
        textNumber: 6,
      },
      {
        title: '使用期限',
        dataIndex: 'usagePeriod',
        textNumber: 4,
        customRender: ({ value }) => `${value}年`,
      },
      {
        title: '额定功率',
        dataIndex: 'ratedPower',
        textNumber: 4,
        // customRender: ({ value }) => `${value}年`,
      },
      {
        title: '特殊参数',
        textNumber: 4,
        dataIndex: 'specialParameters',
      },
    ],

    formConfig: {
      schemas: [
        {
          field: 'deviceName',
          label: '设备名称',
          component: 'NsSelectApi',
          componentProps: {
            placeholder: '请选择设备名称',
            api: (params) => {
              return http.post(device.dropArea, params).then((res) => {
                const result = res.data?.reduce((pre, cur) => {
                  !pre.includes(cur.deviceName) && pre.push(cur.deviceName);
                  return pre;
                }, []);
                return { data: result };
              });
            },
            resultField: 'data',
            params: { orgId, filterField: 'DEVICE_NAME_FACTORY' },
            // labelField: 'deviceName',
            // valueField: 'deviceName',
            filterOption: (input: string, option: any) => {
              return option.deviceName?.toLowerCase().indexOf(input.toLowerCase()) >= 0;
            },
            showSearch: true,
            immediate: true,
            dropdownReload: true,
            allowClear: true,
          },
        },
        {
          field: 'areas',
          label: '设备区域',
          component: 'NsCascader',
          format: (record) => {
            console.log(record);

            return record?.reduce(
              (pre, cur) => {
                const len = cur?.length - 1;
                pre[len].push(cur[len]);
                return pre;
              },
              [[], []],
            );
          },
          fieldMap: ['area1', 'area2'],
          componentProps: {
            placeholder: '请选择设备区域',
            multiple: true,
            loadData: (selectedOptions, options) => {
              const targetOption = selectedOptions[selectedOptions.length - 1];
              if (!selectedOptions.length) {
                http.post(device.dropArea, { orgId, filterField: 'DEVICE_AREA' }).then((res) => {
                  options.value = res.data?.map((item) => {
                    return { label: item, value: item, children: [], isLeaf: false };
                  });
                });
              }
              const value = targetOption?.value;
              if (targetOption) {
                targetOption.loading = true;
                http
                  .post(device.dropArea, { device1Area: value, orgId, filterField: 'DEVICE_AREA' })
                  .then((res) => {
                    targetOption.loading = false;
                    targetOption.children = res.data?.map((item) => {
                      return { label: item, value: item, children: [], isLeaf: true };
                    });
                  });
              }
            },
          },
        },
        {
          field: 'manufacturer',
          label: '设备厂商',
          component: 'NsSelectApi',
          componentProps: {
            placeholder: '请选择设备厂商',
            api: (params) => {
              return http.post(device.dropArea, params).then((res) => {
                const result = res.data?.reduce((pre, cur) => {
                  !pre.includes(cur.manufacturer) && pre.push(cur.manufacturer);
                  return pre;
                }, []);
                return { data: result };
              });
            },
            resultField: 'data',
            params: { orgId, filterField: 'DEVICE_NAME_FACTORY' },
            // labelField: 'manufacturer',
            // valueField: 'manufacturer',
            filterOption: (input: string, option: any) => {
              return option.manufacturer?.toLowerCase().indexOf(input.toLowerCase()) >= 0;
            },
            showSearch: true,
            immediate: true,
            dropdownReload: true,
            allowClear: true,
          },
        },
        {
          field: 'createTime',
          label: '生产日期',
          component: 'NsRangePicker',
          fieldMap: ['manufactureBeginDate', 'manufactureEndDate'],
          componentProps: {
            valueFormat: 'YYYY-MM-DD',
            placeholder: ['生产开始日期', '生产结束日期'],
          },
        },
        {
          field: 'createTime1',
          label: '采购日期',
          component: 'NsRangePicker',
          fieldMap: ['purchaseBeginDate', 'purchaseEndDate'],
          componentProps: {
            valueFormat: 'YYYY-MM-DD',
            placeholder: ['采购开始日期', '采购结束日期'],
          },
        },
        {
          field: 'createTime2',
          label: '启用日期',
          component: 'NsRangePicker',
          fieldMap: ['startBeginDate', 'startEndDate'],
          componentProps: {
            valueFormat: 'YYYY-MM-DD',
            placeholder: ['启用开始日期', '启用结束日期'],
          },
        },
      ],
      params: {},
    },
    // pagination: { pageSizeOptions: false },
    rowKey: 'uuid',
  });
};
