import React, { Component } from "react";
import { WeaLocaleProvider, WeaTools } from "ecCom";
import { Button } from "antd";
import { inject, observer } from "mobx-react";
import { getSaCondition } from "../../../../../apis/archive";
import { getSearchs } from "../../../../../util";

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

@inject("HTStore")
@observer
class AdvanceSearchPannel extends Component {
  constructor(props) {
    super(props);
    this.state = {
      searchConditions: []
    };
  }

  componentDidMount() {
    getSaCondition().then(({ status, data }) => {
      if (status) {
        this.setState({
          searchConditions: _.map(data.condition, item => ({
            ...item,
            items: [
              {
                browserConditionParam: {
                  completeParams: {},
                  conditionDataParams: {},
                  dataParams: {},
                  destDataParams: {},
                  hasAddBtn: false,
                  hasAdvanceSerach: false,
                  idSeparator: ",",
                  isAutoComplete: 1,
                  isDetail: 0,
                  isMultCheckbox: false,
                  isSingle: false,
                  icon: "icon-coms-hrm",
                  linkUrl: "",
                  pageSize: 10,
                  quickSearchName: "",
                  replaceDatas: [],
                  title: "",
                  type: "17",
                  viewAttr: 2
                },
                conditionType: "BROWSER",
                domkey: ["employeeIds"],
                fieldcol: 16,
                label: getLabel(111, "人员"),
                labelcol: 8,
                viewAttr: 2
              },
              ..._.map(_.filter(item.items, k => ["subcompanyIdsStr", "siSchemeId", "fundSchemeId", "otherSchemeId", "taxAgentId"].includes(getKey(k))), o => {
                if (getKey(o) === "subcompanyIdsStr") {
                  return {
                    ...o, domkey: ["subCompanyIds"], browserConditionParam: {
                      completeParams: {},
                      conditionDataParams: {},
                      dataParams: {},
                      destDataParams: {},
                      hasAddBtn: false,
                      hasAdvanceSerach: false,
                      idSeparator: ",",
                      isAutoComplete: 1,
                      isDetail: 0,
                      isMultCheckbox: false,
                      isSingle: false,
                      icon: "icon-coms-hrm",
                      linkUrl: "",
                      pageSize: 10,
                      quickSearchName: "",
                      replaceDatas: [],
                      title: "",
                      type: "194",
                      viewAttr: 2
                    }
                  };
                } else if (["siSchemeId", "fundSchemeId", "otherSchemeId"].includes(getKey(o))) {
                  return { ...o, domkey: [`${getKey(o)}s`], multiple: true };
                }
                return { ...o, domkey: ["taxAgents"], multiple: true };
              })
            ]
          }))
        }, () => {
          const { HTStore: { queryForm } } = this.props;
          queryForm.initFormFields(this.state.searchConditions);
        });
      }
    });
  }

  render() {
    const { searchConditions } = this.state;
    const { HTStore: { queryForm } } = this.props;
    return (
      <React.Fragment>
        <div className="wea-advanced-searchsAd">
          {getSearchs(queryForm, searchConditions, 2, false)}
        </div>
        <div className="wea-search-buttons">
          <div style={{ textAlign: "center" }}>
            <span style={{ marginLeft: 15 }}>
              <Button type="primary" onClick={this.props.onAdSearch}>{getLabel(388113, "搜索")}</Button>
            </span>
            <span style={{ marginLeft: 15 }}>
              <Button type="ghost" onClick={() => queryForm.resetForm()}>{getLabel(2022, "重置")}</Button>
            </span>
            <span style={{ marginLeft: 15 }}>
              <Button type="ghost" onClick={this.props.onCancel}>{getLabel(31129, "取消")}</Button>
            </span>
          </div>
        </div>
      </React.Fragment>
    );
  }
}

export default AdvanceSearchPannel;

