import React from 'react';
import { Button, Upload } from 'antd';
import { Link } from 'react-router-dom';
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, formatDate } from '@src/services/Tools';
import { asyncSMessage, asyncDelConfirm } from '@src/services/AsyncTools';
import { PreviewStatus } from '../../../../Constant';
import { Exercise } from '../../../stores/exercise';
import { Preview } from '../../../stores/preview';
import { System } from '../../../stores/system';

const filterForm = [
  {
    key: 'category',
    type: 'select',
    allowClear: true,
    name: '课程',
    placeholder: '请选择',
    number: true,
  },
  {
    key: 'status',
    type: 'select',
    name: '状态',
    select: PreviewStatus,
    number: true,
    placeholder: '请选择',
  },
];
export default class extends Page {
  constructor(props) {
    super(props);
    this.actionList = [{
      key: 'add',
      name: '新建作业',
      render: (item) => {
        // return <Link to='/subject/preview/add'><Button>{item.name}</Button></Link>;
        return <Button onClick={() => {
          linkTo('/subject/preview/detail');
        }}>{item.name}</Button>;
      },
    }, {
      key: 'template',
      name: '下载批量导入模版',
      render: (item) => {
        return <a href={`/${encodeURIComponent('预习作业导入模板')}.xlsx`}>{item.name}</a>;
      },
    }, {
      key: 'import',
      name: '批量导入',
      render: (item) => {
        return <Upload
          showUploadList={false}
          beforeUpload={(file) => System.uploadImage(file).then((result) => {
            asyncSMessage(result);
          })}
        >
          <Button>{item.name}</Button>
        </Upload>;
      },
    }, {
      key: 'delete',
      name: '批量删除',
      needSelect: 1,
    }];
    this.categoryMap = {};
    this.columns = [{
      title: '课程',
      dataIndex: 'category',
      render: (text) => {
        return this.categoryMap[text] || text;
      },
    }, {
      title: '开始时间',
      dataIndex: 'startTime',
      render: (text) => {
        return formatDate(text);
      },
    }, {
      title: '结束时间',
      dataIndex: 'endTime',
      render: (text) => {
        return formatDate(text);
      },
    }, {
      title: '作业标题',
      dataIndex: 'title',
    }, {
      title: '完成进度',
      dataIndex: 'finish',
      render: (text, record) => {
        return `${text}/${record.userIds.length}`;
      },
    }, {
      title: '操作',
      dataIndex: 'handler',
      render: (text, record) => {
        return <div className="table-button">
          {(
            <Link to={`/subject/preview/detail/${record.id}`}>编辑</Link>
          )}
          {(
            <Link to={`/user/previewList?previewId=${record.id}&startTime=${record.startTime}&endTime=${record.endTime}`}>查看</Link>
          )}
          {(
            <Button
              size="small"
              type="link"
              onClick={() => {

              }}
            >
              复制
          </Button>
          )}
        </div>;
      },
    }];
  }

  init() {
    Exercise.allStruct().then(result => {
      filterForm[0].select = result.filter(row => row.level === 2).map(row => { row.title = `${row.titleZh}/${row.titleEn}`; row.value = row.id; return row; });
      this.categoryMap = getMap(filterForm[0].select, 'id', 'title');
      this.setState({ exercise: result });
    });
  }

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

  deleteAction() {
    const { selectedKeys } = this.state;
    asyncDelConfirm('删除确认', '是否删除选中作业?', () => {
      return Promise.all(selectedKeys.map(row => Preview.delStruct({ id: row })));
    }).then(() => {
      asyncSMessage('删除成功!');
      this.refresh();
    });
  }

  renderView() {
    return <Block flex>
      <FilterLayout
        show
        itemList={filterForm}
        data={this.state.search}
        onChange={data => {
          this.search(data);
        }} />
      <ActionLayout
        itemList={this.actionList}
        selectedKeys={this.state.selectedKeys}
        onAction={key => this.onAction(key)}
      />
      <TableLayout
        select
        columns={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>;
  }
}
