<template>
  <div class="push-form">
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item prop="sysOrigin" :label="$t('pages.giftManagement.cp.form.system')">
        <el-select
          v-model="form.sysOrigin"
          :placeholder="$t('pages.giftManagement.cp.placeholder.system')"
          style="width: 120px"
          class="filter-item"
          @change="handleSearch"
        >
          <el-option
            v-for="(item, index) in permissionsSysOriginPlatforms"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
            <span style="float: left;">
              <sys-origin-icon
                :icon="item.value"
                :desc="item.value"
              /></span>
            <span style="float: left;margin-left:10px">{{ item.label }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="giftId" :label="$t('pages.giftManagement.cp.form.giftId')">
        <el-input v-model="form.giftId" type="number" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{ $t('pages.giftManagement.cp.action.save') }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getPairCpGiveGiftId, pushPairCpGiveGiftId } from '@/api/gift'
import { mapGetters } from 'vuex'
export default {
  name: 'CP',
  data() {
    return {
      form: {
        giftId: '',
        sysOrigin: ''
      }
    }
  },
  computed: {
    ...mapGetters(['permissionsSysOriginPlatforms'])
  },
  created() {
    const that = this
    const querySystem = this.permissionsSysOriginPlatforms[0]
    if (!querySystem) {
      return
    }
    that.form.sysOrigin = querySystem.value
    that.renderData()
  },
  methods: {
    renderData() {
      const that = this
      that.listLoading = true
      getPairCpGiveGiftId(that.form.sysOrigin).then(res => {
        const { body } = res
        that.form.giftId = body
        that.listLoading = false
      })
    },
    handleSearch() {
      this.renderData()
    },
    onSubmit() {
      const that = this
      that.$refs.form.validate((valid) => {
        if (!valid) {
          console.error('error submit!!')
          return false
        }
        if (that.form.sysOrigin === '') {
          that.$opsMessage.fail(this.$t('pages.giftManagement.cp.message.selectSystem'))
          return
        }
        if (that.form.giftId === '') {
          that.$opsMessage.fail(this.$t('pages.giftManagement.cp.message.enterGiftId'))
          return
        }
        that.$confirm(this.$t('pages.giftManagement.cp.message.confirmUpdate'), this.$t('common.tip'), {
          confirmButtonText: this.$t('common.confirm'),
          cancelButtonText: this.$t('common.cancel'),
          type: 'warning'
        }).then(() => {
          pushPairCpGiveGiftId(that.form.giftId, that.form.sysOrigin).then(res => {
            that.$message({
              type: 'success',
              message: this.$t('pages.giftManagement.cp.message.success')
            })
            that.renderData()
          }).catch(er => {
            // that.$opsMessage.success()
          })
        }).catch(() => {
          that.$message({
            type: 'info',
            message: this.$t('pages.giftManagement.cp.message.cancelled')
          })
        })
      })
    }
  }
}
</script>
<style scoped lang="scss">
  .msg-text {
    color: #54c988;
    background-color: #f6f5f5;
  }
</style>
