/*
 * Author: 黎永顺
 * name: 社保福利台账重构-查询组件
 * Description:
 * Date: 2024/1/23
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaFormItem, WeaLocaleProvider, WeaSearchGroup, WeaTools } from "ecCom";
import { WeaSwitch } from "comsMobx";
import * as API from "../../../../apis/standingBook";
import { welfareRQConditions } from "../columns";
import { MonthRangePicker } from "../../../reportView/components/statisticalMicroSettingsSlide";
import moment from "moment";

const getLabel = WeaLocaleProvider.getLabel;
const getKey = WeaTools.getKey;

@inject("standingBookStore") @observer
class WelfareRecordQuery extends Component {
  constructor(props) {
    super(props);
    this.state = {
      conditions: [],
      dateRange: [moment(new Date()).subtract(1, "year").startOf("year").format("YYYY-MM"), moment(new Date()).endOf("year").format("YYYY-MM")]
    };
  }

  componentDidMount() {
    API.getAdminTaxAgentList().then(({ status, data }) => {
      if (status) {
        this.setState({
          conditions: _.map(welfareRQConditions, item => {
            return {
              ...item, items: _.map(item.items, o => {
                if (getKey(o) === "taxAgents") {
                  return { ...o, options: _.map(data, g => ({ key: g.id.toString(), showname: g.name })) };
                }
                return o;
              })
            };
          })
        }, () => {
          const { standingBookStore: { welfareRQForm }, onPutAccountOptions } = this.props;
          welfareRQForm.initFormFields(this.state.conditions);
          onPutAccountOptions(_.map(data, g => ({ key: g.id.toString(), showname: g.name })));
        });
      }
    });
  }

  renderForm = (form, conditions) => {
    const { dateRange } = this.state;
    const { isFormInit } = form;
    const formParams = form.getFormParams();
    let group = [];
    isFormInit && conditions && conditions.map(c => {
      let items = [];
      c.items.map(fields => {
        items.push({
          com: (<WeaFormItem label={`${fields.label}`} labelCol={{ span: `${fields.labelcol}` }}
                             wrapperCol={{ span: `${fields.fieldcol}` }} error={form.getError(fields)}
                             tipPosition="bottom">
            <WeaSwitch fieldConfig={fields} form={form} formParams={formParams} onChange={this.getWelfareRecordList}/>
          </WeaFormItem>)
        });
      });
      group.push(<WeaSearchGroup col={3} needTigger showGroup={c.defaultshow}
                                 items={[{
                                   com: (<WeaFormItem label={getLabel(542443, "账单月份")} labelCol={{ span: 4 }}
                                                      wrapperCol={{ span: 16 }}>
                                     <MonthRangePicker dateRange={dateRange} viewAttr={2}
                                                       onChange={v => this.setState({ dateRange: v }, () => this.getWelfareRecordList())}/>
                                   </WeaFormItem>)
                                 }, ...items]}
      />);
    });
    return group;
  };
  getWelfareRecordList = () => {
    const { standingBookStore: { welfareRQForm } } = this.props;
    const { dateRange } = this.state;
    const [startTime, endTime] = dateRange;
    const payload = {
      ...welfareRQForm.getFormParams(), startTime, endTime
    };
    this.props.onSearch(payload);
  };

  render() {
    const { standingBookStore: { welfareRQForm } } = this.props;
    const { conditions } = this.state;
    return (<div className="salary-welfare-record-query">{this.renderForm(welfareRQForm, conditions)}</div>);
  }
}

export default WelfareRecordQuery;
