<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="100px"
    >
      <el-form-item
        label="名称"
        prop="keyWords1"
      >
        <el-input v-model="form.keyWords1"></el-input>
      </el-form-item>

      <el-form-item
        label="最小开始时间"
        prop="keyWords2"
      >
        <el-time-picker
          v-model="form.keyWords2"
          placeholder="最小开始时间">
        </el-time-picker>
      </el-form-item>

      <el-form-item
        label="最大开始时间"
        prop="keyWords3"
      >
        <el-time-picker
          v-model="form.keyWords3"
          placeholder="最大开始时间">
        </el-time-picker>
      </el-form-item>

      <!-- <el-form-item
        label="策略类型"
        prop="strategy"
      >
        <el-select v-model="form.strategy">
          <el-option
            label="全部"
            value=""></el-option>

          <el-option
            v-for="{label,value} in strategyMap"
            :key="value"
            :label="label"
            :value="value">
          </el-option>
        </el-select>
      </el-form-item> -->

      <el-form-item
        label="状态"
        prop="keyWords4"
      >
        <el-select v-model="form.keyWords4">
          <el-option
            label="全部"
            value=""
          ></el-option>
          <el-option
            label="激活"
            :value="1"
          ></el-option>
          <el-option
            label="禁用"
            :value="0"
          ></el-option>
        </el-select>
      </el-form-item>

      <div class="btn-wrap">
        <el-button
          type="primary"
          @click="getList"
        >
          查询
        </el-button>
        <el-button
          type="primary"
          @click="$refs.dialog.show(null,'add')"
        >
          新增场次策略
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <div class="block-title">
        场次策略列表
      </div>
      <el-table
        border
        stripe
        v-loading="loading"
        :data="tableData">
        <el-table-column
          prop="name"
          label="名称"
          width="120"
        >
        </el-table-column>

        <el-table-column
          prop="startTime"
          label="开始时间"
          width="100"
        >
        </el-table-column>

        <el-table-column
          prop="duration"
          label="时长"
          width="100"
        >
        </el-table-column>

        <el-table-column
          prop="number"
          label="数量"
          width="80"
        >
        </el-table-column>

        <el-table-column
          prop="strategy"
          label="策略"
          width="80"
        >
          <template slot-scope="scope">
            {{ scope.row.strategy | strategyName }}
          </template>
        </el-table-column>

        <el-table-column
          prop="strategy_content"
          label="策略详情"
        >
          <template slot-scope="scope">
            <template v-if="scope.row.strategy==='PERIOD'">
              {{ formatPeriod(scope.row.strategy_content) }}
            </template>
            <template v-if="scope.row.strategy==='WEEKDAY'">
              {{ formatWeekday(scope.row.strategy_content) }}
            </template>
          </template>
        </el-table-column>

        <el-table-column
          prop="batchConfigTicketTypeMatrix"
          label="绑定的票种"
        >
          <template slot-scope="scope">
            <el-tag
              v-for="(item,index) in scope.row.batchConfigTicketTypeMatrix"
              :key="index">
              {{ item.ticketTypeName }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          prop="status"
          label="状态"
          width="80"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.status===1?'success':'danger'">
              {{ scope.row.status===1?'已激活':'已禁用' }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          width="120"
        >
          <template slot-scope="scope">
            <el-link
              type="primary"
              size="small"
              @click="$refs.dialog.show(scope.row,'edit')"
            >
              编辑
            </el-link>
            <el-link
              type="primary"
              size="small"
              @click="handleDelete(scope.row.id)"
            >
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        :current-page.sync="form.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pageSize"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="getList()"
        :total="total">
      </el-pagination>
    </div>

    <Dialog
      ref="dialog"
      @update="getList"></Dialog>
  </div>
</template>

<script>
import { getBatchConfigList, deleteBatchConfig } from '@/api/batch'
import { strategyMap, weekdayMap } from '@/assets/staticData'
import moment from 'moment'
import Dialog from './Dialog'

export default {
  created () {
    this.getList()
  },
  components: {
    Dialog
  },
  data () {
    return {
      form: {
        keyWords1: '',
        keyWords2: new Date(2020, 1, 1, 0, 0, 0),
        keyWords3: new Date(2020, 1, 1, 23, 59, 59),
        keyWords4: '',
        keyWords5: 'NOSEAT',
        strategy: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 1,
      loading: false,
      tableData: [],
      strategyMap,
      weekdayMap
    }
  },
  filters: {
    strategyName (value) {
      return strategyMap.find(i => i.value === value).label
    }
  },
  methods: {
    handleSizeChange (size) {
      this.form.pageSize = size
      this.getList()
    },
    getList () {
      this.loading = true
      let params = JSON.parse(JSON.stringify(this.form))

      params.keyWords2 = moment(params.keyWords2).format('HH:mm:ss')
      params.keyWords3 = moment(params.keyWords3).format('HH:mm:ss')

      getBatchConfigList(params).then(res => {
        this.tableData = res.list
        this.total = res.total
        // 更新全局场次信息
        this.$store.dispatch('getBatchConfigList')
      }).finally(() => {
        this.loading = false
      })
    },
    formatPeriod (arr) {
      return arr.map(i => {
        return i.start + ' ~ ' + i.end
      }).join('、')
    },
    formatWeekday (arr) {
      return arr.map(i => {
        return this.weekdayMap[i]
      }).join('、')
    },
    handleDelete (id) {
      this.$confirm('确认删除吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBatchConfig(id).then(res => {
          this.$message.success('删除成功')
          this.getList()
        })
      }).catch(() => {})
    }
  },
  mounted () {

  }
}
</script>

<style lang="scss" scoped>

</style>
