<template>
  <div class="save-semantic-dialog">
    <el-dialog 
      :title="saveType===0?'另存为':'保存'"
      :visible.sync="isSaveFileShow"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      @close="closeDialog"
      v-dialogDrag
      width="40%" center
    >
      <div class="dialog-container dialog-loading-target">
        <template v-if="step===1">
          <el-form
            ref="formRef"
            label-position="left"
            hide-required-asterisk
            label-width="160px"
            :model="form"
          >
            <el-form-item label="语义分析名称：" prop="Title">
              <el-input
                v-model="form.Title"
                style="width: 240px"
                placeholder="请输入语义分析名称"
              />
            </el-form-item>
            <el-form-item label="所属分类：" prop="ClassifyId">
              <el-select placeholder="请选择所属分类" v-model="form.ClassifyId">
                <el-option 
                  v-for="item in classifyList" :key="item.SaCompareClassifyId"
                  :label="item.ClassifyName"
                  :value="item.SaCompareClassifyId"
                  />
              </el-select>
            </el-form-item>
            <el-form-item label="是否预览结果：">
              <el-radio-group v-model="previewPic">
                <el-radio label="是"></el-radio>
                <el-radio label="否"></el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </template>
        <template v-else>
          <div class="form-item">
            是否上传结果图片：
            <el-radio-group v-model="uploadPic">
                <el-radio label="是"></el-radio>
                <el-radio label="否"></el-radio>
            </el-radio-group>
          </div>
          <div class="preview-wrap">
            <canvas-table 
              ref="canvasTable"
              :tableImgData="resultData"
            />
          </div>
        </template>
      </div>
      <div class="foot-container">
          <el-button type="primary" @click="toNextStep">确定</el-button>
          <el-button @click.stop="closeDialog">取 消</el-button>
      </div>     
    </el-dialog>
  </div>
</template>

<script>
import canvasTable from '../../components/canvasTable.vue';
import {semanticInterface} from '@/api/modules/semanticsApi.js';
export default {
  components: { canvasTable },
  props:{
    isSaveFileShow:{
      type:Boolean,
      default:false
    },
    semanticInfo:{//当前语义分析的一些信息
      type:Object,
      default:()=>{
        return {}
      }
    },
    saveType:{//是点的保存还是另存为
      type:Number,
      default:0 //0另存为 1为保存
    },
    resultData:{//保存结果
      type:Object,
      default:()=>{
        return {}
      }
    },
    dialogLoading:{//loading
      type:Boolean,
      default:false
    }
  },
  watch:{
    isSaveFileShow(val){
      if(val){
        this.step = 1
        this.getClassifyList()
        this.form = _.cloneDeep(this.semanticInfo)
        this.form.previewPic = this.previewPic?this.previewPic:'是'
        this.form.uploadPic = this.uploadPic?this.uploadPic:'是'
        if(this.saveType===0){
            this.form.Title = this.form.Title + '（1）'
        }
      }
    },
    dialogLoading(val){
      //console.log('dialogVal',val)
      if(val){
        this.loading = this.$loading({
          lock: true,
          text: '正在生成结果图片...',
          spinner: 'el-icon-loading',
          customClass:'loading',
          target:document.querySelector('.dialog-loading-target'),
          background: 'rgba(255, 255, 255, 0.8)'
        });
      }else{
        this.loading&&this.loading.close()
      }
    },
    previewPic(val){
        this.form.previewPic = val
    },
    uploadPic(val){
        this.form.uploadPic = val
    }
  },
  data() {
    return {
      step:1,//步骤数 1为保存/另存为图片 2为上传结果图
      classifyList:[],//语义分析分类列表
      previewPic:'是',
      uploadPic:'是',
      canvasTableData:[],
      loading:null,
      form:{}
    };
  },
  methods: {
    getClassifyList(){
      semanticInterface.getClassifyList().then(res=>{
        if(res.Ret!==200) return 
        this.classifyList = res.Data||[]
      })
    },
    async toNextStep(){
      if(this.step===1){
        if(!this.checkForm()) return
        await this.saveSemantic()
        return
      }
      if(this.step===2){
        this.uploadPic==='是'&&await this.uploadImg()
        this.uploadPic==='否'&&this.closeDialog('toList')
        return
      }
    },
    goNext(){
        if(this.step===1){
            this.previewPic==='是'
            ?this.step++
            :this.closeDialog('toList')
        }
    },
    checkForm(){
      const {Title,ClassifyId} = this.form
      if(!Title){
        this.$message.warning('请输入语义分析名称')
        return false
      }
      if(!ClassifyId){
        this.$message.warning('请选择所属分类')
        return false
      }
      return true
    },
    saveSemantic(){
     this.$emit('saveSemantic',this.form)
    },
    async uploadImg(){
      let imgData = await this.$refs.canvasTable.tableToCanvas()
      this.$emit('uploadImg',imgData)
    },
    closeDialog(type){
      //console.log('dialog	Type',type)
      this.$emit('close',type)
    }
  },
};
</script>

<style scoped lang="scss">
.save-semantic-dialog{
  .preview-wrap{
    margin-top:20px;
  }
  .foot-container{
    margin-top:15px;
    text-align: center;
    padding-bottom: 40px;
  }
}
</style>
