import { SupplierPicker, ItemPicker, CascaderItem } from '@bizComponents';
import { SuggestSelect, UdcSelect } from '@components';
import { DatePicker, Input, InputNumber, Checkbox } from 'antd';
import moment from 'moment';
import React from 'react';
import { ctrlUtils } from '@utils';

const { TextArea } = Input;
const { throttle } = ctrlUtils;

const cons = () => {
};

let _vdebug = cons;
const vdebug = throttle(800, false, () => _vdebug(), false); // debounce
// const vdebug = throttle(1000, false, () => _vdebug(), true)
const transCHToEN = (value,dispatch, targetKey, sourceKey) => {
  _vdebug = () => dispatch({ type: 'itemTrial/transCHToEn', payload: { value, targetKey, sourceKey } });
  vdebug();
};

const config = (formData, mode, dispatch, loading) => {
  let businessScore = 0;
  let qualityScore = 0;
  let semiProduct = false;
  let repItemRate;
  if (formData instanceof Array) {

    formData.forEach((element) => {
      if (element.itemScore) {
        businessScore += parseInt(element.itemScore);
      }
    });
    formData.forEach((element) => {
      if (element.itemScore) {
        qualityScore += parseInt(element.itemScore);
      }
    });
  }
  if (formData instanceof Object) {
    semiProduct = formData.semiProduct === '1' ? true : false;
    repItemRate = formData.repItemName && formData.repItemName.id
  }

  // 供应商选择改变事件
  const onChange = (value, index) => {
    dispatch({ type: 'itemTrial/updateSupp', payload: { value, index } });
  };

  // 列表编辑事件
  const handleChange = (type, key, index, newValue) => {
    dispatch({ type: `itemTrial/updateCell`, payload: { type, key, index, value: newValue } });
  };

  function disabledDate(current) {
    return current && current > moment().endOf('day');
  }

  // 物料信息
  const itemForm = [{
    title: '审批编号',
    dataIndex: 'suppItemCode',
  }, {
    title: '物料名称',
    dataIndex: 'itemName',
    tag: <Input maxLength={100} onChange={(e) => transCHToEN(e.target.value,dispatch, 'memCode', 'itemName')} />,
  }, {
    title: '半成品',
    dataIndex: 'semiProduct',
    tag: <Checkbox checked={semiProduct} />,
  }, {
    title: '物料编号',
    dataIndex: 'outerCode',
  }, {
    title: '物料缩写',
    dataIndex: 'memCode',
  }, {
    title: '规格型号',
    dataIndex: 'spec',
    tag: <Input maxLength={40} />,
  }, {
    title: '临时编号',
    dataIndex: 'itemCode',
  }, {
    title: '品牌',
    dataIndex: 'brand',
  }, {
    title: '物料状态',
    dataIndex: 'itemStatusName',
  }, {
    title: '试制部门',
    dataIndex: 'useOrg',
    tag: <UdcSelect domain="SRM" code="TEST_ORG_TYPE" />,
  }, {
    title: '供货区域',
    dataIndex: 'suppCenter',
    tag: <SuggestSelect apiName="suppArea" />,
  }];

  // 选择试制供应商
  const chooseSupp = [{
    title: '供应商名称',
    dataIndex: 'suppName',
    width: '25%',
    render: (value, row, index) => {
      return (
        <SupplierPicker
          filter={{ region: row.region, suppStatusList: row.suppStatusList }}
          value={{ id: row.id, code: row.suppCode, name: row.suppName }}
          size="small"
          onChange={(value) => onChange(value, index)}
        />);
    },
  }, {
    title: '供应商编号',
    dataIndex: 'suppCode',
    width: '25%',
  }, {
    title: '供应商状态',
    dataIndex: 'suppStatusName',
    width: '25%',
  }, {
    title: '最新绩效',
    dataIndex: 'scoreGradeName',
    width: '25%',
  }];

  // 样测结果反馈
  const tiralFeedback = [{
    title: '测试部门',
    dataIndex: 'useOrg',
    tag: <UdcSelect domain="SRM" code="TEST_ORG_TYPE" />,
  }, {
    title: '测试日期',
    dataIndex: 'stDate',
    tag: <DatePicker format="YYYY-MM-DD" style={{ width: '100%' }} disabledDate={disabledDate} />,
  }, {
    title: '测试数量',
    dataIndex: 'stCount',
    tag: <InputNumber min={0} formatter={(value) => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
      parser={(value) => value.replace(/(,*)/g, '')} maxLength={19} style={{ width: '100%' }} />,
  }, {
    title: '评估日期',
    dataIndex: 'stEvaluateDate',
    tag: <DatePicker format="YYYY-MM-DD" style={{ width: '100%' }} disabledDate={disabledDate} />,
  }, {
    title: '测试结果',
    dataIndex: 'stResult',
    tag: <Input maxLength={500} />,
  }, {
    title: '批测部门',
    dataIndex: 'btOrg',
    tag: <SuggestSelect apiName="trialOrg" />,
    hidden: !semiProduct,
  }, {
    title: '综合意见',
    dataIndex: 'stOpinion',
    colProps: {
      xs: 24,
      sm: 24,
      md: 24,
      xl: 24,
      lg: 24,
      style: {},
    },
    formItemLayout: {
      labelCol: {
        span: 3,
      },
      wrapperCol: {
        span: 20,
      },
    },
    tag: <TextArea autosize={{ minRows: 2, maxRows: 2 }} maxLength={500} />,
  }];

  // 现场审核反馈
  const scene = [{
    title: '批测部门',
    dataIndex: 'btOrg',
    tag: <SuggestSelect apiName="trialOrg" />,
  }, {
    title: '批测期间起',
    dataIndex: 'btFromDate',
    tag: <DatePicker format="YYYY-MM-DD" style={{ width: '100%' }} defaultValue={moment()} />,
  }, {
    title: '批测期间止',
    dataIndex: 'btToDate',
    tag: <DatePicker format="YYYY-MM-DD" style={{ width: '100%' }} defaultValue={moment()} />,
  }, {
    title: '采购部意见',
    dataIndex: 'cgOpinion',
    colProps: {
      xs: 24,
      sm: 24,
      md: 24,
      xl: 24,
      lg: 24,
      style: {},
    },
    formItemLayout: {
      labelCol: {
        span: 3,
      },
      wrapperCol: {
        span: 20,
      },
    },
    tag: <TextArea autosize={{ minRows: 2, maxRows: 2 }} maxLength={500} />,
  }, {
    title: '技术质量部意见',
    dataIndex: 'jstlOpinion',
    colProps: {
      xs: 24,
      sm: 24,
      md: 24,
      xl: 24,
      lg: 24,
      style: {},
    },
    formItemLayout: {
      labelCol: {
        span: 3,
      },
      wrapperCol: {
        span: 20,
      },
    },
    tag: <TextArea autosize={{ minRows: 2, maxRows: 2 }} maxLength={500} />,
  }, {
    title: '中央厨房意见',
    dataIndex: 'zycfOpinion',
    colProps: {
      xs: 24,
      sm: 24,
      md: 24,
      xl: 24,
      lg: 24,
      style: {},
    },
    formItemLayout: {
      labelCol: {
        span: 3,
      },
      wrapperCol: {
        span: 20,
      },
    },
    tag: <TextArea autosize={{ minRows: 2, maxRows: 2 }} maxLength={500} />,
  }];

  // 批测结果反馈
  const batch = [{
    title: '批测数量',
    dataIndex: 'btCount',
    tag: <InputNumber min={0} formatter={(value) => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
      parser={(value) => value.replace(/(,*)/g, '')} maxLength={19} style={{ width: '100%' }} />,
  }, {
    title: '批测结果',
    dataIndex: 'btResult',
    tag: <Input maxLength={500} />,
  }, {
    title: '批测意见',
    dataIndex: 'btOpinion',
    colProps: {
      xs: 24,
      sm: 24,
      md: 24,
      xl: 24,
      lg: 24,
      style: {},
    },
    formItemLayout: {
      labelCol: {
        span: 3,
      },
      wrapperCol: {
        span: 20,
      },
    },
    tag: <TextArea autosize={{ minRows: 2, maxRows: 2 }} maxLength={500} />,
  }];

  const business = [{
    title: '商务评估(共40分)',
    dataIndex: 'testItem',
    width: '30%',
  }, {
    title: `得分数(${businessScore}分)`,
    dataIndex: 'itemScore',
    width: '20%',
    render: (value, row, index) => {
      return (
        <InputNumber
          value={value}
          min={0}
          max={parseInt(row.testItem.substring(row.testItem.indexOf('(') + 1, row.testItem.indexOf(')') - 1))}
          style={{ textAlign: 'right' }}
          onChange={(value) => {
            handleChange('business', 'itemScore', index, value);
          }}
        />);
    },
  }, {
    title: '备注',
    dataIndex: 'remark',
    width: '45%',
    render: (value, row, index) => {
      return (
        <TextArea rows={1} value={value} maxLength={300} style={{ textAlign: 'left' }} onChange={(e) => {
          handleChange('business', 'remark', index, e.target.value);
        }}
        />);
    },
  }];

  const qualit = [{
    title: '品质评估(共60分)',
    dataIndex: 'testItem',
    width: '30%',
  }, {
    title: `得分数(${qualityScore}分)`,
    dataIndex: 'itemScore',
    width: '20%',
    render: (value, row, index) => {
      return (
        <InputNumber
          value={value}
          min={0}
          max={parseInt(row.testItem.substring(row.testItem.indexOf('(') + 1, row.testItem.indexOf(')') - 1))}
          style={{ textAlign: 'right' }}
          onChange={(value) => {
            handleChange('quality', 'itemScore', index, value);
          }}
        />);
    },
  }, {
    title: '备注',
    dataIndex: 'remark',
    width: '45%',
    render: (value, row, index) => {
      return (
        <TextArea rows={1} value={value} maxLength={300} style={{ textAlign: 'left' }} onChange={(e) => {
          handleChange('quality', 'remark', index, e.target.value);
        }}
        />);
    },
  }];

  // 物料信息确认
  const itemConfirm = [{
    title: '盘点类型',
    dataIndex: 'itemStakeType',
    tag: <UdcSelect domain="SRM" code="ITEM_STAKE_TYPE" />,
  }, {
    title: '物料类别',
    dataIndex: 'cccCode',
    tag: <CascaderItem placeholder="请选择大类/中类/小类" />,
  }, {
    title: '物料类别',
    dataIndex: 'cccCodeName',
  }, {
    title: '仓库类型',
    dataIndex: 'whType',
    tag: <UdcSelect domain="SRM" code="WH_TYPE" />,
  }, {
    title: '对应标准原料',
    dataIndex: 'repItemName',
    tag: <ItemPicker filter={{ statusList: [2], itemProp: 1 }} apiName="items" />,
  }, {
    title: '转换率',
    dataIndex: 'repItemRate',
    tag: <Input disabled={!repItemRate} maxLength={18} />,
  }, {
    title: '半成品库位',
    dataIndex: 'semiStore',
    tag: <UdcSelect domain="JDE" code="SEMI_STORE" />,
  }, {
    title: '总账类',
    dataIndex: 'glpt',
    tag: <UdcSelect domain="JDE" code="GLPT" />,
  }, {
    title: '产地',
    dataIndex: 'origin',
    tag: <Input maxLength={40} />,
  }, {
    title: '含税单价',
    dataIndex: 'itemPrice',
    tag: <InputNumber formatter={value => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
      parser={value => value.replace(/\$\s?|(,*)/g, '')} style={{ width: '100%' }} />,
  }, {
    title: '主计量单位',
    dataIndex: 'uom',
    tag: <SuggestSelect apiName="udcItem" params={{ domainCode: "SRM", udcCode: "UOM" }} />,
  }, {
    title: '消耗单位',
    dataIndex: 'uom10',
    tag: <SuggestSelect apiName="udcItem" params={{ domainCode: "SRM", udcCode: "UOM" }} />,
  }, {
    title: '报货单位',
    dataIndex: 'uom9',
    tag: <SuggestSelect apiName="udcItem" params={{ domainCode: "SRM", udcCode: "UOM" }} />,
  }, {
    title: '存储方式',
    dataIndex: 'storageMode',
    tag: <UdcSelect domain="ITEM" code="STOCK_CODE" />,
  }, {
    title: '税种',
    dataIndex: 'taxCode',
    tag: <UdcSelect domain="SRM" code="TAX_CODE" />,
  }, {
    title: '订货周期',
    dataIndex: 'orderPeriod',
    tag: <Input maxLength={40} />,
  }, {
    title: '最小订货数量',
    dataIndex: 'moq',
    tag: <InputNumber formatter={value => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
      parser={value => value.replace(/\$\s?|(,*)/g, '')} style={{ width: '100%' }} />,
  }, {
    title: '保质期',
    dataIndex: 'storeValueDays',
    tag: <InputNumber formatter={value => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
      parser={value => value.replace(/\$\s?|(,*)/g, '')} style={{ width: '100%' }} />,
  }, {
    title: '存储条件',
    dataIndex: 'storageCondition',
    tag: <Input maxLength={40} />,
  }, {
    title: '税率(%)',
    dataIndex: 'taxRate',
    tag: <SuggestSelect apiName="tax" />,
  }, {
    title: '采购应计税码',
    dataIndex: 'taxCodeInp',
    tag: <UdcSelect domain="JDE" code="TAX_FLAG" />,
  }, {
    title: '销项税率(%)',
    dataIndex: 'taxRateOutp',
    tag: <SuggestSelect apiName="tax" />,
  }, {
    title: '应纳销售税码',
    dataIndex: 'taxCodeOutp',
    tag: <UdcSelect domain="JDE" code="TAX_FLAG" />,
  }, {
    title: '采购价格仓库',
    dataIndex: 'whProp',
    tag: <UdcSelect domain="SRM" code="WH_PROP" />,
  }, {
    title: '配送类型',
    dataIndex: 'distProp',
    tag: <UdcSelect domain="SRM" code="DIST_TYPE" />,
  }, {
    title: '标准人工',
    dataIndex: 'stdLabCost',
    tag: <InputNumber formatter={value => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
      parser={value => value.replace(/\$\s?|(,*)/g, '')} style={{ width: '100%' }} />,
  }, {
    title: '标准制费',
    dataIndex: 'stdProdCost',
    tag: <InputNumber formatter={value => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
      parser={value => value.replace(/\$\s?|(,*)/g, '')} style={{ width: '100%' }} />,
  }, {
    title: '内部品牌',
    dataIndex: 'innerBrand',
    tag: <UdcSelect domain="ITEM" code="INNER_BRAND" mode="multiple" />,
  }, {
    title: '用途',
    dataIndex: 'usage',
    tag: <TextArea autosize={{ minRows: 2, maxRows: 6 }} />,
  }];

  const uploadInfo = [{
    title: '附件上传',
    dataIndex: 'upload',
  }];


  return {
    itemForm,
    chooseSupp,
    tiralFeedback,
    scene,
    batch,
    business,
    qualit,
    itemConfirm,
    uploadInfo
  };
};

export default config;
