<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="640px"
    @close="handleClose">
    <el-form
      inline
      :model="form"
      ref="form"
      class="form-wrap"
      label-width="100px">
      <div class="dialog-info">
        <el-form-item
          verify
          label="数量"
          prop="number">
          <el-input-number
            v-model="form.number"
            placeholder="数量"></el-input-number>
        </el-form-item>

        <el-form-item
          verify
          label="状态"
          prop="status">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="激活"
            inactive-text="禁用">
          </el-switch>
        </el-form-item>
        <el-form-item
          label="班次"
          v-if="form.batchConfig && form.batchConfig.is_schedule"
          prop="scheduleConfigIds">
          <el-select
            v-model="form.scheduleConfigIds"
            multiple
            placeholder="请选择">
            <el-option
              v-for="item in allScheduleList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </div>
      <div
        class="table-box"
        v-if="form.batchConfig && form.batchConfig.is_schedule">
        <el-table
          border
          stripe
          :data="form.scheduleList">
          <el-table-column
            prop="schedule_name"
            label="班次">
          </el-table-column>
          <el-table-column
            label="库存"
            min-width="90">
            <template slot-scope="scope">
              <el-input-number
                controls-position="right"
                :min="0"
                :precision="0"
                v-model="scope.row.number"
              ></el-input-number>
            </template>
          </el-table-column>
          <el-table-column
            class-name="align-item"
            label="状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="激活"
                inactive-text="禁用">
              </el-switch>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">
        取 消
      </el-button>
      <el-button
        type="primary"
        @click="handleSubmit">
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateBatch } from '@/api/batch'

export default {
  props: {
    allScheduleList: {
      type: Array,
      default: () => []
    }
  },

  computed: {
    ticketTypeList () {
      return this.$store.state.app.ticketTypeList
    }
  },
  data () {
    return {
      title: '编辑场次',
      visible: false,
      form: {
        status: 1,
        number: 0,
        scheduleConfigIds: [],
        scheduleList: []
      }
    }
  },
  watch: {
    'form.scheduleConfigIds': {
      handler: function (newVal, oldVal) {
        // 根据班次选择增加or删除 班次列表
        if (newVal.length > oldVal.length) {
          const addScheduleId = newVal[newVal.length - 1]
          const addSchedule = this.allScheduleList.find(i => i.id === addScheduleId)
          addSchedule.schedule_config_id = addSchedule.id
          addSchedule.schedule_name = addSchedule.name
          if (newVal.length !== this.form.scheduleList.length) {
            this.form.scheduleList.push(addSchedule)
          }
        } else {
          const removeScheduleId = oldVal.find(i => !newVal.includes(i))
          if (this.form.scheduleList) {
            this.form.scheduleList = this.form.scheduleList.filter(i => i.schedule_config_id !== removeScheduleId)
          }
        }
      }
    }
  },
  methods: {
    show (data) {
      this.visible = true
      this.$nextTick(() => {
        this.reset()

        this.form = Object.assign({}, this.form, JSON.parse(JSON.stringify(data)))
        this.form.batch_config_id = this.form.batchConfig.id
        this.form.scheduleList = this.form.scheduleList || []
        this.form.scheduleConfigIds = this.form.scheduleList ? this.form.scheduleList.map(i => { return i.schedule_config_id }) : []
        this.title = '【' + this.form.batch_date + '】 ' + this.form.batchConfig.name
      })
    },
    reset () {
      this.$refs.form.resetFields()
    },
    handleClose () {
      this.visible = false
    },

    handleSubmit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.form))
          updateBatch(params).then(res => {
            this.$message.success('操作成功。')
            this.handleClose()
            this.$emit('update')
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
