<template>
  <div>
    <a-space direction="vertical" style="width: 100%;">
      <a-space direction="horizontal">
        项目名:
        <a-input v-model="queryParam.projectName" style="width: 100%" />
        项目状态:
        <a-select v-model="queryParam.status" placeholder="请选择" default-value="null" style="width: 120px">
          <a-select-option value="null">全部</a-select-option>
          <a-select-option value="1">运行中</a-select-option>
          <a-select-option value="2">已结束</a-select-option>
          <a-select-option value="1">未发布</a-select-option>
        </a-select>
        开始时间:
        <a-date-picker v-model="queryParam.stateDate" style="width: 100%" placeholder="请输入开始时间" />
        结束时间:
        <a-date-picker v-model="queryParam.endDate" style="width: 100%" placeholder="请输入结束时间" />
        培训方式:
        <a-select v-model="queryParam.trainWay" placeholder="请选择" default-value="null" style="width: 120px">
          <a-select-option value="null">全部</a-select-option>
          <a-select-option value="1">培训</a-select-option>
          <a-select-option value="2">考试</a-select-option>
          <a-select-option value="3">培训-练习</a-select-option>
          <a-select-option value="4">培训-练习-考试</a-select-option>
        </a-select>

        <a-button type="primary" icon="search" @click="$refs.table.refresh(true)">查询</a-button>
        <a-button icon="redo" @click="() => (queryParam = {})">重置</a-button>
      </a-space>

      <a-space direction="horizontal">
        <a-button type="primary" icon="plus" @click="handleEdit()">新建</a-button>
        <a-button type="primary" icon="minus" @click="handleEdit()">删除</a-button>
      </a-space>

      <s-table ref="table" size="default" rowKey="key" :columns="columns" :data="loadData" :rowSelection="options.rowSelection">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link"> 更多
              <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;">详情</a>
              </a-menu-item>
              <a-menu-item v-if="$auth('table.disable')">
                <a href="javascript:;">禁用</a>
              </a-menu-item>
              <a-menu-item v-if="$auth('table.delete')">
                <a href="javascript:;">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </s-table>

    </a-space>

  </div>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import { getProjectList } from '@/api/project/project'

export default {
  name: 'TableList',
  components: {
    STable,
  },
  data() {
    return {
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '序号',
          width: 60,
          scopedSlots: { customRender: 'serial' },
        },
        {
          title: '项目名称',
          dataIndex: 'projectName',
          key: 'projectName'
        },
        {
          title: '时间',
          dataIndex: 'description',
        },
        {
          title: '人数',
          dataIndex: 'callNo',
          // sorter: true,
          needTotal: true,
          // customRender: (text) => text + ' 次',
        },
        {
          title: '项目类型',
          dataIndex: 'description',
        },
        {
          title: '项目状态',
          dataIndex: 'description',
        },
        {
          title: '创建人员',
          dataIndex: 'projectStatus',
        },
        {
          title: '创建时间',
          dataIndex: 'updatedAt',
          sorter: true,
        },
        // {
        //   title: '操作',
        //   dataIndex: 'action',
        //   width: '150px',
        //   scopedSlots: { customRender: 'action' },
        // },
        {
          title: '操作',
          key: 'operation',
          width: 100,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],

      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getProjectList(Object.assign(parameter, this.queryParam)).then(res => { return res; })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: {
          show: true,
          clear: () => {
            this.selectedRowKeys = []
          },
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange,
        },
      },
      optionAlertShow: false,
    }
  },
  created() {
    this.tableOption()
    getRoleList({ t: new Date() })
  },
  methods: {
    tableOption() {
      if (!this.optionAlertShow) {
        this.options = {
          alert: {
            show: true,
            clear: () => {
              this.selectedRowKeys = []
            },
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
          },
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null,
        }
        this.optionAlertShow = false
      }
    },

    handleEdit(record) {
      this.$emit('onEdit', record)
    },
    handleOk() { },

    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },

    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date()),
      }
    },
  },
}
</script>
