<template>
  <el-dialog
    title="编辑模板"
    v-if="dialogVisible"
    :visible.sync="dialogVisible"
    width="65%"
    appendToBody
    :close-on-click-modal="false"
    top="8vh"
    :before-close="handleClose">
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="pc端模版" name="pc"></el-tab-pane>
      <el-tab-pane label="移动端模版" name="mobile"></el-tab-pane>
    </el-tabs>
      <el-form v-model="dataTemplate" data-vv-scope="editTemplateHtmlForm">
          <ht-form-item label="报表别名" prop="alias" label-width="180px">
            <ht-input
              v-model="dataTemplate.alias"
              disabled
              :validate="{required:true}"
            ></ht-input>
          </ht-form-item>
          <ht-form-item label="报表模板" prop="name" label-width="180px" v-if="dialogVisible">
            <codemirror
             v-show="activeName ==='pc'"
              ref="template"
              :value="dataTemplate.templateHtml"
              :options="cmOptions"
              class="code">
            </codemirror>
            <codemirror
              v-show="activeName ==='mobile'"
              ref="mobileTemplate"
              :value="dataTemplate.mobileTemplateHtml"
              :options="cmOptions"
              class="code">
            </codemirror>
          </ht-form-item>
      </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="onConfirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import form from "@/api/form.js";
import { codemirror } from 'vue-codemirror';
import "codemirror/theme/ambiance.css";
require("codemirror/mode/javascript/javascript"); 

export default {
  name: "template-html-edit",
  props: ["data"],
  components: { codemirror },
  data() {
    return {
      activeName:'pc',
      dataTemplate: {},
      dialogVisible: false,
      cmOptions:{
        value: '',
        mode: 'vue',
        readOnly: false,
        smartIndent: true,
        tabSize: 2,
        theme: 'base16-light',
        lineNumbers: true,
        line: true,
        lineWrapping: true,
        autoRefresh: true
        
      },
    };
  },
  computed: {
    codemirror() {
      return this.$refs.template.codemirror;
    },
    mobileCodemirror(){
      return this.$refs.mobileTemplate.codemirror
    }
  },
  watch:{
    data:{
      handler(newVal, oldVal) {
        if ( newVal != oldVal) {
          this.dataTemplate = newVal;
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    onConfirm(selection) {
       this.dataTemplate.templateHtml = this.codemirror.getValue();
       this.dataTemplate.mobileTemplateHtml = this.mobileCodemirror.getValue()
       form.saveTemplateHtml(this.dataTemplate).then(response => {
          if(response.state){
            this.dialogVisible = false;
            this.$message.success(response.message);
          }else{
            this.$message.error(response.message);
          }
      });
    },
    handleClose(){
      this.dialogVisible = false;
    },
    showDialog(){
      this.dialogVisible = true;
    },
    handleClick(){

    }
  },
};
</script>
<style scoped>

</style>
