<template>
  <div class="home-container">
    <head-bar @back="back">
      <template #title>
        <div class="navTitle">修改问卷</div>
      </template>
    </head-bar>
    <div class="step_box">
      <topicVue
        :cJson.sync="cJson"
        @getTestWorkByCid="getTestWorkByCid"
        @publish2="publish2"
        :title="name"
        :tid="tid"
        :brief="brief"
        ref="topicVue"
        :gcourseid="gcourseid"
      ></topicVue>
    </div>
    <div class="edit_top">
      <div class="op_btn">
        <el-button type="primary" size="small" @click="publish">修改</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { selectTestCourseWorks } from '@/api/testDetail'
import { selectTestWorksCid } from '@/api/testDetail'

import { addTestWorks2 } from '@/api/testDetail'
import headBar from '@/components/headBar.vue'
import { mapGetters } from 'vuex'
import topicVue from './components/topic.vue'
export default {
  components: {
    headBar,
    topicVue
  },
  data() {
    return {
      courseid: '',
      tid: this.$route.query.tid,
      name: this.$route.query.name,
      title: '',
      brief: '',
      cJson: [],
      cJson2: [],
      cid: this.$route.query.courseid,
      userid: this.$route.query.userid,
      gcourseid: this.$route.query.courseid
    }
  },
  computed: {
    ...mapGetters(['userinfo'])
  },
  methods: {
    back() {
      this.$router.push({ path: '/test' })
    },
    getData() {
      const params = {
        cid: this.cid,
        tid: this.tid
      }
      selectTestCourseWorks(params)
        .then(res => {
          if (res[1].length) {
            this.cJson = JSON.parse(res[1][0].courseJson)
            this.cJson2 = JSON.parse(res[1][0].courseJson)
          }
          this.title = res[0][0].title
          this.brief = res[0][0].brief
          this.$forceUpdate()
        })
        .catch(err => {
          console.error(err)
        })
    },
    JSONSetting(json) {
      let _json = json
      let array = []
      _json.filter(item => {
        if (item.array) {
          item.array = item.array.filter(item2 => {
            if (item2.ttype == 1 && item2.json) {
              array.push(item2)
            }
            if (item2.array) {
              item2.array = item2.array.filter(item3 => {
                if (item3.ttype == 1 && item3.json) {
                  array.push(item3)
                }
                return item3
              })
            }
            return item2
          })
        }
        if (item.ttype == 1 && item.json) {
          array.push(item)
        }
        // console.log(item.array)
        return item
      })
      // console.log(array)
      return array
    },
    setJson2(json) {
      let _json = json
      // this.type = _json[0].ttype;
      let checkArray = _json.filter(item => {
        if (item.array) {
          item.array = item.array.filter(item2 => {
            if (item2.ttype == 1 && item2.json && !item2.json.answer2) {
              item2.json.answer2 = []
            }
            if (item2.array) {
              item2.array = item2.array.filter(item3 => {
                if (item3.ttype == 1 && item3.json && !item3.json.answer2) {
                  item3.json.answer2 = []
                }
                return item3
              })
            }
            return (item2.ttype != 1 && item2.array.length > 0) || item2.ttype == 1
          })
        }
        if (item.ttype == 1 && item.json && !item.json.answer2) {
          item.json.answer2 = []
        }
        console.log(item.array)
        return (item.ttype != 1 && item.array.length > 0) || item.ttype == 1
      })
      console.log(checkArray)
      return checkArray
    },
    setJSON(json) {
      return json.filter(item => {
        if (item.array) {
          item.array = item.array.filter(item2 => {
            if (item2.ttype == 1 && item2.json) {
              delete item2.json.answer2
              delete item2.json.score2
              delete item2.json.file
            }
            if (item2.array) {
              item2.array = item2.array.filter(item3 => {
                if (item3.ttype == 1 && item3.json) {
                  delete item3.json.answer2
                  delete item3.json.score2
                  delete item3.json.file
                }
                return item3
              })
            }
            return item2
          })
        } else if (item.ttype == 1 && item.json) {
          delete item.json.answer2
          delete item.json.score2
          delete item.json.file
        }
        return item
        console.log(item.array)
      })
    },
    setCourseid(json) {
      let _json = json
      // this.type = _json[0].ttype;
      let checkArray = _json.filter(item => {
        if (item.array) {
          item.array = item.array.filter(item2 => {
            if (item2.ttype == 1 && item2.json && !item2.json.answer2 && item2.type != 6 && item2.type != 7) {
              item2.json.answer2 = []
            } else if (item2.type == 6) {
              item2.json.answer2 = this.courseid ? this.courseid : ''
            } else if (item2.type == 7) {
              item2.json.answer2 = ''
            }
            if (item2.array) {
              item2.array = item2.array.filter(item3 => {
                if (item3.ttype == 1 && item3.json && !item3.json.answer2 && item3.type != 6 && item3.type != 7) {
                  item3.json.answer2 = []
                } else if (item3.type == 6) {
                  item3.json.answer2 = this.courseid ? this.courseid : ''
                } else if (item3.type == 7) {
                  item3.json.answer2 = ''
                }
                return item3
              })
            }
            return (item2.ttype != 1 && item2.array.length > 0) || item2.ttype == 1
          })
        }
        if (item.ttype == 1 && item.json && !item.json.answer2 && item.type != 6 && item.type != 6 && item.type != 7) {
          item.json.answer2 = []
        } else if (item.type == 6) {
          item.json.answer2 = this.courseid ? this.courseid : ''
        } else if (item.type == 7) {
          item.json.answer2 = ''
        }
        console.log(item.array)
        return (item.ttype != 1 && item.array.length > 0) || item.ttype == 1
      })
      console.log(checkArray)
      return checkArray
    },
    getTestWorkByCid(courseid) {
      console.log('修改问卷')
      this.courseid = courseid
      let params = {
        cid: this.cid,
        uid: this.userid
      }
      console.log('getTestWorkByCidparams', params)
      let _courseid = courseid
      selectTestWorksCid(params)
        .then(res => {
          if (res[0].length) {
            let rData = res[0]
            console.log('rData', rData)

            this.tid = ''
            // eslint-disable-next-line no-labels
            aa: for (var i = 0; i < rData.length; i++) {
              let rDataJson = this.JSONSetting(JSON.parse(rData[i].courseJson))
              let _json = JSON.parse(rData[i].courseJson)
              for (var j = 0; j < rDataJson.length; j++) {
                let _param = rDataJson[j]
                if (_param.type == 6 && _param.json.answer2 && _param.json.answer2 == _courseid) {
                  this.cJson = _json
                  console.log(this.cJson)
                  // let cJson = this.setJSON(JSON.parse(JSON.stringify(this.cJson)))
                  // let cJson2 = this.setJSON(this.setJson2(JSON.parse(JSON.stringify(this.cJson2))))
                  // if (JSON.stringify(cJson) != JSON.stringify(cJson2)) {
                  //   this.isReset = true
                  // }
                  this.tid = rData[i].id
                  this.$forceUpdate()
                  break aa
                }
              }
            }
            console.log('tid', this.tid)
            if (!this.tid) {
              this.cJson = this.setJSON(this.setCourseid(JSON.parse(JSON.stringify(this.cJson2))))
            }
          } else {
            this.tid = ''
            console.log(111)
            this.cJson = this.setJSON(this.setCourseid(JSON.parse(JSON.stringify(this.cJson2))))
          }
          this.$forceUpdate()
        })
        .catch(err => {
          console.error(err)
        })
    },
    panJSON(array) {
      var pan = 0
      for (var i = 0; i < array.length; i++) {
        if (array[i].type != 6) {
          let param = array[i].json
          if (array[i].type == 5 && param.file && param.file.length > 0) {
            pan++
          } else if (param.answer2.length || typeof param.answer2 == 'number') {
            pan++
          }
        }
      }
      if (pan > 0) {
        return true
      } else {
        return false
      }
    },

    publish() {
      let cjson = this.$refs['topicVue'].checkArray
      const params = [
        {
          uid: this.tid,
          cid: this.cid,
          cjson: encodeURIComponent(JSON.stringify(cjson)),
          type: '2'
        }
      ]
      addTestWorks2(params)
        .then(res => {
          this.$message.success('修改成功')
          this.back()
        })
        .catch(err => {
          console.error(err)
        })
    },
    publish2() {
      let cjson = this.$refs['topicVue'].checkArray
      let _cjson = this.JSONSetting(JSON.parse(JSON.stringify(cjson)))
      let pan = this.panJSON(_cjson)
      console.log('pan', pan)
      if (!pan) {
        setTimeout(() => {
          this.isloading = false
        }, 500)
        return
      }
      let params = [
        {
          uid: this.tid,
          cid: this.cid,
          cjson: encodeURIComponent(JSON.stringify(cjson)),
          type: '2'
        }
      ]
      if (this.tid) {
        addTestWorks2(params)
          .then(res => {
            this.$message.success('修改成功')
          })
          .catch(err => {
            console.error(err)
          })
      } else {
        console.log('tid呢')
      }
    }
  },

  mounted() {
    this.getData()
  }
}
</script>

<style lang="scss" scoped>
.navTitle {
  font-size: 16px;
  color: #fff;
}
.home-container {
  height: 100vh;
  overflow: hidden;
  .step_box {
    margin-top: 1.5rem;
    width: 100%;
    height: calc(100vh - 1.5rem - 50px);
    overflow: auto;
    position: relative;
  }
  .edit_top {
    width: 100%;
    margin: 0 auto;
    /deep/.el-button {
      width: 100%;
      height: 50px;
      text-align: center;
      font-size: 16px;
    }
  }
}
</style>
