<template>
  <el-dialog
    title="导入资产库"
    :visible.sync="dialogVisible"
    :close-on-click-modal=false
    :destroy-on-close=true
    width="450px"
    :before-close="handleClose"
    @closed="restoreParam" class="workspace-import">
    <div v-show="step1" id="step1" style="height: 250px;text-align: center;">
      <div style="position: relative;top:35%;">
        <div style="margin-bottom: 25px;">
          <awsui-button style="width: 130px;" :class="{'button-general-color': clickUploadButton == 'local', 'button-general-color-reverse': clickUploadButton == 'server'}" :type="clickUploadButton == 'local' ? 'primary' : ''" @click="localImport(true, true)">本地文件上传</awsui-button>
        </div>
        <div>
          <awsui-button style="width: 130px;" :class="{'button-general-color': clickUploadButton == 'server', 'button-general-color-reverse': clickUploadButton == 'local'}" :type="clickUploadButton == 'server' ? 'primary' : ''" @click="serverImport(true, true)">服务器文件导入</awsui-button>
        </div>
      </div>
    </div>
    <div v-show="step2" id="step2" style="height: 250px;" v-loading="loading">
      <div style="text-align: center;padding-top: 30px;">
        <awsui-button style="width: 130px;" :class="{'button-general-color': clickUploadButton == 'local', 'button-general-color-reverse': clickUploadButton == 'server'}" :type="clickUploadButton == 'local' ? 'primary' : ''" @click="localImport(false, true)">本地文件上传</awsui-button>
        <awsui-button style="width: 130px;" :class="{'button-general-color': clickUploadButton == 'server', 'button-general-color-reverse': clickUploadButton == 'local'}" :type="clickUploadButton == 'server' ? 'primary' : ''" @click="serverImport(true, true)">服务器文件导入</awsui-button>
      </div>
      <div v-show="source == 'local'" style="height:100px;margin: 10px 10px;">
        <PALUpload ref="palUpload" style="width: 100%;"
                   class="upload-demo"
                   appId="com.actionsoft.apps.coe.pal"
                   repositoryName="tmp"
                   groupValue="Normal"
                   fileValue="Normal"
                   :on-preview="handlePreview"
                   :on-success="handleSuccess"
                   :on-remove="handleRemove"
                   :on-error="handleError"
                   :before-remove="beforeRemove"
                   :before-upload="beforeUpload"
                   :limit="1"
                   :on-exceed="handleExceed"
                   accept=".bak"
                   :file-list="fileList">
          <div style="display: none;">
            <awsui-button id="selectFileButton" style="width: 130px;" class="button-general-color" type="primary">本地文件上传</awsui-button>
          </div>
        </PALUpload>
      </div>
      <div v-show="source == 'remote'" style="height:70px;margin: 40px 10px 0px 10px;">
        <el-select style="width: 100%" v-model="serverValue" placeholder="请选择" size="mini" no-data-text="无资产库文件" @change="changeServerValue">
          <el-option
            v-for="item in serverOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </div>
      <div v-show="wsIsExist" style="margin: 10px;">
        <el-form>
          <el-form-item label="已存在资产库处理方法" prop="resource">
            <el-radio-group v-model="replaceType">
              <el-radio label="replace">替换</el-radio>
              <el-radio label="skip">跳过</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <awsui-button class="button-general-color" :disabled="buttonDisabled" type="primary" @click="save">确定</awsui-button>
      <awsui-button @click="cancel()">取消</awsui-button>
    </span>
  </el-dialog>
</template>

<script>
  import PALUpload from "@/components/common/upload/index.vue";
  import awsuiAxios from "../../awsuiAxios";
    export default {
      name: "WorkspaceImport",
      components: {PALUpload},
      data() {
        return {
          buttonDisabled : false,
          dialogVisible : false,
          clickUploadButton: 'local',
          step1 : true,
          step2 : false,
          source : '',
          wsIsExist : false,
          fileList: [],
          serverOptions: [],
          serverValue: '',
          replaceType: 'replace',
          wsFileName: '',
          loading: false,
          obj: null //导入成功后回调的vue页面
        }
      },
      methods: {
        openImportWsDlg(obj) {
          this.dialogVisible = true;
          this.obj = obj;
        },
        handleClose(done) {
          done();
        },
        restoreParam() {// 关闭后数据初始化
          this.buttonDisabled = false;
          this.loading = false;
          this.dialogVisible = false;
          this.step1 = true;
          this.step2 = false;
          this.source = '';
          this.wsIsExist = false;
          this.fileList = [];
          this.serverOptions = [];
          this.serverValue = '';
          this.replaceType = 'replace';
          this.wsFileName = '';
        },
        cancel() {
          this.dialogVisible = false;
        },
        changeServerValue() {
          const that = this;
          that.wsIsExist = false;
          that.replaceType = 'replace';
          that.wsFileName = that.serverValue;
          that.loading=true;
          that.buttonDisabled = true;

          const data = {
            url:'jd',
            data:{
              cmd : 'com.actionsoft.apps.coe.pal_ws_is_exist',
              wsFileName : that.wsFileName,
              source : that.source
            }
          };
          awsuiAxios.post(data).then(function (ro) {
            if (ro.result == 'ok') {
              if (ro.data.message == "exist") {
                that.replaceType = 'replace';
                that.wsIsExist = true;
              } else {
                that.replaceType = 'replace';
                that.wsIsExist = false;
              }
            } else {
              that.$message.error(ro.msg);
            }
            that.loading = false;
            that.buttonDisabled = false;
          }).catch(error=>{
            console.log(error);
          })
        },
        localImport(changeStep2, openFileSelect) {
          this.clickUploadButton = 'local';
          if (openFileSelect) {// 打开文件选择
            this.$refs.palUpload.clearFiles();
            this.source = 'local';
            document.getElementById("selectFileButton").click();
          }
          if (changeStep2) {
            this.step1 = false;
            this.step2 = true;
          }
          this.wsIsExist = false;
          this.replaceType = 'replace';
          this.wsFileName = '';
        },
        serverImport(changeStep2) {
          this.clickUploadButton = 'server';
          // 查询服务器中是否存在需要导入的资产库文件
          const that = this;
          const data = {
            url:'jd',
            data:{
              cmd : 'com.actionsoft.apps.coe.pal_ws_remote_import_query'
            }
          };
          awsuiAxios.post(data).then(function (ro) {
            if (ro.result == 'ok') {
              that.serverOptions = ro.data.impRepsitorys;
            } else {
              that.$message.error(ro.msg);
            }
          }).catch(error=>{
            console.log(error);
          })
          if (changeStep2) {
            that.step1 = false;
            that.step2 = true;
          }
          // 获取服务器中已经上传好的资产库文件
          that.serverOptions = [];
          that.serverValue = '';
          that.source = 'remote';
          that.wsIsExist = false;
          that.replaceType = 'replace';
          that.wsFileName = '';
        },
        save() {
          const that = this;
          if (that.wsFileName == '') {
            that.$message({message: '请'+ (that.clickUploadButton == 'local' ? '上传' : '选择') + '需要导入的文件', type: 'warning'});
            return;
          }
          that.buttonDisabled = true;
          that.loading = true;
          const data = {
            url:'jd',
            data:{
              cmd : 'com.actionsoft.apps.coe.pal_ws_save_import',
              wsFileName : that.wsFileName,
              replaceChoice : that.replaceType,
              source : that.source
            }
          };
          awsuiAxios.post(data).then(function (ro) {
            if (ro.result == 'ok') {
              console.log("导入资产库[" + ro.data.wsName + "][" + ro.data.wsId + "]成功")
              that.$message({
                message: ro.msg,
                duration: 2000,
                type: 'success'
              });
              that.cancel();
              if (that.obj != null) {
                that.obj.importWorkspaceCallback();
              }
            } else {
              that.loading = false;
              that.buttonDisabled = false;
              that.$message.error(ro.msg);
            }
          }).catch(error=>{
            console.log(error);
          })
        },
        handleRemove(file, fileList) {
          console.log(file, fileList);
        },
        handlePreview(file) {
          console.log(file);
        },

        handleExceed(files, fileList) {

        },
        handleError(err, file, fileList) {
        },
        beforeUpload(file) {
          // 上传之前校验判断大小
          if (file.size / 1024 / 1024 > 2048) {  //文件大于2G
            this.$message.warning('文件过大，请联系管理员将需上传的文件放在在服务器/doccenter/com.actionsoft.apps.coe.pal/tmp/imp/repository目录下使用服务器上传方式上传');
            return false;
          }
        },
        handleSuccess(response, file, fileList) {
          const that = this;
          that.buttonDisabled = true;
          that.loading = true;
          that.wsFileName = file.name;

          const data = {
            url:'jd',
            data:{
              cmd : 'com.actionsoft.apps.coe.pal_ws_is_exist',
              wsFileName : that.wsFileName,
              source : that.source
            }
          };
          awsuiAxios.post(data).then(function (ro) {
            if (ro.result == 'ok') {
              if (ro.data.message == "exist") {
                that.replaceType = 'replace';
                that.wsIsExist = true;
              } else {
                that.replaceType = 'replace';
                that.wsIsExist = false;
              }
            } else {
              that.$message.error(ro.msg);
            }
            that.buttonDisabled = false;
            that.loading = false;
          }).catch(error=>{
            console.log(error);
          })
        },
        beforeRemove(file, fileList) {
          if (file.status == 'success') {
            // 删除之前，进行服务器文件删除
          }
          this.wsFileName = '';
          this.wsIsExist = false;
        },
      }
    }
</script>

<style scoped>
  .workspace-import >>> .el-dialog__body {
    padding: 10px 20px;
    color: #606266;
    font-size: 14px;
    word-break: break-all;
  }
</style>
