<template>
  <a-modal
    :visible="visible"
    title="绑定景点"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="loading">
      <a-form>
        <a-form-item label="景点列表">
          <a-checkbox-group v-model="selectedTerm">
            <a-checkbox
              v-for="(term,index) in termList"
              :key="index"
              :value="term.id"
            >
              {{ term.name }}
            </a-checkbox>
          </a-checkbox-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getTermScenics, setTermScenics } from '@/api/saleChannel/term'
import { getScenic } from '@/api/ticketSetting/scenic'

export default {
  data () {
    return {
      visible: false,
      loading: false,
      termList: [],
      selectedTerm: []
    }
  },
  created () {
    getScenic({ pageNum: 1, pageSize: 0, status: ['active', 'inactive'] }).then(res => {
      const getSenicList = res.extraInfo.list.filter(e => e.id !== 9999)
      this.termList = getSenicList
    })
  },
  methods: {
    show (scenic) {
      this.visible = true
      this.id = scenic.id
      this.selectedTerm = []
      this.getTerms()
    },
    handleOk () {
      this.loading = true

      setTermScenics(this.id, this.selectedTerm).then(res => {
        this.$message.success('设置成功')
        this.handleCancel()
      }).finally(() => {
        this.loading = false
      })
    },
    handleCancel () {
      this.visible = false
    },
    getTerms () {
      this.loading = true

      getTermScenics(this.id).then(res => {
        this.selectedTerm = res.extraInfo.list.map(i => i.id)
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.ant-checkbox-wrapper{
    display: block; margin-left: 0; margin-bottom: 5px;
}
</style>
