<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="5" :sm="24">
              <a-form-item>
                <a-input v-model="queryParam.id" placeholder="名单姓名/家长手机号"/>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item>
                <a-select v-model="queryParam.status" placeholder="一级来源" default-value="0">
                  <a-select-option value="0">全部</a-select-option>
                  <a-select-option value="1">关闭</a-select-option>
                  <a-select-option value="2">运行中</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item>
                <a-select v-model="queryParam.status" placeholder="跟进状态" default-value="0">
                  <a-select-option value="0">全部</a-select-option>
                  <a-select-option value="1">关闭</a-select-option>
                  <a-select-option value="2">运行中</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item>
                <a-input v-model="queryParam.id" placeholder="请选择采单人姓名"/>
              </a-form-item>
            </a-col>
            <a-col :md="!advanced && 4 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      
      <a-row style="margin-bottom:15px">
        已选<a-button size="small" type="link">{{ selectedRows.length }}</a-button>条数据 <a-button size="small" @click="handleAssign">分配</a-button> <a-button size="small" @click="handleGain">捞取</a-button>
      </a-row>
      <s-table
        ref="table"
        size="default"
        :rowKey="record => record.id"
        :columns="columns"
        :data="loadData"
        :alert="false"
        :rowSelection="rowSelection"
        showPagination="auto">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="status" slot-scope="text">
          <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
        </span>
        <span slot="description" slot-scope="text">
          <ellipsis :length="4" tooltip>{{ text }}</ellipsis>
        </span>
      </s-table>

      <!-- 分配 -->
      <assign-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        :length="selectedRows.length"
        @cancel="handleCancel"
        @ok="handleOk"
        ></assign-form>

      <!-- 分配 -->
      <gain-form
        ref="gainModal"
        :visible="gainVisible"
        :length="selectedRows.length"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleGainCancel"
        @ok="handleGainOk"
        ></gain-form>
    </a-card>
  </page-header-wrapper>

</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components';
import { getRoleList, getServiceList } from '@/api/manage'
import AssignForm from '../modules/AssignForm'
import GainForm from '../modules/GainForm'

const columns = [
  {
    title: '#',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '规则编号',
    dataIndex: 'no'
  },
  {
    title: '描述',
    dataIndex: 'description',
    scopedSlots: { customRender: 'description' }
  },
  {
    title: '服务调用次数',
    dataIndex: 'callNo',
    sorter: true,
    needTotal: true,
    customRender: (text) => text + ' 次'
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '更新时间',
    dataIndex: 'updatedAt',
    sorter: true
  }
]

const statusMap = {
  0: {
    status: 'default',
    text: '关闭'
  },
  1: {
    status: 'processing',
    text: '运行中'
  },
  2: {
    status: 'success',
    text: '已上线'
  },
  3: {
    status: 'error',
    text: '异常'
  }
}


export default {
  name: 'SeasList',
  components: {
    STable,
    Ellipsis,
    AssignForm,
    GainForm
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      gainVisible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        console.log('loadData request parameters:', requestParameters)
        return getServiceList(requestParameters)
          .then(res => {
            return res.result
          })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  created() {

  },
  computed: {
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  mounted() {

  },
  methods: {
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    handleEdit (record) {
      this.visible = true;
      this.mdl = { ...record }
    },
    handleSub (record) {
      if (record.status !== 0) {
        this.$message.info(`${record.no} 订阅成功`)
      } else {
        this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      console.log(selectedRowKeys);
      console.log(selectedRows);
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    // 分配
    handleAssign() {
      if(this.selectedRows.length <= 0) {
        this.$message.error('请至少选择一个学员');
        return;
      }
      this.visible = true;
    },
    // 捞取
    handleGain() {
      if(this.selectedRows.length <= 0) {
        this.$message.error('请至少选择一个学员');
        return;
      }
      this.gainVisible = true
    },
    beforeOpen() {

    },
    handleOk () {
      const form = this.$refs.createModal.form;
      const ruleForm = this.$refs.createModal.$refs.ruleForm
      this.confirmLoading = true
      ruleForm.validate(valid  => {
        if (valid) {
          if (form.id > 0) {
            // 修改 e.g.
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
                console.log(form)
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              ruleForm.resetFields()
              // 刷新表格
              this.$refs.table.refresh()
              this.$message.info('修改成功')
            })
          } else {
            // 新增
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
                console.log(form)
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              ruleForm.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.info('新增成功')
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleGainOk() {
      this.gainVisible = false;
      this.$refs.table.refresh();
      this.$message.info('修改成功');
    },
    handleCancel () {
      this.visible = false
      const ruleForm = this.$refs.createModal.$refs.ruleForm
      ruleForm.resetFields() // 清理表单数据（可不做）
    },
    handleGainCancel() {
      this.gainVisible = false;
    }
  }
}
</script>

<style>

</style>
