<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible"
    top="4%"
    width="800px"
    class="copy-modal"
  >
    <el-form ref="form" inline :model="copyModel" label-width="110px">
      <el-row>
        <el-col :span="10" :offset="1">
          <el-form-item :label="$t('ec.copyModal.sourceTenant')" prop="sourceTenantId">
            <ec-tenant
              ref="sourceTenantRef"
              v-model="copyModel.sourceTenantId"
              clearable
              filterable
            />
          </el-form-item>
        </el-col>
        <span class="row-span">
          >>
        </span>
        <el-col :span="10" :offset="1">
          <el-form-item
            :label="$t('ec.copyModal.sourceApp')"
            prop="sourceAppId"
            :rules="[
              { required: true, message: $t('ec.copyModal.sourceApp') + $t('ec.table.emptyError'), trigger: 'blur'}
              ]"
          >
            <ec-register-app
              ref="sourceAppRef"
              v-model="copyModel.sourceAppId"
              :tenant-id="copyModel.sourceTenantId"
              :clearable="true"
              filterable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10" :offset="1">
          <el-form-item :label="$t('ec.copyModal.targetTenant')" prop="targetTenantId">
            <ec-tenant
              ref="targetTenantRef"
              v-model="copyModel.targetTenantId"
              clearable
              filterable
            />
          </el-form-item>
        </el-col>
        <span class="row-span">
          >>
        </span>
        <el-col :span="10" :offset="1">
          <el-form-item
            :label="$t('ec.copyModal.targetApp')"
            prop="targetAppId"
            :rules="[
              { required: true, message: $t('ec.copyModal.targetApp') + $t('ec.table.emptyError'), trigger: 'blur'}
              ]"
          >
            <ec-register-app
              ref="targetAppRef"
              v-model="copyModel.targetAppId"
              :tenant-id="copyModel.targetTenantId"
              clearable
              filterable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <p class="center-p" v-html="$t('ec.message.copyModalTips')">
    </p>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('ec.table.close') }}</el-button>
      <el-button type="primary" :loading="loading" @click="save">{{ $t('ec.table.confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>

import { commonCopy } from 'ecip-web/api/vue/components/componentApi'

export default {
  name: 'CopyModalVue',
  data() {
    return {
      visible: false,
      loading: false,
      isFirst: true,
      title: '',
      saveUrl: '',
      copyModel: {
        sourceTenantId: '',
        sourceAppId: '',
        targetTenantId: '',
        targetAppId: ''
      }
    }
  },
  methods: {
    async init(title, saveUrl) {
      this.visible = true
      this.title = title
      this.saveUrl = saveUrl
      if (!this.isFirst) {
        await this.$nextTick()
        this.$refs.sourceTenantRef.refresh()
        this.$refs.sourceAppRef.refresh()
        this.$refs.targetTenantRef.refresh()
        this.$refs.targetAppRef.refresh()
      }
      this.isFirst = false
    },
    async save() {
      let validFlag = false
      this.$refs.form.validate(valid => { validFlag = valid })
      if (!validFlag) {
        this.$notify.warning(this.$t('ec.table.verifyError'))
        return
      }

      this.$confirm(this.$t('ec.message.copyConfirm'), this.$t('ec.message.prompt')).then(async() => {
        try {
          this.loading = true
          await commonCopy(this.saveUrl, this.copyModel)
          // this.visible = false
          this.$notify.success(this.$t('ec.copyModal.copySuccess'))
        } finally {
          this.loading = false
        }
      })

    }
  }
}
</script>

<style scoped lang="scss">
  .center-p{
    margin: 0;
    padding: 0;
    font-size: 12px;
    text-align: center;
    color: #ff887e;
  }
  .copy-modal{
    ::v-deep .el-dialog__body{
       padding-bottom: 0;
    }
    ::v-deep .el-row{
       .row-span{
         position: absolute;
         top: 30%;
         left: 50%;
         transform: translate(-50%,-50%);
         color: #a6a6a6;
       }
    }
  }
</style>
