<template>
  <div class="edit-page page-prop-edit">
    <el-form ref="form" :model="form" label-width="70px">
      <el-form-item v-if="editPageSize" label="纸张规格">
        <el-select
          v-model="form.pageSize"
          placeholder="请选择"
          :disabled="pageSizeOptions.length < 2"
          @change="modifyPageSize"
        >
          <el-option
            v-for="item in pageSizeOptions"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="editColumnNumber" label="栏位布局">
        <el-button
          v-for="(item, index) in columnOptions"
          :key="index"
          class="column-btn"
          :title="item.title"
          :disabled="item.disabled"
          @click="modifyColumnNum(item)"
        >
          <i
            :class="[
              'icon',
              form.columnNumber == item.value
                ? `icon-column-${item.label}-act`
                : `icon-column-${item.label}`,
            ]"
          ></i>
        </el-button>
      </el-form-item>
      <el-form-item v-if="editForbidArea" label="禁答区域">
        <el-checkbox
          v-model="form.showForbidArea"
          @change="showForbidAreaChange"
          >启用</el-checkbox
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { objAssign } from "../../plugins/utils";
import { mapState, mapActions } from "vuex";
import { getModel as getPageModel } from "./model";

const COLUMN_OPTIONS = [
  {
    value: 1,
    title: "一栏",
    label: "one",
    sizeValid: ["A3", "A4"],
  },
  {
    value: 2,
    title: "二栏",
    label: "two",
    sizeValid: ["A3", "A4"],
  },
  {
    value: 3,
    title: "三栏",
    label: "three",
    sizeValid: ["A3"],
  },
  {
    value: 4,
    title: "四栏",
    label: "four",
    sizeValid: ["A3"],
  },
];

export default {
  name: "EditPage",
  props: {
    editPageSize: {
      type: Boolean,
      default: false,
    },
    editColumnNumber: {
      type: Boolean,
      default: true,
    },
    editForbidArea: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      columnOptions: [],
      pageSizeOptions: ["A3", "A4"],
      form: {
        pageSize: "A3",
        columnNumber: 2,
        showForbidArea: false,
      },
      prePageSize: "A3",
    };
  },
  computed: {
    ...mapState("free", ["curPage"]),
  },
  watch: {
    curPage: {
      immediate: true,
      handler(val) {
        this.form = objAssign(this.form, val);
        this.prePageSize = this.form.pageSize;
        this.columnOptions = COLUMN_OPTIONS.filter((item) =>
          item.sizeValid.includes(this.form.pageSize)
        );
      },
    },
  },
  methods: {
    ...mapActions("free", ["modifyPage"]),
    modifyColumnNum(item) {
      this.$confirm("此操作将会重置当前页面所有元素信息, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          this.form.columnNumber = item.value;
          this.pageChange(true);
        })
        .catch(() => {});
    },
    showForbidAreaChange() {
      this.pageChange();
    },
    modifyPageSize() {
      this.$confirm("此操作将会重置当前页面所有元素信息, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          this.columnOptions = COLUMN_OPTIONS.filter((item) =>
            item.sizeValid.includes(this.form.pageSize)
          );
          this.form.columnNumber = this.columnOptions[0].value;
          this.pageChange(true);
        })
        .catch(() => {
          this.form.pageSize = this.prePageSize;
        });
    },
    pageChange(isInit) {
      if (isInit) {
        let curPage = getPageModel(this.form);
        curPage.id = this.curPage.id;
        this.modifyPage(curPage);
      } else {
        this.modifyPage(Object.assign({}, this.curPage, this.form));
      }
    },
  },
};
</script>
