<template>
  <a-card :bordered="false" class="lotterySet-page-info">
    <!-- 搜索框 -->
    <div class="lotterySet-search">
      <a-form layout="inline" v-bind="formItemLayout" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="20">
          <a-col :span="6">
            <a-form-item label="活动名称" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-input
                allowClear
                v-model="searchData.activeName"
                :maxLength="30"
                placeholder="请输入活动名称"
                id="lotterySet-name" />
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item label="状态" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <v-select
                code="ENABLE_FLAG"
                v-model="searchData.state"
                allowClear
                placeholder="请选择状态"
                id="lotterySet-enable-state"></v-select>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-button class="handle-btn serach-btn" style="margin-right: 10px;" type="primary" @click="$refs.table.refresh(true)" id="lotterySet-refresh">查询</a-button>
            <a-button class="handle-btn" @click="resetForm" id="lotterySet-resetForm">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="add">
      <a-button
        type="primary"
        icon="plus"
        class="addBtn"
        @click="showModal"
        v-if="$hasPermissions('B_lottery_new')"
        id="lotterySet-showModal"
      >新增</a-button>
    </div>
    <!-- table列表 -->
    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      bordered>
      <span slot="action" slot-scope="text, record">
        <a-icon
          :type="record.state==1?'eye':'edit'"
          id="lotterySet-handleEdit"
          :title="record.state==1?'查看':'编辑'"
          class="actionBtn icon-blues"
          v-if="$hasPermissions('B_lottery_view')||$hasPermissions('B_lottery_edit')"
          @click="handleEdit(record)" />
        <a-icon
          v-if="record.participantsNum===0 && record.state==0 && $hasPermissions('B_lottery_del')"
          type="delete"
          id="lotterySet-handleDel"
          title="删除"
          class="actionBtn icon-red"
          @click="handleDel(record)" />
      </span>
      <span slot="actime" slot-scope="text, record">
        {{ record.activeStartTime.split(' ')[0] }} 至 {{ record.activeEndTime.split(' ')[0] }}
      </span>
      <span slot="state" slot-scope="text, record">
        <a-switch
          checkedChildren="启用"
          unCheckedChildren="禁用"
          id="lotterySet-changeFlagHandle"
          v-if="$hasPermissions('B_lottery_enable')"
          :checked="record.state == 1 ? true : false"
          @change="changeFlagHandle(text, record)" />
        <span v-else>--</span>
      </span>
      </span>
    </s-table>
    <addlotterySetModal
      :itemId="itemId"
      :showEditModal="showEditModal"
      @refresh="$refs.table.refresh(true)"
      @close="cancel"></addlotterySetModal>
  </a-card>
</template>

<script>
import {
  Upload,
  STable,
  VSelect
} from '@/components'
import addlotterySetModal from './addlotterySetModal.vue'
import {
  getLuckyDrawList, enableLuckyDraw, deleteLuckyDraw
} from '@/api/luckyDraw.js'

export default {
  components: {
    STable,
    Upload,
    VSelect,
    addlotterySetModal
  },
  data () {
    return {
      formItemLayout: {
        labelCol: { span: 7 },
        wrapperCol: { span: 17 }
      },
      searchData: {
        activeName: '',
        state: undefined
      },
      showEditModal: false, // 默认关闭弹窗
      itemId: null, // 编辑行id
      // 表头
      columns: [{
        title: '序号',
        dataIndex: 'no',
        width: 60,
        align: 'center'
      },
      {
        title: '创建时间',
        width: 100,
        dataIndex: 'createDate',
        align: 'center'
      },
      {
        title: '活动名称',
        width: 100,
        dataIndex: 'activeName',
        align: 'center'
      },
      {
        title: '活动时间',
        width: 100,
        align: 'center',
        scopedSlots: {
          customRender: 'actime'
        }
      },
      {
        title: '状态',
        width: 100,
        dataIndex: 'state',
        align: 'center',
        scopedSlots: {
          customRender: 'state'
        }
      },
      {
        title: '操作',
        align: 'center',
        width: 100,
        scopedSlots: {
          customRender: 'action'
        }
      }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getLuckyDrawList(
          Object.assign(parameter, this.searchData)
        ).then(res => {
          const no = (res.data.pageNo - 1) * res.data.pageSize
          if (res.status == 200) {
            for (let i = 0; i < res.data.list.length; i++) {
              const _item = res.data.list[i]
              _item.no = no + i + 1
            }
            return res.data
          }
        })
      }
    }
  },
  methods: {
    showModal () {
      this.itemId = null
      this.showEditModal = true
    },
    // 编辑
    handleEdit (record) {
      this.itemId = record.id
      this.showEditModal = true
    },
    // 删除
    handleDel (record) {
      this.$confirm({
		  title: '警告',
		  centered: true,
		  content: '删除后无法恢复，确认删除?',
		  okText: '确定',
		  cancelText: '取消',
		  onOk: () => {
		    deleteLuckyDraw({
		      id: record.id
		    }).then(res => {
		      if (res.status == 200) {
		        this.$message.success(res.message)
		        this.$refs.table.refresh()
		      }
		    })
		  }
      })
    },
    // 查看
    handleView (record) {

    },
    // 重置
    resetForm () {
      this.searchData.activeName = ''
      this.searchData.state = ''
      this.$refs.table.refresh(true)
    },
    // 取消添加编辑
    cancel () {
      this.itemId = null
      this.showEditModal = false
      this.resetForm()
    },
    // 更改启用禁用状态
    changeFlagHandle (text, record) {
      const _this = this
      const _data = {
        id: record.id,
        state: record.state == 1 ? '0' : '1'
      }
      enableLuckyDraw(_data).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$refs.table.refresh()
        }
      })
    },
    beforeRouteEnter (to, from, next) {
      next(vm => {
        vm.resetForm()
      })
    }
  }
}
</script>

<style lang="less">
	.lotterySet-page-info{
		.lotterySet-search{
		  .ant-form-inline .ant-form-item{
		    width: 100%;
		  }
		  // 搜索框的下间距
		  .ant-form-item {
		  	margin-bottom: 10px;
		  }
		  .handle-btn{
		    margin-top: 4px;
		  }
		  .serach-btn{
		    margin-right: 10px;
		  }
		}
		.addBtn {
			margin-bottom: 20px;
		}
	}

</style>
