<!--
 * @Description: 画像主题复制对话框组件
 * @Author: liusheng
 * @Date: 2024-06-12 11:30:56
-->
<template>
	<el-dialog :title="title" :visible.sync="showDialog" width="30%" center
	  :before-close="handleCloseDialog" :close-on-click-modal="false" @open="handleOpenDialog">
    <el-form ref="copyForm" :model="copyForm" :rules="copyRules" label-width="80px">
      <el-form-item label="源key:" prop="srcKey">
        <el-input style="width:300px;" v-model="copyForm.srcKey" disabled></el-input>
      </el-form-item>
      <el-form-item label="目标key:" prop="targetKey">
        <el-input style="width:300px;" v-model="copyForm.targetKey"
          maxlength="30" placeholder="请输入目标key"></el-input>
      </el-form-item>
      <div style="text-align: center;">
        <el-button size="small" @click="handleCloseDialog" >取 消</el-button>&emsp;
        <el-button type="primary" size="small" @click="handleCopy">复 制</el-button>
      </div>
    </el-form>
	</el-dialog>
</template>

<script>
import aupUtil from './AupUtil.js';

export default {
  name: 'CopyTagSetDlg',
	props: ['showDialog', 'type', 'srcKey'],

  data() {
    return {
      copyForm: {
				srcKey: this.srcKey,
				targetKey: ''
			},

      copyRules: {
        srcKey: [{required: true, message: '未指定源Key', trigger: 'blur' }],
        targetKey: [
          { required: true, message: '未指定目标Key', trigger: 'blur' },
          { type: 'string', pattern: aupUtil.keyPattern, message: aupUtil.keyErrMsg, trigger: 'blur' },
					{ min: 1, max: 30, message: '最大长度不超过30个字符', trigger: 'blur' }
        ]
      },

      refreshFlag: false    //指示从对话框返回后是否刷新页面
    }
  },

  computed: {
    title() {
      switch (this.type) {
        case "subject":
          return "复制主题";
        case "gather":
          return "复制采集标签集";
        case "comp":
          return "复制计算型分析标签集";
        case "eval":
          return "复制评估型分析标签集";
        case "manual":
          return "复制人工标签集";
        case "render":
          return "复制展现标签集";
        default:
          return "";
      }
    },
  },

  methods:{
    handleOpenDialog() {
      this.copyForm = {
				srcKey: this.srcKey,
				targetKey: ''
			};
      this.refreshFlag = false;
    },

    handleCopy() {
      this.$refs['copyForm'].validate((valid) => {
        if (!valid) return false;
        
        this.appAxios("POST", this.calcCopyUrl()).then(result => {
          if (result == null) return;
          this.refreshFlag = true;
          this.handleCloseDialog();
        })
      })
    },

    calcCopyUrl() {
      let url = '';
      switch (this.type) {
        case "subject":
          url = this.commonUrl.URL_COPY_AUP_SUBJECT;
          break;
        case "gather":
          url = this.commonUrl.URL_COPY_GATHER_TAG_SET;
          break;
        case "comp":
          url = this.commonUrl.URL_COPY_COMPUTATION_TAG_SET;
          break;
        case "eval":
          url = this.commonUrl.URL_COPY_EVALUATION_TAG_SET;
          break;
        case "manual":
          url = this.commonUrl.URL_COPY_MANUAL_TAG_SET;
          break;
        case "render":
          url = this.commonUrl.URL_COPY_RENDER_TAG_SET;
          break;
      }
      return this.commonUtil.fillUrlArgments(url, this.copyForm.srcKey, this.copyForm.targetKey);
    },

    handleCloseDialog() {
      this.$refs['copyForm'].resetFields();
      this.$emit("close", this.refreshFlag);
    }
  }
}
</script>
