<template>
  <div class="fm-style">
    <el-container class="fm2-container">
      <el-main class="fm2-main">
        <el-container>
          <el-aside width="350px">
            <div class="components-list">
              <template v-if="basicFields.length">
                <!-- 基础字段 -->
                <div class="widget-cate">
                  {{ $t("fm.components.basic.title") }}
                </div>
                <draggable
                  tag="ul"
                  :list="basicComponents"
                  v-bind="{
                    group: { name: 'people', pull: 'clone', put: false },
                    sort: false,
                    ghostClass: 'ghost',
                  }"
                  @end="handleMoveEnd"
                  @start="handleMoveStart"
                  :move="handleMove"
                >
                  <template v-for="(item, index) in basicComponents">
                    <li
                      v-if="basicFields.indexOf(item.type) >= 0"
                      class="form-edit-widget-label"
                      :class="{ 'no-put': item.type == 'divider' }"
                      :key="index"
                    >
                      <a>
                        <i class="icon iconfont" :class="item.icon"></i>
                        <span>{{ item.name }}</span>
                      </a>
                    </li>
                  </template>
                </draggable>
              </template>
              <template v-if="evidenceSheetFields.length">
                <template v-if="flowType == '1'">
                  <!-- 取证单字段 -->
                  <div class="widget-cate">
                    {{ $t("fm.components.evidenceSheet.title") }}
                  </div>
                  <draggable
                    tag="ul"
                    :list="evidenceSheetComponents"
                    v-bind="{
                      group: { name: 'people', pull: 'clone', put: false },
                      sort: false,
                      ghostClass: 'ghost',
                    }"
                    @end="handleMoveEnd"
                    @start="handleMoveStart"
                    :move="handleMove"
                  >
                    <template v-for="(item, index) in evidenceSheetComponents">
                      <li
                        v-if="evidenceSheetFields.indexOf(item.type) >= 0"
                        class="form-edit-widget-label"
                        :class="{ 'no-put': item.type == 'divider' }"
                        :key="index"
                      >
                        <a>
                          <i class="icon iconfont" :class="item.icon"></i>
                          <span>{{ item.name }}</span>
                        </a>
                      </li>
                    </template>
                  </draggable>
                </template>
              </template>
              <!-- 收文 -->
              <template v-if="evidenceSheetReceiptFields.length">
                <template v-if="flowType == '2'">
                  <!-- 取证单收文字段 -->
                  <div class="widget-cate">
                    {{ $t("fm.components.evidenceSheetReceipt.title") }}
                  </div>
                  <draggable
                    tag="ul"
                    :list="evidenceSheetReceiptComponents"
                    v-bind="{
                      group: { name: 'people', pull: 'clone', put: false },
                      sort: false,
                      ghostClass: 'ghost',
                    }"
                    @end="handleMoveEnd"
                    @start="handleMoveStart"
                    :move="handleMove"
                  >
                    <template
                      v-for="(item, index) in evidenceSheetReceiptComponents"
                    >
                      <li
                        v-if="
                          evidenceSheetReceiptFields.indexOf(item.type) >= 0
                        "
                        class="form-edit-widget-label"
                        :class="{ 'no-put': item.type == 'divider' }"
                        :key="index"
                      >
                        <a>
                          <i class="icon iconfont" :class="item.icon"></i>
                          <span>{{ item.name }}</span>
                        </a>
                      </li>
                    </template>
                  </draggable>
                </template>
              </template>
              <!-- 回复 -->
              <template v-if="evidenceSheetReplyFields.length">
                <template v-if="flowType == '3'">
                  <!-- 取证单回复字段 -->
                  <div class="widget-cate">
                    {{ $t("fm.components.evidenceSheetReply.title") }}
                  </div>
                  <draggable
                    tag="ul"
                    :list="evidenceSheetReplyComponents"
                    v-bind="{
                      group: { name: 'people', pull: 'clone', put: false },
                      sort: false,
                      ghostClass: 'ghost',
                    }"
                    @end="handleMoveEnd"
                    @start="handleMoveStart"
                    :move="handleMove"
                  >
                    <template
                      v-for="(item, index) in evidenceSheetReplyComponents"
                    >
                      <li
                        v-if="evidenceSheetReplyFields.indexOf(item.type) >= 0"
                        class="form-edit-widget-label"
                        :class="{ 'no-put': item.type == 'divider' }"
                        :key="index"
                      >
                        <a>
                          <i class="icon iconfont" :class="item.icon"></i>
                          <span>{{ item.name }}</span>
                        </a>
                      </li>
                    </template>
                  </draggable>
                </template>
              </template>
              <!-- 回复复核 -->
              <template v-if="evidenceSheetReplyReviewFields.length">
                <template v-if="flowType == '4'">
                  <!-- 取证单回复复核字段 -->
                  <div class="widget-cate">
                    {{ $t("fm.components.evidenceSheetReplyReview.title") }}
                  </div>
                  <draggable
                    tag="ul"
                    :list="evidenceSheetReplyReviewComponents"
                    v-bind="{
                      group: { name: 'people', pull: 'clone', put: false },
                      sort: false,
                      ghostClass: 'ghost',
                    }"
                    @end="handleMoveEnd"
                    @start="handleMoveStart"
                    :move="handleMove"
                  >
                    <template
                      v-for="(
                        item, index
                      ) in evidenceSheetReplyReviewComponents"
                    >
                      <li
                        v-if="
                          evidenceSheetReplyReviewFields.indexOf(item.type) >= 0
                        "
                        class="form-edit-widget-label"
                        :class="{ 'no-put': item.type == 'divider' }"
                        :key="index"
                      >
                        <a>
                          <i class="icon iconfont" :class="item.icon"></i>
                          <span>{{ item.name }}</span>
                        </a>
                      </li>
                    </template>
                  </draggable>
                </template>
              </template>
              <!-- 问题信息录入 -->
              <template v-if="problemEntryFields.length">
                <template v-if="flowType == '5'">
                  <!-- 问题信息录入字段 -->
                  <div class="widget-cate">
                    {{ $t("fm.components.problemEntry.title") }}
                  </div>
                  <draggable
                    tag="ul"
                    :list="problemEntryComponents"
                    v-bind="{
                      group: { name: 'people', pull: 'clone', put: false },
                      sort: false,
                      ghostClass: 'ghost',
                    }"
                    @end="handleMoveEnd"
                    @start="handleMoveStart"
                    :move="handleMove"
                  >
                    <template v-for="(item, index) in problemEntryComponents">
                      <li
                        v-if="problemEntryFields.indexOf(item.type) >= 0"
                        class="form-edit-widget-label"
                        :class="{ 'no-put': item.type == 'divider' }"
                        :key="index"
                      >
                        <a>
                          <i class="icon iconfont" :class="item.icon"></i>
                          <span>{{ item.name }}</span>
                        </a>
                      </li>
                    </template>
                  </draggable>
                </template>
              </template>
              <!-- 整改复核 -->
              <template v-if="rctifyReviewFields.length">
                <template v-if="flowType == '6'">
                  <!-- 整改复核字段 -->
                  <div class="widget-cate">
                    {{ $t("fm.components.rctifyReview.title") }}
                  </div>
                  <draggable
                    tag="ul"
                    :list="rctifyReviewComponents"
                    v-bind="{
                      group: { name: 'people', pull: 'clone', put: false },
                      sort: false,
                      ghostClass: 'ghost',
                    }"
                    @end="handleMoveEnd"
                    @start="handleMoveStart"
                    :move="handleMove"
                  >
                    <template v-for="(item, index) in rctifyReviewComponents">
                      <li
                        v-if="rctifyReviewFields.indexOf(item.type) >= 0"
                        class="form-edit-widget-label"
                        :class="{ 'no-put': item.type == 'divider' }"
                        :key="index"
                      >
                        <a>
                          <i class="icon iconfont" :class="item.icon"></i>
                          <span>{{ item.name }}</span>
                        </a>
                      </li>
                    </template>
                  </draggable>
                </template>
              </template>
              <!-- 外审取证单 -->
              <template v-if="externalAuditFields.length">
                <template v-if="flowType == '7'">
                  <!-- 外审取证单字段 -->
                  <div class="widget-cate">
                    {{ $t("fm.components.externalAudit.title") }}
                  </div>
                  <draggable
                    tag="ul"
                    :list="externalAuditComponets"
                    v-bind="{
                      group: { name: 'people', pull: 'clone', put: false },
                      sort: false,
                      ghostClass: 'ghost',
                    }"
                    @end="handleMoveEnd"
                    @start="handleMoveStart"
                    :move="handleMove"
                  >
                    <template v-for="(item, index) in externalAuditComponets">
                      <li
                        v-if="externalAuditFields.indexOf(item.type) >= 0"
                        class="form-edit-widget-label"
                        :class="{ 'no-put': item.type == 'divider' }"
                        :key="index"
                      >
                        <a>
                          <i class="icon iconfont" :class="item.icon"></i>
                          <span>{{ item.name }}</span>
                        </a>
                      </li>
                    </template>
                  </draggable>
                </template>
              </template>

              <!-- 外审取证单 -->
              <template v-if="externalAuditReplyFields.length">
                <template v-if="flowType == '8'">
                  <!-- 外审取证单字段 -->
                  <div class="widget-cate">
                    {{ $t("fm.components.externalAuditReply.title") }}
                  </div>
                  <draggable
                    tag="ul"
                    :list="externalAuditReplyComponets"
                    v-bind="{
                      group: { name: 'people', pull: 'clone', put: false },
                      sort: false,
                      ghostClass: 'ghost',
                    }"
                    @end="handleMoveEnd"
                    @start="handleMoveStart"
                    :move="handleMove"
                  >
                    <template v-for="(item, index) in externalAuditReplyComponets">
                      <li
                        v-if="externalAuditReplyFields.indexOf(item.type) >= 0"
                        class="form-edit-widget-label"
                        :class="{ 'no-put': item.type == 'divider' }"
                        :key="index"
                      >
                        <a>
                          <i class="icon iconfont" :class="item.icon"></i>
                          <span>{{ item.name }}</span>
                        </a>
                      </li>
                    </template>
                  </draggable>
                </template>
              </template>
              <template v-if="advanceFields.length">
                <!-- 高级字段 -->
                <div class="widget-cate">
                  {{ $t("fm.components.advance.title") }}
                </div>
                <draggable
                  tag="ul"
                  :list="advanceComponents"
                  v-bind="{
                    group: { name: 'people', pull: 'clone', put: false },
                    sort: false,
                    ghostClass: 'ghost',
                  }"
                  @end="handleMoveEnd"
                  @start="handleMoveStart"
                  :move="handleMove"
                >
                  <template v-for="(item, index) in advanceComponents">
                    <li
                      v-if="advanceFields.indexOf(item.type) >= 0"
                      class="form-edit-widget-label"
                      :class="{ 'no-put': item.type == 'table' }"
                      :key="index"
                    >
                      <a>
                        <i class="icon iconfont" :class="item.icon"></i>
                        <span>{{ item.name }}</span>
                      </a>
                    </li>
                  </template>
                </draggable>
              </template>

              <template v-if="layoutFields.length">
                <!-- 布局字段 -->
                <div class="widget-cate">
                  {{ $t("fm.components.layout.title") }}
                </div>
                <draggable
                  tag="ul"
                  :list="layoutComponents"
                  v-bind="{
                    group: { name: 'people', pull: 'clone', put: false },
                    sort: false,
                    ghostClass: 'ghost',
                  }"
                  @end="handleMoveEnd"
                  @start="handleMoveStart"
                  :move="handleMove"
                >
                  <template v-for="(item, index) in layoutComponents">
                    <li
                      v-if="layoutFields.indexOf(item.type) >= 0"
                      class="form-edit-widget-label no-put"
                      :key="index"
                    >
                      <a>
                        <i class="icon iconfont" :class="item.icon"></i>
                        <span>{{ item.name }}</span>
                      </a>
                    </li>
                  </template>
                </draggable>
              </template>
            </div>
          </el-aside>
          <el-container class="center-container" direction="vertical">
            <!-- 动作 -->
            <el-header class="btn-bar" style="height: 45px">
              <slot name="action"> </slot>
              <el-button
                v-if="upload"
                type="text"
                size="medium"
                icon="el-icon-upload2"
                @click="handleUpload"
                >{{ $t("fm.actions.import") }}</el-button
              >
              <el-button
                v-if="clearable"
                type="text"
                size="medium"
                icon="el-icon-delete"
                @click="handleClear"
                >{{ $t("fm.actions.clear") }}</el-button
              >
              <el-button
                v-if="preview"
                type="text"
                size="medium"
                icon="el-icon-view"
                @click="handlePreview"
                >{{ $t("fm.actions.preview") }}</el-button
              >
              <el-button
                v-if="generateJson"
                type="text"
                size="medium"
                icon="el-icon-tickets"
                @click="handleGenerateJson"
                >{{ $t("fm.actions.json") }}</el-button
              >
              <el-button
                v-if="generateCode"
                type="text"
                size="medium"
                icon="el-icon-document"
                @click="handleGenerateCode"
                >{{ $t("fm.actions.code") }}</el-button
              >
            </el-header>
            <el-main :class="{ 'widget-empty': widgetForm.list.length == 0 }">
              <widget-form
                v-if="!resetJson"
                ref="widgetForm"
                :data="widgetForm"
                :select.sync="widgetFormSelect"
              ></widget-form>
            </el-main>
          </el-container>

          <el-aside class="widget-config-container">
            <el-container>
              <el-header height="45px">
                <!-- 字段属性 -->
                <div
                  class="config-tab"
                  :class="{ active: configTab == 'widget' }"
                  @click="handleConfigSelect('widget')"
                >
                  {{ $t("fm.config.widget.title") }}
                </div>
                <!-- 表单数据 -->
                <!-- <div
                  class="config-tab"
                  :class="{ active: configTab == 'form' }"
                  @click="handleConfigSelect('form')"
                >
                  {{ $t("fm.config.form.title") }}
                </div> -->
              </el-header>
              <el-main class="config-content">
                <!-- 配置 -->
                <widget-config
                  v-show="configTab == 'widget'"
                  :data="widgetFormSelect"
                ></widget-config>
                <!-- <form-config
                  v-show="configTab == 'form'"
                  :data="widgetForm.config"
                ></form-config> -->
              </el-main>
            </el-container>
          </el-aside>

          <cus-dialog
            :visible="previewVisible"
            @on-close="previewVisible = false"
            ref="widgetPreview"
            width="1000px"
            form
          >
            <generate-form
              insite="true"
              @on-change="handleDataChange"
              v-if="previewVisible"
              :data="widgetForm"
              :value="widgetModels"
              :remote="remoteFuncs"
              :formOptions="formOptions"
              :isDisabled="isDisabled"
              @orgSign="orgSign"
              @editTable="editTable"
              ref="generateForm"
            >
              <template v-slot:blank="scope">
                Width
                <el-input
                  v-model="scope.model.blank.width"
                  style="width: 100px"
                ></el-input>
                Height
                <el-input
                  v-model="scope.model.blank.height"
                  style="width: 100px"
                ></el-input>
              </template>
            </generate-form>

            <template slot="action">
              <el-button type="primary" @click="handleTest">{{
                $t("fm.actions.getData")
              }}</el-button>
              <el-button @click="handleReset">{{
                $t("fm.actions.reset")
              }}</el-button>
            </template>
          </cus-dialog>

          <cus-dialog
            :visible="uploadVisible"
            @on-close="uploadVisible = false"
            @on-submit="handleUploadJson"
            ref="uploadJson"
            width="800px"
            form
          >
            <el-alert
              type="info"
              :title="$t('fm.description.uploadJsonInfo')"
            ></el-alert>
            <div id="uploadeditor" style="height: 400px; width: 100%">
              {{ jsonEg }}
            </div>
          </cus-dialog>

          <cus-dialog
            :visible="jsonVisible"
            @on-close="jsonVisible = false"
            ref="jsonPreview"
            width="800px"
            form
          >
            <div id="jsoneditor" style="height: 400px; width: 100%">
              {{ jsonTemplate }}
            </div>

            <template slot="action">
              <el-button
                type="primary"
                class="json-btn"
                :data-clipboard-text="jsonCopyValue"
                >{{ $t("fm.actions.copyData") }}</el-button
              >
            </template>
          </cus-dialog>

          <cus-dialog
            :visible="codeVisible"
            @on-close="codeVisible = false"
            ref="codePreview"
            width="800px"
            form
            :action="false"
          >
            <!-- <div id="codeeditor" style="height: 500px; width: 100%;">{{htmlTemplate}}</div> -->
            <el-tabs
              type="border-card"
              style="box-shadow: none"
              v-model="codeActiveName"
            >
              <el-tab-pane label="Vue Component" name="vue">
                <div id="vuecodeeditor" style="height: 500px; width: 100%">
                  {{ vueTemplate }}
                </div>
              </el-tab-pane>
              <el-tab-pane label="HTML" name="html">
                <div id="codeeditor" style="height: 500px; width: 100%">
                  {{ htmlTemplate }}
                </div>
              </el-tab-pane>
            </el-tabs>
          </cus-dialog>
        </el-container>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import Draggable from "vuedraggable";
import WidgetConfig from "./WidgetConfig";
import FormConfig from "./FormConfig";
import WidgetForm from "./WidgetForm";
import CusDialog from "./CusDialog";
import GenerateForm from "./GenerateForm";
import Clipboard from "clipboard";
import {
  basicComponents,
  layoutComponents,
  advanceComponents,
  evidenceSheetComponents,
  evidenceSheetReceiptComponents,
  evidenceSheetReplyComponents,
  evidenceSheetReplyReviewComponents,
  problemEntryComponents,
  rctifyReviewComponents,
  externalAuditComponets,
  externalAuditReplyComponets
} from "./componentsConfig.js";
import { loadJs, loadCss } from "../util/index.js";
import request from "../util/request.js";
import generateCode from "./generateCode.js";

export default {
  name: "fm-making-form",
  components: {
    Draggable,
    WidgetConfig,
    FormConfig,
    WidgetForm,
    CusDialog,
    GenerateForm,
  },
  props: {
    flowType: {
      type: String,
      default: "1",
    },
    preview: {
      type: Boolean,
      default: false,
    },
    generateCode: {
      type: Boolean,
      default: false,
    },
    generateJson: {
      type: Boolean,
      default: false,
    },
    upload: {
      type: Boolean,
      default: false,
    },
    clearable: {
      type: Boolean,
      default: false,
    },
    basicFields: {
      type: Array,
      default: () => [
        "input",
        "textarea",
        "number",
        "radio",
        "checkbox",
        "time",
        "date",
        "rate",
        "color",
        "select",
        "switch",
        "slider",
        "text",
        "fileupload",
      ],
    },
    advanceFields: {
      type: Array,
      default: () => [
        "blank",
        "imgupload",
        "editor",
        "cascader",
        "proItem",
        "quesItem",
        "rectifyItem",
        "auditEdItem",
        "contrItem",
        "riskmattersItem",
        "suggestItem",
        "accountabilityItem",
        "amountProblemsItem",
        "notAmountProblemsItem",
        "countItem",
        "hanldeDeptItem",
        "ChiefEngineerItem",
        "BureauLeaderItem",
        "HandleCSItem",
        "leadershipItem"
      ],
    },
    layoutFields: {
      type: Array,
      default: () => ["grid", "inline"],
    },
    //取证单
    evidenceSheetFields: {
      type: Array,
      default: () => [
        "auditdate",
        "auditproject",
        "eventselect",
        "auditorselect",
        "audittext",
        "secendDept",
        "auditItem",
        "problem",
        "auditopinion",
        "replyReviewAuditDeptChargeLeader"
      ],
    },
    //取证单收文
    evidenceSheetReceiptFields: {
      type: Array,
      default: () => [
        "receiptCompany",
        "receiptTietle",
        "departmentHead",
        "proposedOpinions",
        "InstructionsOpinions",
        "leadership",
        "leadershipInstructions",
        "evidenceId",
        "handleOpinion",
        "receiptdate",
        "cbcsfzr",
        "cbcsfgfzr",
        "blr",
        "bmfgfzr",
        "audittext",
        "eventselect",
        "secendDept",
      ],
    },
    //取证单回复
    evidenceSheetReplyFields: {
      type: Array,
      default: () => [
        "providerOpinion",
        "replyFiles",
        "orgSign",
        "replyDate",
        "leaderOpinion",
        "handleOpinion",
        "departmentLeaderOpinion",
        "auditDepartmentLeader",
        "auditDepartmentLeaderOpinion",
        "auditBusinessLeader",
        "auditBusinessLeaderOpinion",
        "mainDepartmentLeader",
        "mainDepartmentLeaderOpinion",
        "departmentLeader",
        "deptLeaderOpinion",
        "cbcsfzr",
        "cbcsfgfzr",
        "bmfgfzr",
        "replyHandleOpinion",
        "secendDept",
        "audittext",
        "receiptTietle",
        "evidenceId",
        "eventselect",
      ],
    },
    //取证单回复复核
    evidenceSheetReplyReviewFields: {
      type: Array,
      default: () => [
        "replyReviewCompany",
        "replyReviewTietle",
        "replyReviewDate",
        "replyReviewAuditItem",
        "replyReviewAuditItemOpinion",
        "replyReviewAuditItemLeader",
        "replyReviewAuditItemLeaderOpinion",
        "replyReviewAuditDeptProLeader",
        "replyReviewAuditDeptProLeaderOpinion",
        "replyReviewAuditDeptMainLeader",
        "replyReviewAuditDeptMainLeaderOpinion",
        "replyReviewAuditDeptChargeLeader",
        "replyReviewAuditDeptChargeLeaderOpinion",
        "replyReviewAuditDeptLeader",
        "replyReviewAuditDeptLeaderOpinion",
        "replyHandleOpinion",
        "secendDept",
      ],
    },
    //问题录入
    problemEntryFields: {
      type: Array,
      default: () => [
        "projectId",
        "auditTypeName",
        "auditYear",
        "orgName",
        "deptName",
        "auditModuleName",
        "evidenceId",
        "questionId",
        "questionType",
        "questionDetails",
        "reducedAmount",
        "questionLevel",
        "contractName",
        "contractCode",
        "contractAmount",
        "supplierName",
        "suggestion",
        "riskmatters",
        "managesuggestion",
      ],
    },
    //整改审核
    rctifyReviewFields: {
      type: Array,
      default: () => [
        "projectId",
        "auditTypeName",
        "auditYear",
        "orgName",
        "deptName",
        "auditModuleName",
        "evidenceId",
        "questionId",
        "rectificationMethod",
        "questionDetails",
        "reducedAmount",
        "questionLevel",
        "contractName",
        "contractCode",
        "contractAmount",
        "supplierName",
        "suggestion",
        "riskmatters",
        "managesuggestion",

        "causeAnalysis",
        "situation",
        "supportFile",
        "review",
        "state",
        "isCancel",
        "responseDept",
        "personLiable",
        "mainPenaltyAmount",
        "secondaryResponseDept",
        "secondaryPersonLiable",
        "secondaryPenaltyAmount",
        "questionClass",
        "questionSubclass",
        "adjustAccountAccount",
        "recoveryFund",
        "retrieveLoss",
        "returnCapitalChannel",
        "paymentTaxesFees",
        "otherAmount",
        "sumAmount",
        "newSystem",
        "reviseImproveSystem",
        "improveProcesses",
        "otherNotAmount",
        "rectifyDutyDept",
        "rectifyDutyPerson",
        "mainJob",
        "mainMajor",
        "secondaryJob",
        "secondaryMajor",
      ],
    },
    //取证单外审
    externalAuditFields: {
      type: Array,
      default: () => [
        "getTime",
        "getCode",
        "auditDept",
        "evidenceCode",
        "auditItem",
        "content",
        "auditPro",
        "auditedDept",
        "evidenceFile",
        "proposedOpinions",
        "InstructionsOpinions",
        "leadership",
        "handleOpinion",
        "cbcsfzr",
        "cbcsfgfzr",
        "blr",
        "bmfgfzr",
        "auditor",
        "departmentHead",
      ],
    },
    //取证单外审回复
    externalAuditReplyFields: {
      type: Array,
      default: () => [
        "getTime",
        "getCode",
        "auditDept",
        "evidenceCode",
        "auditItem",
        "auditPro",
        "auditedDept",
        "blOpinions",
        "csfzOpinions",
        "bmfzOpinions",
        "jointlySignDept",
        "jointlySignDeptOpinions",
        "auditDeptOpinions",
        "fgfzs",
        "fgjld",
        "fgfzsOpinions",
        "fgjldOpinions",
        "zsjsOpinions",
        "jdzsjOpinions",
        "jzOpinions",
        "zsjs",
        "cbcsfzr",
        "cbcsfgfzr",
        "blr",
        "bmfgfzr",
        'director',
        'jdzsj'
      ],
    },
  },
  data() {
    return {
      basicComponents,
      layoutComponents,
      advanceComponents,
      evidenceSheetComponents,
      evidenceSheetReceiptComponents,
      evidenceSheetReplyComponents,
      evidenceSheetReplyReviewComponents,
      problemEntryComponents,
      rctifyReviewComponents,
      externalAuditComponets,
      externalAuditReplyComponets,
      resetJson: false,
      widgetForm: {
        list: [],
        config: {
          labelWidth: 160,
          labelPosition: "right",
          size: "small",
        },
      },
      configTab: "widget",
      widgetFormSelect: null,
      previewVisible: false,
      jsonVisible: false,
      codeVisible: false,
      uploadVisible: false,
      remoteFuncs: {
        func_test(resolve) {
          setTimeout(() => {
            const options = [
              { id: "1", name: "1111" },
              { id: "2", name: "2222" },
              { id: "3", name: "3333" },
            ];

            resolve(options);
          }, 2000);
        },
        funcGetToken(resolve) {
          request
            .get("http://tools-server.xiaoyaoji.cn/api/uptoken")
            .then((res) => {
              resolve(res.uptoken);
            });
        },
        upload_callback(response, file, fileList) {
          console.log("callback", response, file, fileList);
        },
      },
      formOptions: {},

      isDisabled: false,
      widgetModels: {},
      blank: "",
      htmlTemplate: "",
      vueTemplate: "",
      jsonTemplate: "",
      uploadEditor: null,
      jsonCopyValue: "",
      jsonClipboard: null,
      jsonEg: `{
  "list": [],
  "config": {
    "labelWidth": 160,
    "labelPosition": "top",
    "size": "small"
  }
}`,
      codeActiveName: "vue",
    };
  },
  mounted() {
    this._loadComponents();
  },
  methods: {
    orgSign() {
      this.$emit('orgSign')
    },
    editTable(data) {
      this.$emit('editTable',{
        id: data.id,
        flag: data.flag
      })
    },
    _loadComponents() {
      this.basicComponents = this.basicComponents.map((item) => {
        return {
          ...item,
          name: this.$t(`fm.components.fields.${item.type}`),
        };
      });
      //取证单定制
      this.evidenceSheetComponents = this.evidenceSheetComponents.map(
        (item) => {
          return {
            ...item,
            name: this.$t(`fm.evidenceSheetComponets.fields.${item.type}`),
          };
        }
      );

      //取证单收文定制
      this.evidenceSheetReceiptComponents =
        this.evidenceSheetReceiptComponents.map((item) => {
          return {
            ...item,
            name: this.$t(
              `fm.evidenceSheetReceiptComponents.fields.${item.type}`
            ),
          };
        });

      //取证单回复定制
      this.evidenceSheetReplyComponents = this.evidenceSheetReplyComponents.map(
        (item) => {
          return {
            ...item,
            name: this.$t(
              `fm.evidenceSheetReplyComponents.fields.${item.type}`
            ),
          };
        }
      );

      //取证单回复复核定制
      this.evidenceSheetReplyReviewComponents =
        this.evidenceSheetReplyReviewComponents.map((item) => {
          return {
            ...item,
            name: this.$t(
              `fm.evidenceSheetReplyReviewComponents.fields.${item.type}`
            ),
          };
        });

      //问题信息录入定制
      this.problemEntryComponents = this.problemEntryComponents.map((item) => {
        return {
          ...item,
          name: this.$t(`fm.problemEntryComponents.fields.${item.type}`),
        };
      });

      //整改复核定制
      this.rctifyReviewComponents = this.rctifyReviewComponents.map((item) => {
        return {
          ...item,
          name: this.$t(`fm.rctifyReviewComponents.fields.${item.type}`),
        };
      });

      //外审取证单定制
      this.externalAuditComponets = this.externalAuditComponets.map((item) => {
        return {
          ...item,
          name: this.$t(`fm.externalAuditComponets.fields.${item.type}`),
        };
      });

      //外审回复定制
      this.externalAuditReplyComponets = this.externalAuditReplyComponets.map((item) => {
        return {
          ...item,
          name: this.$t(`fm.externalAuditReplyComponets.fields.${item.type}`),
        };
      });



      this.advanceComponents = this.advanceComponents.map((item) => {
        return {
          ...item,
          name: this.$t(`fm.components.fields.${item.type}`),
        };
      });
      this.layoutComponents = this.layoutComponents.map((item) => {
        return {
          ...item,
          name: this.$t(`fm.components.fields.${item.type}`),
        };
      });
    },
    handleGoGithub() {
      window.location.href = "https://github.com/GavinZhuLei/vue-form-making";
    },
    handleConfigSelect(value) {
      this.configTab = value;
    },
    handleMoveEnd(evt) {
      console.log("end", evt);
    },
    handleMoveStart({ oldIndex }) {
      console.log("start", oldIndex, this.basicComponents);
    },
    handleMove() {
      return true;
    },
    handlePreview() {
      console.log(this.widgetForm);
      this.previewVisible = true;
    },
    handleTest() {
      this.$refs.generateForm
        .getData()
        .then((data) => {
          this.$alert(data, "").catch((e) => {});
          this.$refs.widgetPreview.end();
        })
        .catch((e) => {
          this.$refs.widgetPreview.end();
        });
    },
    handleReset() {
      this.$refs.generateForm.reset();
    },
    handleGenerateJson() {
      this.jsonVisible = true;
      this.jsonTemplate = this.widgetForm;
      console.log(JSON.stringify(this.widgetForm));
      this.$nextTick(() => {
        const editor = ace.edit("jsoneditor");
        editor.session.setMode("ace/mode/json");

        if (!this.jsonClipboard) {
          this.jsonClipboard = new Clipboard(".json-btn");
          this.jsonClipboard.on("success", (e) => {
            this.$message.success(this.$t("fm.message.copySuccess"));
          });
        }
        this.jsonCopyValue = JSON.stringify(this.widgetForm);
      });
    },
    //处理继承代码
    handleGenerateCode() {
      this.codeVisible = true;
      this.htmlTemplate = generateCode(JSON.stringify(this.widgetForm), "html");
      this.vueTemplate = generateCode(JSON.stringify(this.widgetForm), "vue");
      this.$nextTick(() => {
        const editor = ace.edit("codeeditor");
        editor.session.setMode("ace/mode/html");

        const vueeditor = ace.edit("vuecodeeditor");
        vueeditor.session.setMode("ace/mode/html");
      });
    },
    handleUpload() {
      this.uploadVisible = true;
      this.$nextTick(() => {
        this.uploadEditor = ace.edit("uploadeditor");
        this.uploadEditor.session.setMode("ace/mode/json");
      });
    },
    handleUploadJson() {
      try {
        this.setJSON(JSON.parse(this.uploadEditor.getValue()));
        this.uploadVisible = false;
      } catch (e) {
        this.$message.error(e.message);
        this.$refs.uploadJson.end();
      }
    },
    handleClear() {
      this.widgetForm = {
        list: [],
        config: {
          labelWidth: 160,
          labelPosition: "right",
          size: "small",
          customClass: "",
        },
      };

      this.widgetFormSelect = {};
    },
    clear() {
      this.handleClear();
    },
    getJSON() {
      return this.widgetForm;
    },
    getHtml() {
      return generateCode(JSON.stringify(this.widgetForm));
    },
    setJSON(json) {
      console.log(json, "*/*/*/*/*/");
      this.widgetForm = json;

      if (json.list.length > 0) {
        this.widgetFormSelect = json.list[0];
      }
    },
    handleInput(val) {
      console.log(val);
      this.blank = val;
    },
    handleDataChange(field, value, data) {
      console.log(field, value, data);
    },
  },
  watch: {
    widgetForm: {
      deep: true,
      handler: function (val) {
        console.log(this.$refs.widgetForm);
      },
    },
    $lang: function (val) {
      this._loadComponents();
    },
  },
};
</script>

<style>
</style>
