<template>
  <m-popup
          ref="popup"
          :ok-text="item ? $t('Edit') : $t('Submit')"
          :nameText="item ? $t('Edit queue') : $t('Create queue')"
          @ok="_ok">
    <template slot="content">
      <div class="create-tenement-model">
        <m-list-box-f>
          <template slot="name"><b>*</b>{{$t('Name')}}</template>
          <template slot="content">
            <x-input
                    type="input"
                    v-model="queueName"
                    :placeholder="$t('Please enter name')"
                    autocomplete="off">
            </x-input>
          </template>
        </m-list-box-f>
        <m-list-box-f>
          <template slot="name"><b>*</b>{{$t('Queue value')}}</template>
          <template slot="content">
            <x-input
                    type="input"
                    v-model="queue"
                    :placeholder="$t('Please enter queue value')"
                    autocomplete="off">
            </x-input>
          </template>
        </m-list-box-f>

      </div>
    </template>
  </m-popup>
</template>
<script>
  import _ from 'lodash'
  import store from '@/conf/home/store'
  import mPopup from '@/module/components/popup/popup'
  import mListBoxF from '@/module/components/listBoxF/listBoxF'

  export default {
    name: 'create-tenement',
    data () {
      return {
        store,
        queue: '',
        queueName: ''
      }
    },
    props: {
      item: Object
    },
    methods: {
      _ok () {
        if (!this._verification()) {
          return
        }

        let param = {
          queue: _.trim(this.queue),
          queueName: _.trim(this.queueName)
        }
        // edit
        if (this.item) {
          param.id = this.item.id
        }

        let $then = (res) => {
          this.$emit('onUpdate')
          this.$message.success(res.msg)
          setTimeout(() => {
            this.$refs['popup'].spinnerLoading = false
          }, 800)
        }

        let $catch = (e) => {
          this.$message.error(e.msg || '')
          this.$refs['popup'].spinnerLoading = false
        }

        if (this.item) {
          this.$refs['popup'].spinnerLoading = true
          this.store.dispatch(`security/updateQueueQ`, param).then(res => {
            $then(res)
          }).catch(e => {
            $catch(e)
          })
        } else {
          this._verifyName(param).then(() => {
            this.$refs['popup'].spinnerLoading = true
            this.store.dispatch(`security/createQueueQ`, param).then(res => {
              $then(res)
            }).catch(e => {
              $catch(e)
            })
          }).catch(e => {
            this.$message.error(e.msg || '')
          })
        }
      },
      _verification () {
        if (!this.queueName) {
          this.$message.warning(`Please enter name`)
          return false
        }
        if (!this.queue) {
          this.$message.warning(`请输入队列值`)
          return false
        }
        return true
      },
      _verifyName (param) {
        return new Promise((resolve, reject) => {
          this.store.dispatch(`security/verifyQueueQ`, param).then(res => {
            resolve()
          }).catch(e => {
            reject(e)
          })
        })
      }
    },
    watch: {
    },
    created () {
      if (this.item) {
        this.queueName = this.item.queueName
        this.queue = this.item.queue
      }
    },
    mounted () {

    },
    components: { mPopup, mListBoxF }
  }
</script>