<template>
  <m-popup
          ref="popup"
          :ok-text="item ? $t('Edit') : $t('Submit')"
          :nameText="item ? $t('Edit worker group') : $t('Create worker group')"
          @ok="_ok">
    <template slot="content">
      <div class="create-worker-model">
        <m-list-box-f>
          <template slot="name"><b>*</b>{{$t('Group Name')}}</template>
          <template slot="content">
            <x-input
                    type="input"
                    v-model="name"
                    :placeholder="$t('Please enter group name')">
            </x-input>
          </template>
        </m-list-box-f>
        <m-list-box-f>
          <template slot="name">IP</template>
          <template slot="content">
            <x-input
                    :autosize="{ minRows: 4, maxRows: 6 }"
                    type="textarea"
                    v-model="ipList"
                    :placeholder="$t('Please enter the IP address separated by commas')">
            </x-input>
            <div class="ipt-tip">
              <span>{{$t('Note: Multiple IP addresses have been comma separated')}}</span>
            </div>
          </template>
        </m-list-box-f>
      </div>
    </template>
  </m-popup>
</template>
<script>
  import i18n from '@/module/i18n'
  import store from '@/conf/home/store'
  import mPopup from '@/module/components/popup/popup'
  import mListBoxF from '@/module/components/listBoxF/listBoxF'

  export default {
    name: 'create-warning',
    data () {
      return {
        store,
        id: 0,
        name: '',
        ipList: ''
      }
    },
    props: {
      item: Object
    },
    methods: {
      _ok () {
        if (this._verification()) {
          // Verify username
          this._submit()
        }
      },
      _verification () {
        // group name
        if (!this.name) {
          this.$message.warning(`${i18n.$t('Please enter group name')}`)
          return false
        }
        if (!this.ipList) {
          this.$message.warning(`IP地址不能为空`)
          return false
        }
        return true
      },
      _submit () {
        let param = {
          id: this.id,
          name: this.name,
          ipList: this.ipList
        }
        if (this.item) {
          param.id = this.item.id
        }
        this.$refs['popup'].spinnerLoading = true
        this.store.dispatch(`security/saveWorkerGroups`, param).then(res => {
          this.$emit('onUpdate')
          this.$message.success(res.msg)
          setTimeout(() => {
            this.$refs['popup'].spinnerLoading = false
          }, 800)
        }).catch(e => {
          this.$message.error(e.msg || '')
          this.$refs['popup'].spinnerLoading = false
        })
      }
    },
    watch: {},
    created () {
      if (this.item) {
        this.id = this.item.id
        this.name = this.item.name
        this.ipList = this.item.ipList
      }
    },
    mounted () {
    },
    components: { mPopup, mListBoxF }
  }
</script>
<style lang="scss" rel="stylesheet/scss">
  .create-worker-model {
    .ipt-tip {
      color: #999;
      padding-top: 4px;
      display: block;
    }
  }
</style>