<!-- 通用形配置 -->
<template>
  <div>
    <el-form-item label="远程组件">
      <avue-switch v-model="main.activeOption.remote"></avue-switch>
    </el-form-item>
    <template v-if="main.activeOption.remote">
      <el-form-item label="组件地址">
        <avue-input v-model="main.activeOption.src"></avue-input>
      </el-form-item>
      <el-form-item label="组件ID">
        <el-input v-model="main.activeOption.id"
                  placeholder="请选择组件">
          <template #append>
            <el-button type="primary"
                       @click="handleOpen(true)"
                       icon="el-icon-search"></el-button>
          </template>
        </el-input>
      </el-form-item>
    </template>
    <template v-else>
      <el-form-item label="组件库">
        <div>
          <p>
            <a href="https://echarts.apache.org/examples/zh/index.html"
               target="_blank">官方组件库</a>
          </p>
          <p>
            <a href="http://chart.majh.top"
               target="_blank">三方组件库1</a>
          </p>
          <p>
            <a href="http://echarts.zhangmuchen.top"
               target="_blank">三方组件库2</a>
          </p>
          <p>
            <a href="https://madeapie.com"
               target="_blank">三方组件库3</a>
          </p>
          <p>
            <a href="http://ppchart.com"
               target="_blank">三方组件库4</a>
          </p>
        </div>
      </el-form-item>
      <el-form-item label="配置">
        <el-button type="primary"
                   @click="openCode">编辑</el-button>
        <el-button type="primary"
                   @click="handleOpen()"
                   icon="el-icon-search"></el-button>
      </el-form-item>
    </template>
    <el-form-item label-width="0"
                  v-if="!main.activeOption.remote">
      <avue-highlight :height="500"
                      v-model="main.activeObj.echartFormatter"></avue-highlight>
    </el-form-item>
    <el-form-item label-width="0"
                  v-else>
      <avue-highlight :height="500"
                      v-model="main.activeComponent.remoteContent"></avue-highlight>
    </el-form-item>
    <codeedit @submit="codeClose"
              title="Echart配置"
              codeType="echart"
              v-model="code.obj"
              v-if="code.box"
              :type="code.type"
              v-model:visible="code.box">
    </codeedit>
    <el-dialog title="组件库"
               v-model="api.box"
               class="avue-dialog"
               width="70%">
      <component-list v-if="api.box"
                      :activeName="1"
                      @change="handleApiChange"
                      :menu="false"></component-list>
    </el-dialog>
  </div>
</template>

<script>
import codeedit from '../../page/group/code.vue';
import componentList from '../../page/list/components.vue'
import { getObj } from '@/api/components'
export default {
  name: 'common',
  inject: ["main"],
  data () {
    return {
      api: {
        isId: false,
        box: false,
        item: {}
      },
      code: {
        type: 'echartFormatter',
        box: false,
        obj: {},
      }
    }
  },
  components: {
    componentList,
    codeedit
  },
  methods: {
    handleOpen (isId) {
      this.api.isId = isId
      this.api.box = true
    },
    handleApiChange (val) {
      this.api.item = val
      getObj(this.api.item.id).then(res => {
        const data = res.data.data;
        if (this.api.isId) {
          this.main.activeOption.id = data.id
        } else {
          this.main.activeObj[this.code.type] = data.content
        }
        this.api.box = false;
        this.api.item = {}
        this.$message.success('导入成功')
      })
    },
    codeClose (value) {
      this.main.activeObj[this.code.type] = value;
    },
    openCode () {
      this.code.obj = this.main.activeObj[this.code.type];
      this.code.box = true;
    },
  }
}
</script>

<style>
</style>