import { connect } from "dva/index";
import router from "umi/router";
import React, { Component, Fragment } from "react";
import { Form, Spin, Switch } from "antd";
import {
  sldIconBtn,
  failTip,
  sucTip,
  getSldEmptyH,
  dragSldTableColumn,
  sldHandlePaginationData,
  formItemLayoutModal,
  getTableNum,
  sldComLanguage,
  getSldCopyData,
  sldtbaleOpeBtnText,
  sldPopConfirmDiy,
  mobile_reg,
  showMoreHelpTip,
  list_com_page_size_10,
  validatorNumbe,
  list_com_page_more,
  getSldComShowMoreTtex,
  sldLlineRtextAddGoods,
  sldLlineRtextAddGoodsAddMargin,
  dateFormat
} from "@/utils/utils";
import global from "@/global.less";
import StandardTable from "@/components/StandardTable";
import SldModal from "@/components/SldModal/SldModal";
import Search from "@/components/Search/Search";
import Link from "umi/link";

let pageSize = list_com_page_size_10;
@connect(({ order, common, global }) => ({
  order,
  common,
  global,
}))
@Form.create()
export default class EnquiryLists extends Component {
  constructor(props) {
    super(props);
    this.state = {
      initLoading: false,
      show_foot: false,
      data: {}, //列表数据
      title: "",
      currentSite: props.global.currentSite,
      params: { pageSize: pageSize }, //搜索条件
      operateData: [], //操作的数据
      formValues: {}, //搜索条件、
      columns: [
        {
          title: "序号",
          dataIndex: "id",
          align: "center",
          width: 55,
          render: (text, record, index) =>
            getTableNum(this.state.params, pageSize, index),
        },
        {
          title: `${sldComLanguage("公司名称")}`,
          dataIndex: "companyName",
          align: "center",
          width: 100,
        },
        {
          title: `${sldComLanguage("手机号")}`,
          dataIndex: "phone",
          align: "center",
          width: 100,
        },
        {
          title: `${sldComLanguage("月份")}`,
          dataIndex: "queryMonth",
          align: "center",
          width: 80,
        },
        {
          title: `${sldComLanguage("当月已消耗次数")}`,
          dataIndex: "usedCount",
          align: "center",
          width: 80,
        },
        {
          title: `${sldComLanguage("当月剩余次数")}`,
          dataIndex: "leftCount",
          align: "center",
          width: 80,
        },
        {
          title: `${sldComLanguage("报告获取时间")}`,
          dataIndex: "reportTime",
          align: "center",
          width: 100,
        },
        {
          title: `${sldComLanguage("操作")}`,
          width: 80,
          align: "center",
          //   fixed: "right",
          render: (text, record) => (
            <Fragment>
              {sldtbaleOpeBtnText(`${sldComLanguage("查看报告")}`, () =>
                this.detailRecord(record)
              )}
            </Fragment>
          ),
        },
      ],
      search_data: [
        {
          type: "input",
          label: `${sldComLanguage("公司名称")}`,
          name: "companyName",
          placeholder: `${sldComLanguage("公司名称")}`,
        },
        {
          type: "input",
          label: `${sldComLanguage("手机号")}`,
          name: "phone",
          placeholder: `${sldComLanguage("请输入手机号")}`,
        },
        {
          type: "rangepicker",
          label: `${sldComLanguage("获取报告时间")}`,
          name: "search_report_time",
          width: 350,
          placeholder1: `${sldComLanguage("开始时间")}`,
          placeholder2: `${sldComLanguage("结束时间")}`,
        },
      ],
    };
  }

  componentDidMount() {
    const { currentSite } = this.state;
    if (currentSite !== "2") {
      router.push("/manage_order/enquiry");
      return;
    }
    this.get_list({ pageSize: pageSize });
  }

  detailRecord = (record) => {
    if (record.pdfUrl) {
      window.open(record.pdfUrl, "_blank");
    }
  };

  //获取数据列表
  get_list = (params) => {
    this.setState({ initLoading: true });
    const { dispatch } = this.props;
    dispatch({
      type: "order/get_ai_business_list",
      payload: { ...params },
      callback: (res) => {
        this.setState({ initLoading: false });
        if (res.state == 200) {
          if (
            (res.data.list == null || res.data.list.length == 0) &&
            this.state.params.current > 1
          ) {
            params.current = params.current - 1;
            this.get_list(params);
          } else {
            this.setState({
              data: res.data,
            });
          }
        }
      },
    });
  };

  handleTablePagination = (pagination, filtersArg, sorter, type = "main") => {
    const { formValues } = this.state;
    if (type == "main") {
      const params = sldHandlePaginationData(
        pagination,
        filtersArg,
        sorter,
        formValues
      );
      pageSize = params.pageSize;
      this.setState({ params });
      this.get_list(params);
    }
  };

  //表格拖动
  resizeTable = (index, size, type, data) => {
    let datas = dragSldTableColumn(index, size, data);
    this.setState({ [type]: datas });
  };

  //搜索框内容的变化
  sldSearChange = (val) => {
    this.setState({
      search_con: val.target.value,
    });
  };

  //清空搜索内容
  sldSearClear = () => {
    this.setState({
      search_con: "",
    });
    this.sldSearch("");
  };

  //搜索事件
  search = (data) => {
    const values = { ...data };
    if (values.search_report_time) {
      values.startTime = values.search_report_time[0] ? values.search_report_time[0].format(dateFormat) + ' 00:00:00' : '';
      values.endTime = values.search_report_time[1] ? values.search_report_time[1].format(dateFormat) + ' 23:59:59' : '';
      delete values.search_report_time;
    }
    for (let i in values) {
      if (values[i] == "") {
        delete values[i];
      }
    }
    this.setState({
      formValues: values,
      params: { pageSize: pageSize },
    });
    this.get_list({ pageSize: pageSize, ...values });
  };
  //搜索重置事件
  seaReset = () => {
    //搜索条件置为空
    this.setState({
      formValues: {},
      params: { pageSize: pageSize },
    });
    this.get_list({ pageSize: pageSize });
  };

  render() {
    const {
      search_data,
      columns,
      initLoading,
      data,
      submiting,
      title,
      show_foot,
    } = this.state;
    const renderSerchData = search_data.filter((item) => !item.hide);
    return (
      <div className={global.common_page} style={{ flex: 1 }}>
        {sldLlineRtextAddGoodsAddMargin(
          "#69A2F2",
          `${sldComLanguage("ai商情分析列表")}`,
          0,
          0,
          10
        )}
        <div className={global.tableListForm}>
          <Search
            search_data={renderSerchData}
            seaSubmit={(data) => this.search(data)}
            seaReset={() => this.seaReset()}
          />
        </div>

        <Spin spinning={initLoading}>
          <div className={global.self_enquiry_list_wrap}>
            {/*标准表格-start*/}
            <StandardTable
              data={data}
              rowKey={"id"}
              isCheck={false}
              columns={columns}
              onChange={(pagination, filtersArg, sorter) =>
                this.handleTablePagination(
                  pagination,
                  filtersArg,
                  sorter,
                  "main"
                )
              }
              resizeTable={(index, size) =>
                this.resizeTable(index, size, "columns", columns)
              }
              isColumnResize={true}
            />
            {/*标准表格-end*/}
          </div>
        </Spin>
      </div>
    );
  }
}
