<template>
  <div>
    <el-form :inline="false" :model="form" label-position="right" label-width="120px">
      <div class="setting-top">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="因子分" label-width="120px" prop="factorType">
              <el-radio-group v-model="form.factorType" size="mini">
                <el-radio label="1">有</el-radio>
                <el-radio label="2">无</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="naxtBox">
              <el-button v-if="form.factorType==1" class="" size="mini" type="primary" @click="addFactor">新增因子
              </el-button>
              <el-button v-show="showPreviousBtn" size="mini" type="primary" @click="previousTab()">上一步</el-button>
              <el-button class="" size="mini" type="primary" @click="save">下一步</el-button>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-form>

    <div v-if="form.factorType==1" class="setting-content">
      <factor-setting
          v-for="(it,index) in form.rules"
          :key="it.uid"
          ref="fs"
          :data.sync="it"
          :index="index"
          :maxIndex="form.rules.length"
          :questionList="questionList"
          class="setting-content-item"
          @update="(res)=>form.rules[index]=res"
      >
        <div class="y-del-factor">
          <el-button
              v-if="form.rules.length>1"
              size="mini"
              @click="delFactor(index)">
            删除因子
          </el-button>
        </div>
      </factor-setting>
    </div>
  </div>
</template>

<script>

import FactorSetting from './factor-setting'
import config from './config'

export default {
  name: 'factor',
  components: { FactorSetting },
  inject: ['getToken', 'getSetting'],
  props: {
  },
  data () {
    return {
      // 通用配置结构
      configName: 'factor', // 配置名称
      showPreviousBtn: false,
      // 题项目列表
      questionList: [],
      // 配置
      form: {
        factorType: '2',
        rules: []
      },
    }
  },
  provide () {
    return {
      getToken:  () =>  this.getToken(),
      getSetting: () => this.getSetting(),
    }
  },
  watch: {
    'form.factorType': {
      handler (type) {
        if (typeof type == 'number') {
          this.form.factorType = String(type)
        }
        if (type === '1' && this.form.rules.length == 0) {
          this.form.rules.push({})
        } else if (type === '2') {
          this.form.rules = []
        }
      },
      immediate: true
    },
  },
  methods: {
    save () {
      let is_bool = true
      for (let i = 0; i < this.form.rules.length; i++) {
        if (this.form.factorType == 1 && is_bool) {
          is_bool = this.$refs['fs'][i].form_validate()
        }
      }
      if (is_bool) {
        console.log('因子配置保存：', this.form)
        config.localSetConclusionList('updateData', this.configName, this.form)

        this.$emit('NextTab')
        // 保存接口
        // this.$message.success('保存成功')
      }

    },
    // 获取当前因子配置
    loadData () {
      if (config.localGetConclusionList('updateData', this.configName)) {
        this.form = config.localGetConclusionList('updateData', this.configName)
      } else if (config.localGetConclusionList('requestData', this.configName)) {
        this.form = config.localGetConclusionList('requestData', this.configName)
      } else {
        // 默认配置
        this.form = {
          rules: [],
          factorType: '2'
        }
      }
      this.showPreviousBtn = config.permissionIndex(this.configName)

      console.log('加载-因子分配置', this.configName, this.form)
      // 获取题目列表
      this.getQuestionList()
    },
    // 获取题目列表
    async getQuestionList () {
      const { host, hostSuffix, questionListPath, method, shortId } = this.getSetting()
      console.log('获取question 数据', shortId)
      this.$tool.request({
        url: host + hostSuffix + questionListPath,
        headers: {
          Authorization: await this.getToken(),
          way: 1
        },
        method: method,
        params: {
          shortId: shortId
        }
      }).then(res => {
        if (res.code === 200) {
          this.questionList = res.data
        } else if (res.code === 500 && res.data) {
          this.$alert(`<h3><i class="el-icon-warning"></i> 未识别到以下标题中题号，请优先修改</h3><ul><li>${res.data.join('</li><li>')}</li></ul>`, {
            dangerouslyUseHTMLString: true,
            showClose: false,
            customClass: 'question-error-list-alert',
            confirmButtonText: '已知晓'
          })
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        this.$message.error(err)
      })
    },
    // 上一步
    previousTab () {
      this.$emit('PreviousTab')
    },
    // 获取当前配置因子
    getFactor () {
      console.log('获取当前因子：', this.form.rules)
      return this.form.rules
    },
    // 删除因子
    delFactor (index) {
      this.form.rules.splice(index, 1)
    },
    // 新增因子
    addFactor () {
      this.form.rules.push({
        uid: this.$tool.random.snowflakeId()
      })
    },
  }
}
</script>
<style>
/* 异常弹窗 */
.question-error-list-alert {
  width: 450px;
}

.question-error-list-alert h3 {
  text-align: center;
  padding: 0;
  margin: 0;
}

.question-error-list-alert .el-icon-warning {
  color: #e6a23c;
  font-size: 1.2em;
  position: relative;
  top: auto;
  padding-right: 5px;
  text-align: center;
  transform: translateY(1px);
}

.question-error-list-alert ul {
  min-height: 60px;
  max-height: 140px;
  overflow-y: auto;
  padding: 0 0 0 10px;
  margin: 10px 0 0 0;
}

.question-error-list-alert ul li {
  white-space: nowrap;
  text-overflow: ellipsis;
  list-style-position: inside;
  overflow: hidden;
}

.question-error-list-alert ul::-webkit-scrollbar {
  width: 2px;
}

/*滚动条样式*/
.question-error-list-alert ul::-webkit-scrollbar {
  width: 5px;
  /*height: 4px;*/
}

.question-error-list-alert ul::-webkit-scrollbar-thumb {
  border-radius: 10px;
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  background: rgba(0, 0, 0, 0.2);
}

.question-error-list-alert ul::-webkit-scrollbar-track {
  -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  border-radius: 0;
  background: rgba(0, 0, 0, 0.1);
}
</style>
<style scoped>

.el-form {
  background: #f0f0f0;
  margin: 0;
}

.y-del-factor {
  float: right;
  z-index: 1;
  right: 40px;
  line-height: 40px;
  position: relative;
}

.setting-top {
  background: #FFFFFF;
  padding-top: 20px;
}

.naxtBox {
  position: absolute;
  right: 50px;
}

/*.setting-top .el-form-item {*/
/*  margin: 10px;*/
/*}*/

.setting-top .el-form-item .button {
  position: absolute;
  right: 50px;
  bottom: 5px;
}

.setting-content {
  margin: 10px 10px 0px 10px;
  background: #FFFFFF;
  /* height: 600px; */
  /* overflow-y: scroll; */
  /* overflow-x: auto; */
  /* overflow-x: overlay; */
}

.setting-content-item {
  margin-bottom: 10px;
  background-color: #f7f9fd;
}
</style>
