import { connect } from 'dva/index';
import React, { Component } from 'react';
import { Form, Spin } from 'antd';
import {
  failTip,
  sucTip,
  sldComLanguage,
} from '@/utils/utils';
import { sld_need_update_setting, sld_config_save_btn } from '@/utils/util_data';
import global from '@/global.less';
import SldTableEdit from '@/components/SldTableEdit/SldTableEdit';
import SldComHeader from '@/components/SldComHeader';
import {apiUrl} from "@/utils/sldconfig";

let sthis = '';
@connect(({ sldsetting }) => ({
  sldsetting,
}))
@Form.create()
export default class SiteInfo extends Component {
  constructor(props) {
    super(props);
    sthis = this;
    this.state = {
      flag: 0,
      submitting: false,//提交按钮加载状态
      initLoading: false,//页面初始化加载状态
      info_data: [],
    };
  }

  componentDidMount() {
    this.get_siet_base_info();
  }

  componentWillUnmount() {

  }

  //获取站点的基本信息
  get_siet_base_info = () => {
    const { dispatch } = this.props;
    let { info_data } = this.state;
    this.setState({initLoading: true})
    dispatch({
      type: 'sldsetting/get_site_base_info',
      callback: (res) => {
        if (res.state == 200) {
          for (let i in res.data) {
             if(res.data[i].name =='about_pic' || res.data[i].name =='about_banner') {
               let fileList = [];
               let tmp_data = {};
               tmp_data.uid = new Date();
               tmp_data.name = res.data[i].name;
               tmp_data.status = 'done';
               tmp_data.url = res.data[i].value;
               fileList.push(tmp_data);
                info_data.push({
                  type: 'upload_img_upload',
                  label: res.data[i].title,
                  name: res.data[i].name,
                  extra: res.data[i].description,
                  fileList: fileList,
                  upload_name: 'file',
                  upload_url: apiUrl + `v3/oss/common/upload?source=setting`,
                  uploadPreview: this.uploadImgPre,
                  img_succ_info:'',
                  uploadChange: (info) => this.uploadImg(info, res.data[i].name, res.data[i].name),
                  initialValue: res.data[i].value,
                });
              }
             else  if(res.data[i].name == 'about_text'){
               info_data.push({
                 type: 'ueditor',
                 label: res.data[i].title,
                 name: res.data[i].name,
                 extra: res.data[i].description,
                 flag:false,
                 initialValue: res.data[i].value
               });
             }
          }
          if (info_data.length > 0) {
            info_data.push({
              type: 'button',
              label: '',
              name: 'button',
              show:false
            });
          }
        }
        this.setState({ info_data, flag: 1, initLoading: false });
      },
    });
  };

  //预览图片
  uploadImgPre = (info) => {
    this.viewImg(true, info.name);
  };

  //上传图片 dataType:用于区分更改那个数据
  uploadImg = (info, dataType) => {
    let { info_data } = this.state;
    if (info.file.status != undefined && info.file.status != 'error') {
      for (let i in info_data) {
        if (info_data[i].name == dataType) {
          info_data[i].fileList = info.fileList;
          info_data[i].img_succ_info = (info.file.response != undefined && info.fileList.length > 0 && info.file.response.data != undefined) ? info.file.response.data : [];
        }
      }
      this.setState({ info_data });
    }
  };


  //保存事件
  handleSubmit = (values,con) => {
    console.log(values)
    this.setState({ submitting: true });
    const { dispatch } = this.props;
    let parmas = {}
    parmas.seller_center_entrance_is_enable = values.seller_center_entrance_is_enable ? 1 : 0;
    parmas.verification_code_check_is_enable = values.verification_code_check_is_enable ? 1 : 0;
    parmas.pay_test_enable = values.pay_test_enable ? 1 : 0;
    parmas.about_text = con
    parmas.about_banner = typeof (values.about_banner) === 'string' ? values.about_banner : values.about_banner.file.response.data.url
    parmas.about_pic = typeof (values.about_pic) === 'string' ? values.about_pic : values.about_pic.file.response.data.url
    dispatch({
      type: 'common/saveSetting',
      payload: parmas,
      callback: (res) => {
        this.setState({ submitting: false });
        if (res.state == 200) {
          sucTip(res.msg);
        } else {
          failTip(res.msg);
        }
      },
    });
  };



  render() {
    const { info_data, submitting, initLoading, flag } = this.state;
    return (
      <Spin spinning={initLoading}>
        <div className={global.common_page}>
          <SldComHeader
            type={2}
            title={sldComLanguage('关于我们')}
            tip_title={''}
            tip_data={sld_need_update_setting()}
          />
          <div className={'about_wrap'}>
            {flag == 1 &&
            <SldTableEdit
              submiting_sec={submitting}
              width={1000}
              data={info_data}
              handleSubmit={this.handleSubmit}
              ueFlag={{
                flag:false
              }}
              showAboutUsUeditor={{
                text:`${sldComLanguage('保存并更新')}`,
              }}
            />
            }
          </div>

        </div>
      </Spin>
    );
  }
}
