import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { Time } from '/@/components/Time';
import { partAddOrUpdate } from '/@/api/spares'
import { saveRoleApi } from '/@/api/sys/system';
import { usePermissionStore } from '/@/store/modules/permission';
  // import { ListAllCompanyApi } from '/@/api/corporation/list';
import { useMessage } from '/@/hooks/web/useMessage';
import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  const { createMessage } = useMessage();
  const permissionStore = usePermissionStore();
  const { getCheckPerm } = permissionStore;

export const columns: BasicColumn[] = [
  {
    title: '设备类型',
    dataIndex: 'cameraType',
    width: 100,
    customRender:({ record }) => {
      return t(`routes.scene.tableType.${record.cameraType}`)
    }
  },
  {
    title: '备件编号',
    dataIndex: 'partNum',
    width: 100,
  },
  {
    title: '名称',
    dataIndex: 'partName',
    width: 180,
  },
  {
    title: '单位',
    dataIndex: 'partUnit',
    width: 100,
  },
  {
    title: '单价（元）',
    dataIndex: 'partPrice',
    width: 100,
  },
  {
    title: '折扣价（元）',
    dataIndex: 'partPriceDiscount',
    width: 100,
    customRender:({ record }) => {
      return record.partPriceDiscount || '-'
    }
  },
  {
    title: '库存（件）',
    dataIndex: 'partStock',
    width: 180,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    // ifShow:getCheckPerm('spares-status'),
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 0,
        checkedChildren: '启用',
        unCheckedChildren: '禁用',
        disabled:!getCheckPerm('spares-status'),
        loading: false,
        onChange: async (checked: boolean) => {
          record.pendingStatus = true;
          const newStatus = checked ? 0 : 1;
          Reflect.set(record, 'status', newStatus);
          await partAddOrUpdate({ ...record, isShow: newStatus });
          createMessage.success(t('common.optSuccess'));
          // reload()
        },
      });
    },
  },
  // {
  //   title: t('routes.system.remarks'),
  //   dataIndex: 'remark',
  // },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'cameraType',
    label: '设备类型',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: t('routes.scene.tableType.0'),
          value: '0',
        },{
          label: t('routes.scene.tableType.1'),
          value: '1',
        },{
          label: t('routes.scene.tableType.2'),
          value: '2',
        },
      ],
    },
    colProps: {
      xl: 6,
      xxl: 6,
    },
  },{
    field: 'partName',
    label: '备件名称',
    component: 'Input',
    componentProps: {
      maxLength: 100,
    },
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show:false,
  },
  {
    field: 'roleName',
    label: t('common.roleNameText'),
    required: true,
    component: 'Input',
    componentProps:{
      maxLength: 15,
    }
  },
  // {
  //   field: 'companyId',
  //   label: t('routes.staff.companyId'),
  //   component: 'ApiSelect',
  //   // required: true,
  //   itemProps: {
  //     validateTrigger: 'blur',
  //   },
  //   componentProps: {
  //     api: getAllRoleList,
  //     resultField: 'data',
  //     labelField: 'roleName',
  //     valueField: 'id',
  //     immediate: true,
  //     onChange: function () {
  //       // Reflect.set(modalRecord, 'shippingName', opt.label);
  //     },
  //     params: {
  //       page: 1,
  //       limit: 1000,
  //     },
  //     required: true,
  //   },
  // },
  // {
  //   field: 'canShow',
  //   label: '状态',
  //   component: 'RadioButtonGroup',
  //   defaultValue: 0,
  //   componentProps: {
  //     options: [
  //       { label: '启用', value: 0 },
  //       { label: '停用', value: 1 },
  //     ],
  //   },
  // },
  // {
  //   field: 'status',
  //   label: t('routes.system.isPlatformRole'),
  //   component: 'RadioButtonGroup',
  //   defaultValue: 1,
  //   componentProps: {
  //     options: [
  //       { label: '启用', value: 1 },
  //       { label: '禁用', value: 0 },
  //     ],
  //   },
  // },
  {
    label: t('routes.system.description'),
    field: 'description',
    component: 'InputTextArea',
    required: true,
    componentProps:{
      rows:4,
      maxLength: 200,
    }
  },
  // {
  //   label: ' ',
  //   field: 'menuIdList',
  //   slot: 'menu',
  //   component: 'Input',
  // },
  // {
  //   label: ' ',
  //   field: 'deptIdList',
  //   slot: 'dept',
  //   component: 'Input',
  // },
];
