/*
* 商户入驻——经营信息
* */
import { connect } from 'dva/index';
import React, { Component, Fragment } from 'react';
import { Form, Button, message, Spin} from 'antd';
import {
  sldComLanguage,
  sldLlineRtextAddGoodsAddMargin,
  saveSettleData,
  getSettleData,
  getSldEmptyH,
  sucTip,
  failTip,
  list_com_page_more,
} from '@/utils/utils';
import styles from './index.less';

import SldTableRowThree from '@/components/SldTableRowThree';
import { Scrollbars } from 'react-custom-scrollbars';

@connect(({ settled,project }) => ({
  settled,project
}))
@Form.create()
export default class BusinessInfo extends Component {
  constructor(props) {
    super(props);
    this.state = {
      //经营信息
      site_base_info: [
        {
          type: 'input',
          label: `${sldComLanguage('店铺名称')}`,
          name: 'storeName',
          placeholder: `${sldComLanguage('请输入店铺名称,最多100个字')}`,
          initialValue: '',
          required: true,
          disable: getSettleData('state') === 1,
          maxLength:100,
          rules: [{
            required: true,
            message: `${sldComLanguage('请输入店铺名称')}`,
          }],
        },
      ],
      // store_grade_data:[{
      //   type: 'scroll_table',
      //   label: `${sldComLanguage('店铺等级')}`,
      //   name: 'grade',
      //   data:[],
      //   width:600,
      //   columns:this.store_grade_column,
      //   rowKey:'gradeId',
      //   item_height: 300,
      //   scroll_height:200,
      //   required: true,
      //   handleSelectRows:this.handleSelectGrade,
      //   selectedRows:this.selectedRows,
      //   selectedRowKeys:this.selectedRowKeys,
      // }],//店铺等级数据
      // open_time_data:[{
      //   type: 'select',
      //   label: `${sldComLanguage('开店时长')}`,
      //   name: 'applyYear',
      //   placeholder: `${sldComLanguage('请选择开店时长')}`,
      //   sel_data: [],
      //   required: true,
      //   rules: [{
      //     required: true,
      //     message: `${sldComLanguage('请选择开店时长')}`,
      //   }],
      // }],//开店时长
      select_cat:[{
        width:200,
        type: 'select_cat',
        label: `${sldComLanguage('经营类型')}`,
        name: 'goodsCategoryIds',
        item_height: 300,
        disabled: getSettleData('state') === 1,
        placeholder: ``,
        paddingLeft: 0,
        tree_data: [],
        selected_keys:[],//选择的key数组
        select_cat_id: [],
        required: true,
      }],//选择经营类目
      company_qualification: {},
      loading: false,
      siteList: [],
      siteListLoading: false, // 站点tab列表loading
      selectCatLoading: false,
    };
  }

  store_grade_column = [
    {
      title: `${sldComLanguage('店铺等级')}`,//店铺等级
      dataIndex: 'gradeName',
      align: 'center',
      width: 100,
    },
    {
      title: `${sldComLanguage('可发布商品')}`,//可发布商品
      dataIndex: 'goodsLimit',
      align: 'center',
      width: 100,
    },
    {
      title: `${sldComLanguage('可推荐商品')}`,//可推荐商品
      dataIndex: 'recommendLimit',
      align: 'center',
      width: 100,
    },
    //todo 因业务需求暂时不需要此功能
    /*{
      title: `${sldComLanguage('收费标准')}`,//收费标准
      dataIndex: 'price',
      align: 'center',
      width: 100,
    },*/
    {
      title: `${sldComLanguage('申请说明')}`,//申请说明
      dataIndex: 'description',
      align: 'center',
      width: 200,
    },
  ];
  selectedRows = [];
  selectedRowKeys = [];

  componentDidMount () {
    // this.getStoreGrade();
    // this.getStoreOpenTime();
    this.getSiteList();
  }

  //获取平台分类
  getSystemCat = (webSite) => {
    const { dispatch } = this.props;
    return new Promise((resolve) => {
      dispatch({
      type: 'project/get_system_cat_tree_grade3',
      payload: { webSite },
      callback: (res) => {
        if(res.state == 200){
          if (res.data.length > 0) {
            res.data.map(item => {
              item.key = item.categoryId;
              item.title = item.categoryName;
              if (item.children != null&&item.children.length>0) {
                item.children.map(second => {
                  second.key = second.categoryId;
                  second.title = second.categoryName;
                  if (second.children != null&&second.children.length>0) {
                    second.children.map(third => {
                      third.key = third.categoryId;
                      third.title = third.categoryName;
                    });
                  }else{
                    second.disableCheckbox = true;
                  }
                });
              }else{
                item.disableCheckbox = true;
              }
            });
          }
          resolve({
            webSite,
            data: res.data
          });
        }
      },
    });
    })
    
  }

  // 获取站点列表
  getSiteList = () => {
    this.setState({ siteListLoading: true });
    const { dispatch } = this.props;
    const { select_cat, site_base_info } = this.state;
    dispatch({
      type: "global/get_site_list_data",
      payload: { isAll: true },
      callback: async (res) => {
        if (res.state == 200) {
          const siteListData = res.data;
          const promiseList = siteListData.map(item => this.getSystemCat(item.value))
          const result = await Promise.all(promiseList);
          result.forEach(r => {
              siteListData.forEach(siteInfo => {
                if (siteInfo.value === r.webSite) {
                  siteInfo.site_select_cat = [
                    {
                      ...select_cat[0],
                      tree_data: r.data,
                      onCheck: (checkedKeys, e) => this.handleCatCheck(checkedKeys, e, siteInfo.value)
                    }
                  ]
                  siteInfo.site_base_info = [
                    {
                      ...site_base_info[0],
                      name: `${siteInfo.value}_${site_base_info[0].name}` // ✅ 添加前缀
                    }
                  ]
                }
            })
          })
          this.setState({
            siteList: siteListData,
            siteListLoading: false,
          });
          this.initData(siteListData);
        }
      },
    });
  };

  setCatCache = (siteList, values) => {
    let params = { };
    params.siteInfoList = siteList.map(site => ({
      webSite: site.value,
      storeName: values[`${site.value}_storeName`],
      goodsCategoryIds: site.site_select_cat[0].select_cat_id.join(','),
      selected_keys: site.site_select_cat[0].selected_keys,
      id: site.id
    }))
    saveSettleData('bussinessInfo', JSON.stringify(params));//经营信息存缓存

    return params
  }


  //选择分类事件
  handleCatCheck = (checkedKeys, e, webSite) => {
    const select_cat_id = [];
    const { siteList } = this.state;
    e.checkedNodes.map(item_one=>{
      if(item_one.props.grade == 3){
        let tmp_data = item_one.props.path.split('/');
        select_cat_id.push(`${tmp_data[1]}-${tmp_data[2]}-${item_one.props.categoryId}`);
      }
    })
    const newSiteList = siteList.map(site_info => {
      if (site_info.value === webSite) {
        const siteSelectCat = site_info.site_select_cat[0];
        const newSelectCat = [
          {
            ...siteSelectCat,
            selected_keys: checkedKeys,
            select_cat_id,
          }
        ]
        return {
          ...site_info,
          site_select_cat: newSelectCat
        };
      }
      return site_info;
    })
    this.setState({siteList: newSiteList})
    // 设置缓存
    const values = this.props.form.getFieldsValue();
    this.setCatCache(newSiteList, values)
  };

  //店铺等级选择
  // handleSelectGrade = (rows, rowkeys) => {
  //   let {store_grade_data} = this.state;
  //   store_grade_data[0].selectedRows = rows;
  //   store_grade_data[0].selectedRowKeys = rowkeys;
  //   this.setState({
  //     selectedRows: rows,
  //     selectedRowKeys: rowkeys,
  //     store_grade_data
  //   });
  // }

  //获取店铺等级
  // getStoreGrade = () => {
  //   let {store_grade_data} = this.state;
  //   const { dispatch } = this.props;
  //   dispatch({
  //     type: 'settled/get_store_grade',
  //     payload: {pageSize:list_com_page_more},
  //     callback: (res) => {
  //       if (res.state == 200) {
  //         store_grade_data[0].data = res.data.list;
  //         this.setState({
  //           store_grade_data
  //         });
  //       }
  //     },
  //   });
  // }

  //获取开店时长列表
  // getStoreOpenTime = () => {
  //   let {open_time_data} = this.state;
  //   const { dispatch } = this.props;
  //   dispatch({
  //     type: 'settled/get_store_open_time',
  //     callback: (res) => {
  //       if (res.state == 200) {
  //         let tmp_data = [];
  //         res.data.map(item=>{
  //           tmp_data.push({key:item,name:item+'年'})
  //         });
  //         open_time_data[0].sel_data = tmp_data
  //         this.setState({
  //           open_time_data
  //         });
  //       }
  //     },
  //   });
  // }

  

  //初始化页面数据
  initData = (siteList) => {
    let tmp_data = getSettleData('bussinessInfo');
    const { siteInfoList } = tmp_data;
    if (tmp_data) {
      const newSiteList = siteList.map(site => {
        const tempSiteData = siteInfoList.find(i => i.webSite === site.value);
        const siteBaseInfo = site.site_base_info[0];
        const siteSelectCat = site.site_select_cat[0];
        site.site_base_info = [
          {
            ...siteBaseInfo,
            initialValue: tempSiteData.storeName,
          }
        ];
        site.site_select_cat  = [
          {
            ...siteSelectCat,
            select_cat_id: tempSiteData.goodsCategoryIds?.split(','),
            selected_keys: tempSiteData.selected_keys
          }
        ]
        site.id = tempSiteData.id;

        return site;
      })

      // store_grade_data[0].selectedRowKeys = [tmp_data.storeGradeId];
      // store_grade_data[0].selectedRows =  store_grade_data[0].data.filter(item=>item.gradeId == tmp_data.storeGradeId);
      // open_time_data.map(item=>{
      //   item.initialValue = tmp_data[item.name];
      // });


      this.setState({
        siteList: newSiteList
        // store_grade_data,
        // open_time_data,
      });
    }
  };

// 判断经营类型是否已选
 isHaveCatNotChecked = () => {
  const { siteList } = this.state;
  let err = {
    validate: true,
    webSite: '',
    msg: `${sldComLanguage('请选择经营类型～')}`
  }
  let breakFlag = false;

  for(let i in siteList){
     const site_info = siteList[i];
     const { site_select_cat } = site_info;
     const { select_cat_id } = site_select_cat[0]
     if (!select_cat_id.length) {
        err.webSite = site_info.title;
        err.validate = false;
        breakFlag = true;
        break; // 跳出内层
     }
  }
  return err;
 }

 handleNextStep = (e) => {
    const { siteList } = this.state;
    const { dispatch } = this.props;
    this.props.form.validateFieldsAndScroll((err, values) => {
        if (!err) {
          const { validate, msg, webSite } = this.isHaveCatNotChecked();
          if(!validate){
            failTip(`${webSite}${sldComLanguage('请选择经营类型～')}`);
            return false;
          }
          let base_info = JSON.parse(localStorage.getItem('baseInfo').replace(/&quot;/g,"\""));
          const params = this.setCatCache(siteList, values)
          let tar_params = {...params};
          tar_params.siteInfoList = tar_params.siteInfoList.map(item => ({
            webSite: item.webSite,
            storeName: item.storeName,
            goodsCategoryIds: item.goodsCategoryIds,
            id: item.id
          }));
          tar_params.companyAddress = base_info.companyAddress;//公司详细地址
          tar_params.companyProvinceCode = base_info.companyProvinceCode;//企业注册省编码
          tar_params.companyCityCode = base_info.companyCityCode;//	企业注册市编码
          tar_params.companyAreaCode = base_info.companyAreaCode;//企业注册区编码
          tar_params.areaInfo = base_info.areaInfo;//省市区名称
          tar_params.contactName = base_info.contactName;//联系人姓名
          tar_params.contactPhone = base_info.contactPhone;//联系人电话
          tar_params.enterType = base_info.enterType;//入驻类型：0-个人入驻，1-企业入驻
          tar_params.personCardDown = base_info.personCardDown;//身份证背面扫描件
          tar_params.personCardUp = base_info.personCardUp;//身份证正面扫描件
          if(tar_params.enterType == 1){
            //企业入驻
            tar_params.companyName = base_info.companyName;//公司名称
            tar_params.businessLicenseImage = base_info.businessLicenseImage;//营业执照扫描件,企业入驻时必传
            tar_params.moreQualification1 = base_info.moreQualification1?base_info.moreQualification1:'';//补充认证一
            tar_params.moreQualification2 = base_info.moreQualification2?base_info.moreQualification2:'';//补充认证二
            tar_params.moreQualification3 = base_info.moreQualification3?base_info.moreQualification3:'';//补充认证三
          }
          //只有小于当前的才需要更新
          if(getSettleData('cur_step')*1 < 3){
            saveSettleData('cur_step', 3);
          }
          this.setState({loading: true});
          dispatch({
            type: 'settled/save_apply',
            payload: tar_params,
            callback: res => {
              if (res.state == 200) {
                saveSettleData('cur_step', 3);
                message.success(res.msg);
                setTimeout(() => {
                  this.props.history.push('/apply/open_up');
                }, 1500);
              } else {
                message.error(res.msg);
              }
              this.setState({loading: false});
            },
          });
          }
    })
 }

  render() {
    
    const {
      // open_time_data,
      // store_grade_data,
      select_cat,
      loading,
      siteList,
      siteListLoading,
      selectCatLoading
    } = this.state;
    
    return (
      <div style={{ flex: 1, overflow: 'auto' }}>
        <div className={styles.title}>
          {sldLlineRtextAddGoodsAddMargin('#69A2F2', `${sldComLanguage('经营信息')}`, 0, 0, 5)}
        </div>
        {/* 站点tab列表 */}
        <Spin spinning={siteListLoading}>
          <div className={styles.site_list} style={{ paddingLeft: "85px" }}>
            {siteList.length ? siteList.map((site) => (
              <div className={ styles.site_item } key={site.name}>
                <div className={ styles.site_item_title }>{ site.title }</div>
                {getSldEmptyH(10)}
                <Scrollbars
                  autoHeight
                  autoHeightMin={100}
                  autoHeightMax={600}
                >
                  <div className={styles.commen_wrap}>
                    <SldTableRowThree form={this.props.form} data={site.site_base_info} />
                    {/*<SldTableRowThree form={this.props.form} data={store_grade_data}/>*/}
                    {/*<SldTableRowThree form={this.props.form} data={open_time_data}/>*/}
                    <Spin spinning={selectCatLoading}>
                      <SldTableRowThree
                        form={this.props.form}
                        data={site.site_select_cat}
                      />
                    </Spin>
                  </div>
                  {getSldEmptyH(10)}
                </Scrollbars>
              </div>
            )) : null}
          </div>
          {((getSettleData('state')&&getSettleData('state')==3)||!getSettleData('state'))&&
              <div className={styles.commen_wrap} style={{borderTopRightRadius:0,borderTopLeftRadius:0,marginTop:-30,paddingBottom:20,textAlign:'center'}}>
                <Button loading={loading} type='primary' onClick={this.handleNextStep} className={styles.next_step}>提交</Button>
              </div>
          }
        </Spin>        
      </div>
    );
  }
}
