/*
  * 业务线管理
  * 高级搜索面板
  * @Author:    黎永顺
  * @Date:      2024/9/24
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaLocaleProvider, WeaTools } from "ecCom";
import { roleSearchConditions } from "../conditions";
import { getAuthOptTree } from "../../../../apis/taxAgent";
import { getSearchs } from "../../../../util";
import { Button } from "antd";

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

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

  async componentDidMount() {
    const { taxAgentStore: { advanceForm } } = this.props;
    const { data } = await getAuthOptTree();
    this.setState({
      conditions: _.map(roleSearchConditions, item => ({
        ...item, title: getLabel(item.lanId, item.title),
        items: _.map(item.items, o => {
          if (getKey(o) === "opts") {
            return {
              ...o, label: getLabel(o.lanId, o.label),
              options: _.map(o.options, k => ({ ...k, showname: getLabel(k.lanId, k.showname) }))
            };
          } else if (getKey(o) === "pages") {
            return {
              ...o, label: getLabel(o.lanId, o.label),
              treeData: [{
                label: data.name, value: data.key, key: data.key, id: data.key, name: data.name,
                children: _.map(data.modules, i => ({
                  label: i.name, value: `${data.key}-${i.key}`, key: `${data.key}-${i.key}`,
                  id: `${data.key}-${i.key}`, name: i.name,
                  children: _.map(i.pages, k => ({
                    label: k.name, value: k.key, key: k.key, selectable: true,
                    id: k.key, name: k.name
                  }))
                }))
              }]
            };
          }
          return { ...o, label: getLabel(o.lanId, o.label) };
        })
      }))
    }, () => advanceForm.initFormFields(this.state.conditions));
  }

  handleReset = () => {
    const { taxAgentStore: { advanceForm } } = this.props;
    this.setState({
      conditions: _.map(this.state.conditions, item => ({
        ...item, items: _.map(item.items, o => ({ ...o, value: "" }))
      }))
    }, () => advanceForm.resetForm());
  };
  handleFormChange = (val) => {
    const key = _.keys(val)[0];
    const { taxAgentStore: { advanceForm } } = this.props;
    if (key === "taxAgentIds" || key === "sobIds" || key === "pages") {
      this.setState({
        conditions: _.map(this.state.conditions, item => ({
          ...item, items: _.map(item.items, o => {
            if (key === getKey(o)) {
              return { ...o, value: _.map(val[key], o => o.id).join(",") };
            }
            return { ...o, value: advanceForm.getFormParams()[getKey(o)] };
          })
        }))
      });
    }
  };

  render() {
    const { taxAgentStore: { advanceForm } } = this.props;
    const { conditions } = this.state;
    return (
      <React.Fragment>
        <div className="wea-advanced-searchsAd">
          {getSearchs(advanceForm, conditions, 2, false, this.handleFormChange)}
        </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={this.handleReset}>{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;

