import moment from 'moment';
import React, { Component } from 'react';
import { Form } from '@ant-design/compatible';
// import '@ant-design/compatible/assets/index.css';
import { Row, Col, message, DatePicker, Button } from 'antd-v4';
import StaticEditTable from '@/components/Common/CommonTable';/* 可编辑表格 */
import * as commonBusiness from '@/components/Common/commonBusiness';/* 单据业务功能 */
import styles from '@/index.less';
import commonConfig from '@/utils/config';
import * as commonServices from '@/services/services';
import * as commonUtils from '@/utils/utils';
import * as commonFunc from '@/components/Common/commonFunc';/* 通用方法 */

const FormItem = Form.Item;

class TabFinancePeriodComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      mode: 'year',
      open: false,
      value: moment(new Date().getFullYear().toString(), 'YYYY'), /* 年份 */
    };
  }
  handleOpenChange = (open) => {
    if (open) {
      this.setState({ mode: 'year', open: true });
    }
  };

  handlePanelChange = async (value, mode) => {
    const bFilterValue = moment(value).format('YYYY');
    this.setState({ mode, open: false, value });
    const {
      token, sModelsId, sysaccountperiodConfig, masterConfig,
    } = this.props;
    const condition = {
      sSqlCondition: {
        sParentId: masterConfig.sId,
      },
      bFilter: [{
        bFilterName: 'sPeriodId',
        bFilterValue,
        bFilterCondition: 'like',
      }],
    };
    const configDataId = sysaccountperiodConfig.sId;
    const dataUrl = `${commonConfig.server_host}business/getBusinessDataByFormcustomId/${configDataId}?sModelsId=${sModelsId}`;
    const dataReturn = (await commonServices.postValueService(token, condition, dataUrl)).data;
    if (dataReturn.code === 1) {
      const returnData = dataReturn.dataset.rows[0].dataSet;
      this.props.onSaveState({ sysaccountperiodData: returnData, year: bFilterValue });
    } else {
      this.props.getServiceError(dataReturn);
    }
  };
  handleAddData = async () => {
    const year = moment(this.state.value).format('YYYY');
    const bFilterValue = year - 1;
    const {
      token, sModelsId, sysaccountperiodConfig, masterConfig,
    } = this.props;
    const condition = {
      sSqlCondition: {
        sParentId: masterConfig.sId,
      },
      bFilter: [{
        bFilterName: 'sPeriodId',
        bFilterValue,
        bFilterCondition: 'like',
      }],
    };
    const configDataId = sysaccountperiodConfig.sId;
    const dataUrl = `${commonConfig.server_host}business/getBusinessDataByFormcustomId/${configDataId}?sModelsId=${sModelsId}`;
    const dataReturn = (await commonServices.postValueService(token, condition, dataUrl)).data;
    if (dataReturn.code === 1) {
      const returnData = dataReturn.dataset.rows[0].dataSet;
      if (commonUtils.isEmptyObject(returnData)) {
        message.error('请先添加上一年度数据');
      } else {
        returnData.forEach((row, index) => {
          returnData[index].bAP = false;
          returnData[index].bAR = false;
          returnData[index].bFrozen = false;
          returnData[index].sId = commonUtils.createSid();
          returnData[index].sPeriodId = (Number(returnData[index].sPeriodId) + 100).toString();
          returnData[index].tEndDate = moment(returnData[index].tEndDate, 'YYYY-MM-DD HH:mm:ss').add(1, 'years').format('YYYY-MM-DD HH:mm:ss');
          returnData[index].tStartDate = moment(returnData[index].tStartDate, 'YYYY-MM-DD HH:mm:ss').add(1, 'years').format('YYYY-MM-DD HH:mm:ss');
          // returnData[index].tForzenDate = '';
          // returnData[index].tARDate = '';
          // returnData[index].tAPDate = '';
          returnData[index].handleType = 'add';
        });
      }
      this.props.onSaveState({ sysaccountperiodData: returnData });
    } else {
      this.props.getServiceError(dataReturn);
    }
  };
  render() {
    const { open, value, mode } = this.state;
    const { enabled, app } = this.props;
    const formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 15 } };
    const Year = commonFunc.showMessage(app.commonConst, 'Year');/* 年度 */
    const BtnDefaultValueIncrease = commonFunc.showMessage(app.commonConst, 'BtnDefaultValueIncrease');/* 默认值增加 */
    return (
      <FormItem className={styles.searchSlaveForm}>
        <div className={styles.tableOptionSys}>
          <Row>
            <Col span={6}>
              <FormItem
                {...formItemLayout}
                label={Year}
              >
                <DatePicker
                  format="YYYY"
                  open={open}
                  value={value}
                  mode={mode}
                  onOpenChange={this.handleOpenChange}
                  onPanelChange={this.handlePanelChange}
                />
              </FormItem>
            </Col>
            <Col span={6}>
              <Button disabled={!enabled} onClick={this.handleAddData} >{BtnDefaultValueIncrease}</Button>
            </Col>
          </Row>
          <div className="financePeriod">
            <StaticEditTable {...commonBusiness.getTableTypes('sysaccountperiod', this.props)} />
          </div>
        </div>
      </FormItem>
    );
  }
}

export default TabFinancePeriodComponent;
