<template>
  <el-card :class="!showButtons ? 'box-card cms-page-form no-buttons' : 'box-card cms-page-form'">
    <div style="line-height: 30px;font-weight: 700;" slot="header" class="clearfix" v-if="formTitle">
      <i class="el-icon-document" style="margin-right: 5px;font-size: 18px;margin-top: -8px"></i>
      <span style="font-size: 18px;font-weight: 700;">{{ formTitle }}</span>
    </div>
    <cms-edit-form
      v-bind="$attrs"
      ref="formRef"
      :label-width="labelWidth"
      :form-model="formModel"
      :form-rules="formRules"
      :form-type="formType">
      <el-collapse v-if="formCollapseItem.length > 0" v-model="activeNames" :accordion="accordion">
        <div class="el-collapse-item-d" v-for="(item, index) in formCollapseItem" :key="index">
          <el-collapse-item :name="item.name" :id="item.name">
            <div slot="title" style="padding-left: 0">
              <i class="el-icon-notebook-1"></i> {{ item.title }}
            </div>
            <slot :name="item.name"></slot>
          </el-collapse-item>
        </div>
      </el-collapse>
    </cms-edit-form>
    <div class="cms-page-button" v-if="showButtons">
      <div class="form-item-anchor">
        <span v-for="(item, index) in formCollapseItem" :key="index">
          <a href="javascript:void(0)" :class="item.name === activeName ? 'active' : ''" @click="jumpToPosition(item.name)">
            <i class="el-icon-info"></i>
            {{ item.title }}</a>
        </span>
      </div>
      <div class="form-item-button">
        <slot name="buttons">
          <cms-button
            type="primary"
            v-if="showSubmit && !(formType === 'view' || formType === 'approval')"
            @click="onSubmit">
            <span>{{ $t("common.button.submit") }}</span>
          </cms-button>
          <cms-button
            @click="onSave"
            v-if="!(formType === 'view' || formType === 'approval')"
            type="primary">
            <span>{{ $t("common.button.save") }}</span>
          </cms-button>
          <cms-button @click="onBack">
            <span>{{ $t("common.button.back") }}</span>
          </cms-button>
        </slot>
      </div>
    </div>
  </el-card>
</template>

<script>
import { EditFormMixin, CmsPageFormMixin } from './edit-form-mixin'

export default {
  name: "CmsPageForm",
  mixins: [ EditFormMixin, CmsPageFormMixin ],
  data() {
    return {
      activeName: this.formCollapseItem.length > 0 ? this.formCollapseItem[0].name : ''
    };
  },
  methods: {
    /**/
    jumpToPosition(name) {
      this.activeName = name;
      document.querySelector("#" + name).scrollIntoView(true);
    },
    /** 提交 */
    onSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$emit("submit", this.formModel);
        } else {
          return false;
        }
      });
    },
    /** 执行保存 */
    onSave() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$emit("save", this.formModel);
        } else {
          return false;
        }
      });
    },
    /** 重置表单 */
    resetFields() {
      this.$refs.formRef.resetFields();
    },
    /** 执行返回 */
    onBack() {
      this.$emit("back", false, this.formModel);
    },
    getForm() {
      return this.$refs.formRef;
    },
    validate(callback) {
      this.$refs.formRef.validate((valid) => {
        if (callback) callback(valid);
      });
    },
    clearValidate(props = []) {
      this.$refs['formRef'].clearValidate(props);
    },
  },
  mounted() {
  },
  props: {},
};
</script>

<style scoped lang="scss">
.el-card__body {
  padding: 0 !important;
  position: static;
}

.edit-page-form {
  padding: 0 !important;
  margin: 20px 20px 0 0;
}

.edit-form-main {
  padding: 0;
}

.edit-page-form-1 {
  padding: 0 !important;
}

.el-collapse-item__wrap {
  padding: 10px 10px 10px 0 !important;
}
</style>
