import React from 'react';
import './index.less';
import Page from '@src/containers/Page';
import Block from '@src/components/Block';
// import FilterLayout from '@src/layouts/FilterLayout';
import ActionLayout from '@src/layouts/ActionLayout';
import TableLayout from '@src/layouts/TableLayout';
import { getMap, formatTreeData } from '@src/services/Tools';
import { asyncSMessage, asyncForm } from '@src/services/AsyncTools';
import { ServiceKey, ServiceParamMap, SwitchSelect } from '../../../../Constant';
import { Course } from '../../../stores/course';
import { Exercise } from '../../../stores/exercise';

const SwitchSelectMap = getMap(SwitchSelect, 'value', 'label');

export default class extends Page {
  constructor(props) {
    super(props);
    this.exerciseMap = {};
    this.actionList = [{
      key: 'add',
      type: 'primary',
      name: '创建',
    }];

    this.itemList = [{
      key: 'id',
      type: 'hidden',
    }, {
      key: 'title',
      type: 'input',
      name: '套餐名称',
    }, {
      key: 'structId',
      type: 'select',
      select: [],
      name: '套餐学科',
    }, {
      key: 'description',
      type: 'input',
      name: '套餐描述',
    }, {
      key: 'price',
      type: 'number',
      name: '套餐价格',
    }, {
      key: 'courseIds',
      type: 'multiple',
      name: '包含课程',
    }];
    ServiceKey.forEach((row) => {
      const list = ServiceParamMap[row.value];
      const item = {
        key: `gift.${row.value}`,
        type: 'number',
        name: `赠送${row.label}`,
      };
      if (list) {
        item.select = list;
        item.type = 'select';
      }
      this.itemList.push(item);
    });

    this.columns = [{
      title: '套餐名称',
      dataIndex: 'title',
    }, {
      title: '套餐学科',
      dataIndex: 'structId',
      render: (text) => {
        return this.exerciseMap[text];
      },
    }, {
      title: '套餐价格',
      dataIndex: 'price',
    }, {
      title: '销售数量',
      dataIndex: 'saleNumber',
    }, {
      title: '首页推荐',
      dataIndex: 'isSpecial',
      render: (text) => {
        return SwitchSelectMap[text] || text;
      },
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          {(
            <a onClick={() => {
              this.editAction(record);
            }}>编辑</a>
          )}
          {!!record.isSpecial && (
            <a onClick={() => {
              this.special(record, 0);
            }}>取消推荐</a>
          )}
          {!record.isSpecial && (
            <a onClick={() => {
              this.special(record, 1);
            }}>首页推荐</a>
          )}
        </div>;
      },
    }];
  }

  init() {
    Exercise.courseStruct().then((result) => {
      const list = result.filter(row => row.level === 1).map(row => {
        row.title = `${row.titleZh}`;
        row.value = row.id;
        return row;
      });
      this.itemList[2].select = list;
      this.exerciseMap = getMap(list, 'id', 'title');
      this.setState({ exercise: formatTreeData(list, 'id', 'title', 'parentId') });
    });
    Course.list().then((result) => {
      this.itemList[5].select = result.list.map(row => {
        row.value = row.id;
        return row;
      });
    });
  }

  initData() {
    Course.listPackage(this.state.search).then(result => {
      this.setTableData(result.list, result.total);
    });
  }

  detailAction(row) {
    this.setState({ detail: row });
  }

  addAction() {
    asyncForm('创建', this.itemList, {}, data => {
      return Course.addPackage(data).then(() => {
        asyncSMessage('添加成功!');
        this.refresh();
      });
    });
  }

  editAction(row) {
    asyncForm('编辑', this.itemList, row, data => {
      return Course.editPackage(data).then(() => {
        asyncSMessage('编辑成功!');
        this.refresh();
      });
    });
  }

  special(row, isSpecial) {
    Course.editPackage({ id: row.id, isSpecial }).then(() => {
      asyncSMessage('编辑成功!');
      this.refresh();
    });
  }

  renderView() {
    return <Block flex>
      {/* <FilterLayout
        show
        itemList={this.filterForm}
        data={this.state.search}
        onChange={data => {
          this.search(data);
        }} /> */}
      <ActionLayout
        itemList={this.actionList}
        selectedKeys={this.state.selectedKeys}
        onAction={key => this.onAction(key)}
      />
      <TableLayout
        columns={this.tableSort(this.columns)}
        list={this.state.list}
        pagination={this.state.page}
        loading={this.props.core.loading}
        onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
        onSelect={(keys, rows) => this.tableSelect(keys, rows)}
        selectedKeys={this.state.selectedKeys}
      />
    </Block>;
  }
}