<template>
  <div>
    <el-form :model="property" :label-position="labelPosition" label-width="72px">
      <el-form-item label="序号" class="serial-number">
        <el-input v-model="property.serial_number" />
        <SvgIcon icon-class="switch" size="14" @click="switchSerialNumber(property)" />
      </el-form-item>
      <el-form-item>
        <el-radio
          v-for="{ value, label } in snGenerationMethodList"
          :key="value"
          v-model="property.sn_generation_method"
          :label="value"
        >
          {{ label }}
        </el-radio>
      </el-form-item>
      <el-form-item label="序号位置">
        <div class="grid-container">
          <div class="top">
            <el-button
              :class="['top-start', { active: 'top-start' === property.sn_position }]"
              @click="changeNumberPosition('top-start')"
            />
            <el-button
              :class="['top', { active: 'top' === property.sn_position }]"
              @click="changeNumberPosition('top')"
            />
            <el-button
              :class="['top-end', { active: 'top-end' === property.sn_position }]"
              @click="changeNumberPosition('top-end')"
            />
          </div>
          <div class="left">
            <el-button
              :class="['left-start', { active: 'left-start' === property.sn_position }]"
              @click="changeNumberPosition('left-start')"
            />
            <el-button
              :class="['left', { active: 'left' === property.sn_position }]"
              @click="changeNumberPosition('left')"
            />
            <el-button
              :class="['left-end', { active: 'left-end' === property.sn_position }]"
              @click="changeNumberPosition('left-end')"
            />
          </div>
          <div class="main"></div>
          <div class="right">
            <el-button
              :class="['right-start', { active: 'right-start' === property.sn_position }]"
              @click="changeNumberPosition('right-start')"
            />
            <el-button
              :class="['right', { active: 'right' === property.sn_position }]"
              @click="changeNumberPosition('right')"
            />
            <el-button
              :class="['right-end', { active: 'right-end' === property.sn_position }]"
              @click="changeNumberPosition('right-end')"
            />
          </div>
          <div class="bottom">
            <el-button
              :class="['bottom-start', { active: 'bottom-start' === property.sn_position }]"
              @click="changeNumberPosition('bottom-start')"
            />
            <el-button
              :class="['bottom', { active: 'bottom' === property.sn_position }]"
              @click="changeNumberPosition('bottom')"
            />
            <el-button
              :class="['bottom-end', { active: 'bottom-end' === property.sn_position }]"
              @click="changeNumberPosition('bottom-end')"
            />
          </div>
        </div>
      </el-form-item>
      <el-divider />
      <el-form-item label="查看方式">
        <el-radio v-for="{ value, label } in viewMethodList" :key="value" v-model="property.view_method" :label="value">
          {{ label }}
        </el-radio>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import SettingMixin from '@/views/book/courseware/create/components/common/SettingMixin';
import {
  snGenerationMethodList,
  viewMethodList,
  switchSerialNumber,
  checkString,
} from '@/views/book/courseware/data/common';

export default {
  name: 'PictureSetting',
  mixins: [SettingMixin],
  data() {
    return {
      switchSerialNumber,
      checkString,
      snGenerationMethodList,
      viewMethodList,
      labelPosition: 'left',
      property: {
        serial_number: 1, // 序号
        sn_type: 'number',
        sn_position: 'top-start', // 序号位置：top-start top top-end 等
        sn_generation_method: snGenerationMethodList[0].value, // 序号生成方式：recalculate 重新计算follow 跟随
        view_method: viewMethodList[0].value, // 查看方式：independent 独立 list 列表icon 图标
      },
    };
  },
  watch: {
    property: {
      handler(val) {
        if (this.isSet) {
          val.sn_type = checkString(val.serial_number);
          this.$emit('updateSetting', val);
        }
      },
      deep: true,
    },
  },
  methods: {
    /**
     * @description 设置属性
     * @param {Object} property 属性
     */
    setSetting(property) {
      this.isSet = true;
      this.property = property;
    },

    /**
     * @description 改变序号位置
     * @param {String} sn_position
     */
    changeNumberPosition(sn_position) {
      this.property.sn_position = sn_position;
    },
  },
};
</script>

<style lang="scss" scoped>
.el-form {
  .serial-number {
    :deep .el-form-item__content {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
  }

  .el-input {
    margin-right: 16px;
  }

  .top {
    grid-area: top;
    column-gap: 8px;
    justify-content: center;
  }

  .left {
    flex-direction: column;
    grid-area: left;
    row-gap: 4px;
  }

  .main {
    grid-area: main;
  }

  .right {
    flex-direction: column;
    grid-area: right;
    row-gap: 4px;
  }

  .bottom {
    grid-area: bottom;
    column-gap: 8px;
    justify-content: center;
  }

  .grid-container {
    display: grid;
    grid:
      'top top top top top top'
      'left main main main main right'
      'bottom bottom bottom bottom bottom bottom';
    width: 134px;
    height: 80px;
    padding: 8px 8px 0;
    line-height: 10px;
    border: 1px solid #ebebeb;

    div {
      display: flex;
      margin: 0 auto;
      text-align: center;
      background-color: rgba(255, 255, 255, 8%);

      .el-button {
        width: 16px;
        height: 8px;
        padding: 0;
        margin: 0;
        border: 1px solid #e4e4e4;
        border-radius: 2px;

        &.active {
          background-color: $setting-active-color;
        }
      }
    }

    .main {
      width: 64px;
      height: 32px;
      background-color: #f8f8f8;
      border: 1px solid #e4e4e4;
      border-radius: 2px;
    }
  }

  .el-divider {
    margin: 16px 0;
  }
}
</style>
