<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="16" @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">
          <el-button
            v-for="{ value } in serialNumberPositionList"
            :key="value"
            :class="[value, { active: value === property.sn_position }]"
            :style="{ gridArea: value }"
            @click="changeNumberPosition(value)"
          />

          <div class="main"></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,
  serialNumberPositionList,
} from '@/views/book/courseware/data/common';

export default {
  name: 'VideoSetting',
  mixins: [SettingMixin],
  data() {
    return {
      switchSerialNumber,
      checkString,
      serialNumberPositionList,
      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, // 查看方式
      },
    };
  },
  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;
    }

    .svg-icon {
      cursor: pointer;
    }
  }

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

  .main {
    grid-area: main;
  }

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

    .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>
