<template>
  <QuestionBase>
    <template #content>
      <div v-show="isMask" class="mask"></div>
      <div class="stem">
        <RichText v-model="data.stem" :font-size="18" placeholder="输入题干" />

        <RichText
          v-if="isEnable(data.property.is_enable_description)"
          v-model="data.description"
          placeholder="输入提示"
        />
      </div>

      <div class="content">
        <div class="option-wrapper" :style="computedOptionStyle()">
          <template v-for="i in data.property.column_number">
            <div :key="i" class="fill-form">
              <!-- 头部 -->
              <span
                v-if="isEnable(data.property.is_enable_table_header)"
                class="form-header"
                :style="{ paddingLeft: isEnable(data.property.is_enable_number_column) && i === 1 ? '40px' : '0' }"
              >
                <el-input v-model="data.option_header_list[i - 1].text" placeholder="请输入" />
              </span>

              <span v-for="j in data.property.row_number" :key="j" :class="['form-item', `form-item-${i}-${j}`]">
                <span v-if="i === 1 && isEnable(data.property.is_enable_number_column)" class="serial-number">
                  {{ j }}
                </span>
                <el-input v-model="data.option_list[j - 1][i - 1].text" placeholder="请输入" />
              </span>
            </div>
            <span
              v-if="i !== data.property.column_number"
              :key="`resize-${i}`"
              class="resize"
              @mousedown="resize($event, i)"
            ></span>
          </template>
        </div>

        <el-input
          v-if="isEnable(data.property.is_enable_reference_answer)"
          v-model="data.reference_answer"
          type="textarea"
          rows="3"
          placeholder="输入参考答案"
        />
      </div>
    </template>

    <template #property>
      <el-form :model="data.property" label-width="72px" label-position="left">
        <el-form-item label="题号">
          <el-input v-model="data.property.question_number" />
        </el-form-item>
        <el-form-item>
          <el-radio
            v-for="{ value, label } in questionNumberTypeList"
            :key="value"
            v-model="data.other.question_number_type"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>

        <el-form-item label="题干题号">
          <el-select v-model="data.property.stem_question_number_font_size">
            <el-option v-for="item in fontSizeList" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>

        <el-form-item label="提示">
          <el-radio
            v-for="{ value, label } in switchOption"
            :key="value"
            v-model="data.property.is_enable_description"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>

        <el-form-item label="参考答案">
          <el-radio
            v-for="{ value, label } in switchOption"
            :key="value"
            v-model="data.property.is_enable_reference_answer"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>

        <el-form-item label="表头">
          <el-radio
            v-for="{ value, label } in switchOption"
            :key="value"
            v-model="data.property.is_enable_table_header"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>

        <el-form-item label="序号列">
          <el-radio
            v-for="{ value, label } in switchOption"
            :key="value"
            v-model="data.property.is_enable_number_column"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item label="列表宽度">
          <el-input-number v-model="data.property.form_width" :min="100" :max="3000" :step="100" />
        </el-form-item>
        <el-form-item label="行数">
          <el-input-number v-model="data.property.row_number" :min="1" :max="30" />
        </el-form-item>
        <el-form-item label="列数">
          <el-input-number v-model="data.property.column_number" :min="2" :max="5" />
        </el-form-item>
        <el-form-item label="分值">
          <el-radio
            v-for="{ value, label } in scoreTypeList"
            :key="value"
            v-model="data.property.score_type"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item>
          <el-input-number
            v-model="data.property.score"
            :min="0"
            :step="data.property.score_type === scoreTypeList[0].value ? 1 : 0.1"
          />
        </el-form-item>
      </el-form>
    </template>
  </QuestionBase>
</template>

<script>
import QuestionMixin from '../common/QuestionMixin.js';

import { getRandomNumber } from '@/utils';
import { getTableFillData, getOption } from '@/views/exercise_questions/data/tableFill.js';

export default {
  name: 'TableFillQuestion',
  mixins: [QuestionMixin],
  data() {
    return {
      isMask: false,
      data: getTableFillData(),
    };
  },
  computed: {
    optionList() {
      return JSON.parse(JSON.stringify(this.data.option_list));
    },
  },
  watch: {
    'data.property.row_number': {
      handler(val) {
        if (val > this.data.option_list.length) {
          this.data.option_list.push(Array.from({ length: this.data.property.column_number }, () => getOption()));
        } else if (val < this.data.option_list.length) {
          this.data.option_list.pop();
        }
      },
      immediate: true,
    },
    'data.property.column_number': {
      handler(val) {
        // 列数变化时，需要重新计算每列的宽度
        if (val > this.data.option_header_list.length) {
          let width = 0;
          this.data.option_header_list.forEach((item) => {
            // 平均分配宽度，最小宽度为10
            let w = Math.max((item.width / val) * this.data.option_header_list.length, 10);
            width += item.width - w;
            item.width = w;
          });
          this.data.option_header_list.push({ mark: getRandomNumber(), text: '', width });
        } else if (val < this.data.option_header_list.length) {
          let width = this.data.option_header_list[val].width;
          this.data.option_header_list.pop();
          this.data.option_header_list.forEach((item) => {
            item.width += width / val;
          });
        }

        this.data.option_list = this.data.option_list.map((item) => {
          const arr = [];
          for (let i = 0; i < val; i++) {
            arr.push(item[i] || getOption());
          }
          return arr;
        });
      },
      immediate: true,
    },
    optionList: {
      handler(newVal, oldVal) {
        if (newVal.length === 0 || !oldVal) return;
        if (newVal.length !== oldVal.length || newVal[0].length !== oldVal[0].length) return;
        newVal.forEach((item, i) => {
          item.forEach((li, j) => {
            let newValue = newVal[i][j];
            let oldValue = oldVal[i][j];
            if (newValue.text === oldValue.text) return;
            this.$set(this.data.option_list[i][j], 'type', newValue.text.length > 0 ? 'text' : 'input');
          });
        });
      },
      immediate: true,
      deep: true,
    },
  },
  methods: {
    /**
     *鼠标拖动更改列宽
     * @param {MouseEvent} e 鼠标事件
     * @param {number} i 选项列表的索引
     */
    resize(e, i) {
      let target = e.target;
      let startX = e.clientX;
      this.isMask = true;

      // 鼠标拖动事件
      document.onmousemove = (e) => {
        const endX = e.clientX;
        let list = this.data.option_header_list;
        // 计算拖动的距离与总宽度的比例
        let w = ((endX - startX) / this.data.property.form_width) * 100;
        // 限制最小宽度为10，最大宽度为总宽度-10
        list[i - 1].width = Math.min(Math.max(10, list[i - 1].width + w), list[i - 1].width + list[i].width - 10);
        list[i].width = Math.min(Math.max(10, list[i].width - w), list[i - 1].width + list[i].width - 10);

        startX = endX;
      };

      // 鼠标松开事件
      document.onmouseup = () => {
        this.isMask = false;
        document.onmousemove = null;
        document.onmouseup = null;
        target.releaseCapture && target.releaseCapture(); // 当不在需要继续获得鼠标消息就要应该调用 ReleaseCapture() 释放掉
      };
      target.setCapture && target.setCapture(); // 该函数在属于当前线程的指定窗口里设置鼠标捕获
      return false;
    },
    // 计算选项样式
    computedOptionStyle() {
      let gridTemplateColumns = this.data.option_header_list.reduce((acc, { width }, i) => {
        if (i === this.data.option_header_list.length - 1) {
          return `${acc}${width}%`;
        }
        return `${acc}${width}% 2px `;
      }, '');

      return {
        gridTemplateColumns,
        width: `${this.data.property.form_width}px`,
      };
    },
  },
};
</script>

<style lang="scss" scoped>
.mask {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  cursor: col-resize;
}

.content {
  width: calc(100vw - 645px);
  overflow: auto;

  .option-wrapper {
    display: grid;
    grid-auto-flow: column;
    margin-bottom: 8px;

    .fill-form {
      display: flex;
      flex-direction: column;

      .form-header {
        display: flex;
        align-items: center;
        height: 40px;
        background-color: $fill-color;

        .header-serial-number {
          padding: 8px 16px;
          font-weight: bold;
        }

        :deep .el-input__inner {
          font-size: 16px;
          font-weight: bold;
        }
      }

      .form-item {
        display: flex;
        height: 55px;
        border-bottom: $border;

        .serial-number {
          width: 40px;
          min-width: 40px;
          line-height: 55px;
          text-align: center;
        }

        .el-input {
          :deep &__inner {
            height: 54px;
            font-size: 16px;
            line-height: 54px;
            background-color: #fff;

            &:focus {
              border-color: #c0c4cc;
            }
          }
        }

        :deep .tox-editor-header {
          display: none;
        }

        :deep .tox-editor-header {
          display: none;
        }
      }
    }

    .resize {
      width: 2px;
      height: 100%;
      cursor: col-resize;
      background-color: #165dff;
      border-radius: 4px;
    }
  }
}

.property {
  .el-input-number {
    width: 200px;
  }
}
</style>
