import React from 'react';

import { OuPicker, SupplierPicker } from '@bizComponents';
import { SuggestSelect, UdcSelect } from '@components';
import { DatePicker, Input, InputNumber, Select } from 'antd';
import moment from 'moment';

const TextArea = Input.TextArea;
const RangePicker = DatePicker.RangePicker;
const Option = Select.Option;

const config = (formData, mode, dispatch, loading, extraInfo) => {
  const domain = 'contractDetail';
  /*
  * @param key      字段名
  * @param index    行号
  * @param newValue 新值
  */
  const handleChange = (key, index, newValue) => {
    dispatch({ type: `${domain}/updateCell`, payload: { key, index, value: newValue } });
  };

  const handleChange2 = (key, index, newValue) => {
    dispatch({ type: `${domain}/updateInputCell`, payload: { key, index, value: newValue } });
  };

  function disabledDate(current: moment) {
    return current && current <= moment().endOf('day');
  }

  // 基本信息form
  const contractBasicForm = [
    {
      title: '合同编号',
      dataIndex: 'contractCode',
      tag: <Input disabled={true}/>,
    },{
      title: '供应商编号',
      dataIndex: 'suppCode',
      // tag: <Input disabled={true} />,
      options: {
        rules: [
          {
            required: true,
            message: '请选择供应商',
          },
        ],
      },
    }, {
      title: '采购方编号',
      dataIndex: 'ouCode',
      options: {
        rules: [
          {
            required: true,
            message: '请选择企业方',
          },
        ],
      },
    },
    {
      title: '合同名称',
      dataIndex: 'contractName',
      tag: <Input disabled={true}/>,
      // options: {
      //   rules: [
      //     {
      //       required: true,
      //       message: '请输入合同名称',
      //     }, {
      //       max: 100,
      //       message: '不能超过100个字',
      //     },
      //   ],
      // },
    }, {
      title: '供应商名称',
      dataIndex: 'suppName',
      tag: <SupplierPicker filter={{ suppStatusList: ['02', '03'] }}/>,
      options: {
        rules: [
          {
            required: true,
            message: '请选择企业方',
          },
        ],
      },
    }, {
      title: '采购方名称',
      dataIndex: 'ouName',
      tag: <OuPicker/>,
      options: {
        rules: [
          {
            required: true,
            message: '请选择企业方',
          },
        ],
      },
    },{
      title: '合同类型',
      dataIndex: 'contractType',
      tag: (extraInfo && extraInfo.AFlag)
        ? (<UdcSelect domain="CONTRACT" code="CONTRACT_TYPE"/>)
        : (<Select
        defaultActiveFirstOption={false}
        style={{ width: '100%' }}
        filterOption={false}
        allowClear
      >
        <Option key='2' title='B类'>B类</Option>
        <Option key='3' title='C类'>C类</Option>
      </Select>),
      options: {
        rules: [
          {
            required: true,
            message: '请选择合同类型',
          },
        ],
      },
    },{
      title: '主供应品类',
      dataIndex: 'itemClassId',
      tag: <SuggestSelect apiName="c3" params={{ mode }} />,
      options: {
        rules: [
          {
            required: true,
            message: '请选择主供应品类',
          },
        ],
      },
    },{
      title: '物料总数量',
      dataIndex: 'itemCount',
      tag: (
        <InputNumber
          min={0}
          max={999999999}
          precision={2}
          style={{ width: '100%' }}
          formatter={(value) => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
          parser={(value) => value.replace(/\$\s?|(,*)/g, '')}
        />
      ),
    },
    {
      title: '合同状态',
      dataIndex: 'checkStatus',
      tag: <UdcSelect domain="CONTRACT" code="CONTRACT_STATUS" disabled={true}/>,
    },
    {
      title: '合同关联品类',
      dataIndex: 'linkItemClass',
      tag: <SuggestSelect apiName="c3" mode="multiple" params={{ mode }} />,
    },
     {
      title: '合同总金额(元)',
      dataIndex: 'amount',
      options: {
        rules: [
          {
            required: true,
            message: '请输入合同总金额',
          },
        ],
      },
      tag: (
        <InputNumber
          min={0}
          max={999999999}
          precision={2}
          style={{ width: '100%' }}
          formatter={(value) => `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
          parser={(value) => value.replace(/\$\s?|(,*)/g, '')}
        />
      ),
    },{
      title: '签署日期',
      dataIndex: 'signDate',
      tag: <DatePicker style={{ width: '100%' }}/>,
      options: {
        rules: [
          {
            required: true,
            message: '请选择签署日期',
          },
        ],
      },
    },{
      title: '合同生效日期',
      dataIndex: 'timeInForceDate',
      tag: <DatePicker style={{ width: '100%' }}/>,
      options: {
        rules: [
          {
            required: true,
            message: '请选择合同生效日期',
          },
        ],
      },
    }, {
      title: '币种',
      dataIndex: 'currencyId',
      tag: <SuggestSelect apiName="currs" params={{ isDept: true }}/>,
    },   {
      title: '签署地点',
      dataIndex: 'signPlace',
      options: {
        rules: [
          {
            max: 500,
            message: '不能超过500个字',
          },
        ],
      },
      tag: <Input/>,
    },

    {
      title: '合同失效日期',
      dataIndex: 'deadDate',
      tag: <DatePicker style={{ width: '100%' }} disabledDate={disabledDate}/>,
      options: {
        rules: [
          {
            required: true,
            message: '请选择合同失效日期',
          },
        ],
      },
    }, {
      title: '月结付款期限',
      dataIndex: 'paymentTerms',
      options: {
        rules: [
          {
            max: 40,
            message: '不能超过40个字',
          },
        ],
      },
      tag: <Input/>,
    }
  ];

  const contractTmplForm = [
    {
      title: '合同模板:',
      dataIndex: 'contractModelId',
      options: {
        rules: [
          {
            required: true,
            message: '请选择模板',
          },
        ],
      },
      tag: <SuggestSelect apiName="contractTmpl" params={{ mode}}/>,
    },
  ];

  const contractVariateList = [
    {
      title: '变量说明',
      dataIndex: 'contractVarDesc',
      width: '20%',
    },
    {
      title: '变量值',
      dataIndex: 'itemContent',
      required: true,
      width: '70%',
      render: (value, row, index) => {
        return (
          <TextArea
            autosize={true}
            disabled={mode==='view' || !row.settled}
            // value={(!row.settled && !value) ? `${row.contractVarDesc}` : value}
            value={(!row.settled && !value) ? '' : value}
            onChange={(e) => {  handleChange2('itemContent', index, e.target.value); }}
          />
        );
      },
    },
  ];

  // 条款内容
  const contractAgreementList = [
    {
      title: '排列序号',
      dataIndex: 'lineNo',
      width: '20%',
      required: true,
      render: (value, row, index) => {
        return (
          <InputNumber
            style={{ width: '100%' }}
            value={value}
            min={1}
            onChange={(e) => { handleChange('lineNo', index, e); }}
          />
        );
      },
    },
    {
      title: '补充协议内容',
      dataIndex: 'itemContent',
      width: '70%',
      required: true,
      render: (value, row, index) => {
        return (
          <TextArea
            autosize={true}
            value={value}
            onChange={(e) => {  handleChange('itemContent', index, e.target.value); }}
          />
        );
      },
    },
  ];

  const contractAttchInfo = [{
    title: '合同附件',
    dataIndex: 'files',
  }];

  const contractStampAttchInfo = [{
    title: '盖章合同',
    dataIndex: 'stamp',
  }];
  return {
    contractBasicForm,
    contractTmplForm,
    contractVariateList,
    contractAgreementList,
    contractAttchInfo,
    contractStampAttchInfo
  };
};

export default config;
