<template>
  <view class="content">
    <view class="card">
      <view class="title">{{ questionnaireInfo.NAME }}</view>
      <uni-table border class="mt-10">
        <uni-tr>
          <uni-th>安全措施</uni-th>
          <uni-th width="200rpx">选项</uni-th>
        </uni-tr>
        <uni-tr v-for="(item,index) in items" :key="item.itemInfo.EW_RE_QUES_ITEM_ID">
          <uni-td>
            <view>{{ item.itemInfo.TEXT_INFO }}</view>
            <view v-for="item1 in item.inputBoxes" :key="item1.EW_RE_QUES_ITEM_FILL_ID">
              <u-cell :title="item1.TEXT_INFO + '：'" :border="false">
                <template #value>
                  <u-input v-model="item1.MARKERS" :customStyle="{marginTop: '8px'}" border="bottom"/>
                </template>
              </u-cell>
            </view>
          </uni-td>
          <uni-td>
            <u-radio-group placement="column" v-model="item.optionsValue" @change="fnRadioChange($event,index)">
              <u-radio :customStyle="{marginTop: '8px'}" v-for="item1 in item.options"
                       :key="item1.EW_RE_QUES_ITEM_FILL_ID" :label="item1.TEXT_INFO" :name="item1.VALUE_INFO"/>
            </u-radio-group>
          </uni-td>
        </uni-tr>
      </uni-table>
      <view class="mt-10">
        <u-button type="primary" text="下一步" @click="$u.debounce(fnSubmit, 1000,true)"/>
      </view>
    </view>
  </view>
</template>

<script>
import {setSecurityMeasures} from "@/utils/submitHomeworkProcess";

export default {
  data() {
    return {
      EW_RU_JOB_ID: '',
      taskId: '',
      TYPE: '',
      CORP_ID: '',
      questionnaireInfo: {},
      items: [],
      vernier: ''
    }
  },
  onLoad(event) {
    this.vernier  = event.vernier
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('questionnaire', (data) => {
      this.EW_RU_JOB_ID = data.EW_RU_JOB_ID
      this.taskId = data.taskId
      this.TYPE = data.TYPE
      this.CORP_ID = data.CORP_ID
      this.questionnaireInfo = data.questionnaire.questionnaire
      this.items = data.questionnaire.items
    })
  },
  methods: {
    fnRadioChange(event, index) {
      for (let i = 0; i < this.items[index].options.length; i++) {
        this.items[index].options[i].MARKERS = '0'
        if (this.items[index].options[i].VALUE_INFO === event) {
          this.items[index].options[i].MARKERS = '1'
        }
      }
    },
    async fnSubmit() {
      for (let i = 0; i < this.items.length; i++) {
        let isYes = false;
        for (let j = 0; j < this.items[i].options.length; j++) {
          if (
              this.items[i].options[j].MARKERS &&
              this.items[i].options[j].MARKERS === "1" &&
              this.items[i].options[j].VALUE_INFO === "1"
          ) {
            isYes = true;
          }
        }
        for (let j = 0; j < this.items[i].inputBoxes.length; j++) {
          if (isYes && !this.items[i].inputBoxes[j].MARKERS) {
            uni.$u.toast(`第${i + 1}行第${j + 1}项未填写`)
            return
          }
        }
        for (let j = 0; j < this.items[i].options.length; j++) {
          if (!this.items[i].options[j].MARKERS) {
            uni.$u.toast(`第${i + 1}行第${j + 1}项未选择`)
            return
          }
        }
      }
      await setSecurityMeasures({
        questionnaires: this.items,
        questionnaireInfo: this.questionnaireInfo,
        taskId: this.taskId,
        EW_RU_JOB_ID: this.EW_RU_JOB_ID,
        CORP_ID: this.CORP_ID,
        TYPE: this.TYPE,
        vernier:this.vernier
      })
    }
  },
}
</script>

<style scoped lang="scss">
.title {
  text-align: center;
  font-size: 36rpx;
}

::v-deep {
  .u-cell__body {
    padding: 0;
  }
}
</style>
