/*
* 入驻店铺管理——续签列表
* */
import { connect } from 'dva/index';
import React, { Component, Fragment } from 'react';
import { Form, Spin, Switch } from 'antd';
import {
  list_com_page_size_10,
  dragSldTableColumn,
  getTableNum,
  sldComLanguage,
  sldHandlePaginationData,
  sldtbaleOpeBtnText,
  sldPopConfirmDiy,
  failTip,
  sucTip,
  showMoreHelpTip, sldIconBtn, sldPopConfirm, formItemLayoutModal,
} from '@/utils/utils';
import global from '@/global.less';
import StandardTable from '@/components/StandardTable';
import Search from '@/components/Search/Search';
import SldModal from '@/components/SldModal/SldModal';

let pageSize = list_com_page_size_10;
@connect(({ store }) => ({
  store,
}))
@Form.create()
export default class SettledStoreReNewList extends Component {
  constructor(props) {
    super(props);
    this.state = {
      search_height:0,
      initLoading: false,
      data: {},//列表数据
      selectedRows: [],
      selectedRowKeys: [],//selectedRows的key
      modal_width: 500,
      submiting: false,
      title: '',
      type: 'add',//'add'新增  'edit'编辑
      params: { pageSize: pageSize },//搜索条件
      operateData:[{
        type: 'select',
        label: `${sldComLanguage('拒绝理由')}`,//拒绝理由
        name: 'refuseReason',
        placeholder: `${sldComLanguage('拒绝理由')}`,//请选择拒绝理由
        sel_data: [],
        sele_key: 'reasonId',
        sele_name: 'content',
        diy: true,
        rules: [{
          required: true,
          message: `${sldComLanguage('拒绝理由')}`,//请选择拒绝理由
        }],
      },{
        type: 'textarea',
        label: `${sldComLanguage('备注')}`,//备注
        name: 'remark',
        placeholder: `${sldComLanguage('请输入审核拒绝理由，最多100字')}`,
        extra: `${sldComLanguage('最多输入100字')}`,
        maxLength: 100,
      }],
      search_data: [{
        type: 'input',
        label: `${sldComLanguage('店铺名称')}`,
        name: 'storeName',
        placeholder: `${sldComLanguage('请输入店铺名称')}`,
      },{
        type: 'input',
        label: `${sldComLanguage('店主账号')}`,
        name: 'vendorName',
        placeholder: `${sldComLanguage('请输入店主账号')}`,
      },{
        type: 'input',
        label: `${sldComLanguage('联系人')}`,
        name: 'contactName',
        placeholder: `${sldComLanguage('请输入联系人')}`,
      },{
        type: 'input',
        label: `${sldComLanguage('联系电话')}`,
        name: 'contactPhone',
        placeholder: `${sldComLanguage('请输入联系电话')}`,
      }
      ,{
          type: 'select',
          label: `${sldComLanguage('审核状态')}`,
          name: 'status',
          placeholder: `${sldComLanguage('请选择审核状态')}`,
          sel_data: [
            { key: '', name: `${sldComLanguage('全部')}` },
            { key: '2', name: `${sldComLanguage('已通过')}` },
            { key: '3', name: `${sldComLanguage('已拒绝')}` },
            { key: '4', name: `${sldComLanguage('待审核')}` },
          ],
        }
      // ,{
      //   type: 'select',
      //   label: `${sldComLanguage('付款状态')}`,
      //   name: 'state',
      //   placeholder: `${sldComLanguage('请选择付款状态')}`,
      //   sel_data: [
      //     { key: '', name: `${sldComLanguage('全部')}` },
      //     { key: '1', name: `${sldComLanguage('待付款')}` },
      //     { key: '2', name: `${sldComLanguage('已付款')}` },
      //   ],
      // }
      ],
      formValues: {},//搜索条件
      columns: [
        {
          title: '序号',
          align: 'center',
          width: 30,
          render: (text, record, index) => getTableNum(this.state.params, pageSize, index),
        },
        {
          title: `${sldComLanguage('店铺名称')}`,
          dataIndex: 'storeName',
          align: 'center',
          width: 100,
        },
        {
          title: `${sldComLanguage('店主账号')}`,
          dataIndex: 'vendorName',
          align: 'center',
          width: 100,
        },
        {
          title: `${sldComLanguage('联系人')}`,
          dataIndex: 'contactName',
          align: 'center',
          width: 100,
        },{
          title: `${sldComLanguage('联系电话')}`,
          dataIndex: 'contactPhone',
          align: 'center',
          width: 100,
        },{
          title: `${sldComLanguage('续签等级')}`,
          dataIndex: 'gradeName',
          align: 'center',
          width: 100,
        }
        // ,{
        //   title: `${sldComLanguage('收费标准(元/年)')}`,
        //   dataIndex: 'price',
        //   align: 'center',
        //   width: 150,
        // }
        ,{
          title: `${sldComLanguage('续签时长(年)')}`,
          dataIndex: 'duration',
          align: 'center',
          width: 150,
        }
        // ,{
        //   title: `${sldComLanguage('应付金额(元)')}`,
        //   dataIndex: 'payAmount',
        //   align: 'center',
        //   width: 120,
        // }
        ,{
          title: `${sldComLanguage('续签起止有效期')}`,
          dataIndex: 'startTime',
          align: 'center',
          width: 150,
          render: function(text, record) {
            return <div className={global.voucher_time_wrap}>
              <p>{text}</p>
              <p>~</p>
              <p>{record.endTime}</p>
            </div>;
          },
        }
        ,{
          title: `${sldComLanguage('审核状态')}`,
          dataIndex: 'statusName',
          align: 'center',
          width: 150,
        }
        ,{
          title: `${sldComLanguage('拒绝理由')}`,
          dataIndex: 'refuseReason',
          align: 'center',
          width: 100,
          render: (text, record) => (text ? text : '--'),
        }
        // ,{
        //   title: `${sldComLanguage('付款状态')}`,
        //   dataIndex: 'stateValue',
        //   align: 'center',
        //   width: 100,
        // },{
        //   title: `${sldComLanguage('付款方式')}`,
        //   dataIndex: 'paymentName',
        //   align: 'center',
        //   width: 100,
        //   render: function(text) {
        //     return text?text:'--';
        //   },
        // }
        ,{
          title: `${sldComLanguage('操作')}`,//操作
          align: 'center',
          width: 200,
          render: (text, record) => (
            <Fragment>
              {record.status == 2 &&
                <Fragment>
                  {sldtbaleOpeBtnText(`${sldComLanguage('--')}`)}
                </Fragment>
              }
              {record.status == 3 &&
                <Fragment>
                  {sldtbaleOpeBtnText(`${sldComLanguage('--')}`)}
                </Fragment>
              }
              {record.status == 4 &&
                <Fragment>
                  {sldtbaleOpeBtnText(`${sldComLanguage('审核通过')}`, () => this.operate({renewIds:record.renewId,isPass:true}))}
                  <span className={global.splitLine}></span>
                  {sldtbaleOpeBtnText(`${sldComLanguage('审核拒绝')}`, () => this.refuse(record.renewId))}
                </Fragment>
              }
            </Fragment>
          ),
        },
      ],
    };
  }

  componentDidMount() {
    this.get_list({ pageSize: pageSize });
    this.get_reason_list();//获取审核拒绝的理由
    this.resize();
  }

  resize = () =>{
    const {search_height} = this.state;
    if(this.refs.search_part!=undefined){
      if(this.refs.search_part.clientHeight != search_height){
        this.setState({search_height:this.refs.search_part.clientHeight})
      }
    }
  }

  //获取数据列表
  get_list = (params) => {
    this.setState({ initLoading: true });
    const { dispatch } = this.props;
    dispatch({
      type: 'store/get_store_renew_list',
      payload: { ...params},
      callback: (res) => {
        this.setState({ initLoading: false });
        if (res.state == 200) {
          res.data.list.map(item=>{
            if(item.status != 4){
              item.disabled = true;
            }
          });
          if (res.data.length == 0 && this.state.params.current > 1) {
            params.current = params.current - 1;
            this.get_list(params);
          } else {
            this.setState({
              data: res.data,
            });
          }
        }
      },
    });
  };



  handleSelectRows = (rows, rowkeys) => {
    this.setState({
      selectedRows: rows,
      selectedRowKeys: rowkeys,
    });
  };

  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 });
  };

  //搜索事件
  search = (data) => {
    const values = { ...data };
    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 });
  };

  //搜索模块点击展开/收起
  moreSearchToggle = () => {
    const {search_height} = this.state;
    if(this.refs.search_part!=undefined){
      if(this.refs.search_part.clientHeight != search_height){
        this.setState({search_height:this.refs.search_part.clientHeight})
      }
    }
  }

  reason_list = [];
  //获取审核拒绝的理由
  get_reason_list = () => {
    const { dispatch } = this.props;
    let { operateData } = this.state;
    dispatch({
      type: 'common/get_reason_list',
      payload: { type: 103, isShow: 1 },
      callback: (res) => {
        if (res.state == 200) {
          for (let i in operateData) {
            if (operateData[i].name === 'refuseReason') {
              operateData[i].sel_data = res.data.list;
              break;
            }
          }
          this.reason_list = res.data.list;
          this.setState({ operateData });
        }
      },
    });
  };

  //操作
  operate = (parm) => {
    const { params } = this.state;
    const { dispatch } = this.props;
    let param_data = {};
    let dis_type = 'store/batch_store_renew_list';
    param_data = parm;
    this.setState({submiting:true})
    dispatch({
      type: dis_type,
      payload: param_data,
      callback: (res) => {
        if (res.state == 200) {
          sucTip(res.msg);
          this.get_list(params);
          this.setState({
            modalVisible:false,
            selectedRows: [],
            selectedRowKeys: [],
          })
        } else {
          failTip(res.msg);
        }
        this.setState({submiting:false})
      },
    });
  };

  //审核拒绝的method
  operate_ids = '';//当前操作的id串
  refuse = (ids) => {
    let { operateData } = this.state;
    operateData = JSON.parse(JSON.stringify(operateData));
    this.operate_ids = ids;
    this.setState({ modalVisible: true, operateData });
  }
//审核拒绝之后的弹框的取消method
  sldHandleCancle = () => {
    this.setState({ modalVisible: false });
  };
//审核拒绝之后的弹框的确定method
  sldHandleConfirm = (val) => {
    let selectd_reason = this.reason_list.filter(item => item.reasonId == val.refuseReason)[0];
    val.refuseReason = selectd_reason.content;
    val.isPass = false;
    val.renewIds = this.operate_ids;
    this.operate(val)
  };





  render() {
    const { selectedRows, selectedRowKeys,search_data, columns, initLoading, data,search_height,modal_width,submiting,modalVisible,operateData,show_foot} = this.state;
    return (
      <div className={global.common_page} style={{ flex: 1,paddingTop:0 }}>
        {showMoreHelpTip('',[`${sldComLanguage('店铺等级发生变化时，续签的店铺等级会在新的签约有效期内生效')}`],10)}
        <div className={global.tableListForm} ref={'search_part'}>
          <Search search_data={search_data} moreSearchToggle={() => this.moreSearchToggle()}
                  seaSubmit={(data) => this.search(data)} seaReset={() => this.seaReset()}/>
        </div>
        {/*公共功能条-start*/}
        <div className={global.operate_bg}
             style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between' }}>
          <div style={{ display: 'flex', flexDirection: 'row' }}>
            {selectedRowKeys.length == 0 ? sldIconBtn(() => {
              failTip(`${sldComLanguage('请先选中数据')}`);//请先选中数据
              //确认审核通过选中的商品吗？
            }, `${sldComLanguage('审核通过')}`, 7, 0, 19, 19, 3, 'shenhetongguo', '#0fb39a') : sldPopConfirm('leftBottom', `${sldComLanguage('确认审核通过选中的续签吗？')}`, () => this.operate({renewIds:selectedRowKeys.join(','),isPass:true}), `${sldComLanguage('确定')}`, `${sldComLanguage('取消')}`, sldIconBtn(null, `${sldComLanguage('审核通过')}`, 7, 0, 19, 19, 3, 'shenhetongguo', '#0fb39a'), 0, 0, '#0fb39a')}

            {selectedRowKeys.length == 0 ? sldIconBtn(() => {
              failTip(`${sldComLanguage('请先选中数据')}`);//请先选中数据
              //确认审核拒绝选中的商品吗？
            }, `${sldComLanguage('审核拒绝')}`, 7, 0, 15, 15, 3, 'shenhejujue', '#fa0920') : sldIconBtn(() => this.refuse(selectedRowKeys.join(',')), `${sldComLanguage('审核拒绝')}`, 7, 0, 15, 15, 3, 'shenhejujue', '#fa0920')}
          </div>
        </div>
        {/*公共功能条-end*/}
        <Spin spinning={initLoading}>
          {/*标准表格-start*/}
          <StandardTable
            totalHeight={document.body.clientHeight - 130 - search_height - 20 - 52}
            bordered={false}
            selectedRows={selectedRows}
            data={data}
            rowKey={'renewId'}
            isCheck={true}
            columns={columns}
            onSelectRow={this.handleSelectRows}
            onChange={(pagination, filtersArg, sorter) => this.handleTablePagination(pagination, filtersArg, sorter, 'main')}
            onSldHandleSeleRow={this.onSldHandleSeleRow}
            resizeTable={(index, size) => this.resizeTable(index, size, 'columns', columns)}
            isColumnResize={true}
          />
          {/*标准表格-end*/}
        </Spin>
        <SldModal
          title={`${sldComLanguage('审核拒绝理由')}`}
          width={modal_width}
          submiting={submiting}
          modalVisible={modalVisible}
          sldHandleConfirm={(val) => this.sldHandleConfirm(val)}
          sldHandleCancle={this.sldHandleCancle}
          formItemLayoutModal={formItemLayoutModal}
          content={operateData}
          show_foot={show_foot}
        />
      </div>

    );
  }
}
