<template>
  <div v-loading="loading" style="min-height:400px;">
    <el-card v-if="isRender" shadow="never" :body-style="{ padding: '10px' }">
      <div slot="header" class="clearfix">
        <span style="font-weight: bold;">历史评判结果</span>
      </div>
      <div v-for="task in visitTaskInfoList" :key="task.visitTaskId">
        <el-form
          ref="questions"
          size="small"
          :model="task.questionForm"
        >
          <FormItem
            v-for="qs in task.questions"
            :key="qs.ReadingQuestionTrialId"
            :question="qs"
            :question-form="task.questionForm"
            :reading-task-state="2"
            @resetFormItemData="resetFormItemData"
          />
        </el-form>
      </div>
    </el-card>

    <el-card v-if="isRender" shadow="never" :body-style="{ padding: '10px' }" class="box-mr">
      <div slot="header" class="clearfix">
        <span style="font-weight: bold;">裁判结果</span>
      </div>
      <el-form ref="adForm" :model="adForm">
        <el-form-item
          label=""
          prop="judgeResultTaskId"
          :rules="[
            { required: true, message: '请选择'},
          ]"
        >
          <el-radio-group v-model="adForm.judgeResultTaskId" :disabled="readingTaskState >= 2">
            <el-radio v-for="t in visitTaskInfoList" :key="t.visitTaskId" :label="t.visitTaskId">{{ t.armEnum }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="备注"
          prop="judgeResultRemark"
          :rules="[
            { required: true, message: '请注明'},
          ]"
        >
          <el-input
            v-model="adForm.judgeResultRemark"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            :disabled="readingTaskState >= 2"
          />
        </el-form-item>
        <el-form-item v-if="readingTaskState < 2">
          <div style="text-align:center;">
            <el-button type="primary" @click="handleSave">保存</el-button>
            <el-button type="primary" @click="handleSubmit">提交</el-button>
          </div>

        </el-form-item>
      </el-form>
    </el-card>
    <!-- 签名框 -->
    <el-dialog
      v-if="signVisible"
      :visible.sync="signVisible"
      :close-on-click-modal="false"
      width="600px"
      custom-class="base-dialog-wrapper"
    >
      <div slot="title">
        <span style="font-size:18px;">{{ $t('common:dialogTitle:sign') }}</span>
        <span style="font-size:12px;margin-left:5px">{{ `(${$t('common:label:sign')}${ currentUser })` }}</span>
      </div>
      <SignForm ref="signForm" :sign-code-enum="signCode" @closeDialog="closeSignDialog" />
    </el-dialog>
  </div>

</template>

<script>

import { getJudgeReadingInfo, saveJudgeVisitTaskResult, submitJudgeVisitTaskResult } from '@/api/trials'
import const_ from '@/const/sign-code'
import FormItem from './FormItem'
import SignForm from '@/views/trials/components/newSignForm'
export default {
  name: 'ECRF',
  components: {
    FormItem,
    SignForm
  },
  props: {
    trialId: {
      type: String,
      required: true
    },
    visitTaskId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      isRender: false,
      signVisible: false,
      signCode: null,
      currentUser: zzSessionStorage.getItem('userName'),
      readingTaskState: 0,
      visitTasks: [],
      adForm: {
        visitTaskId: '',
        judgeResultTaskId: '',
        judgeResultRemark: ''
      },
      visitTaskInfoList: []
    }
  },
  mounted() {
    this.getQuestions()
  },
  methods: {
    getQuestions() {
      this.loading = true
      var param = {
        visitTaskId: this.visitTaskId
      }
      getJudgeReadingInfo(param).then(res => {
        this.adForm.judgeResultTaskId = res.Result.JudgeResultTaskId
        this.adForm.judgeResultRemark = res.Result.JudgeResultRemark
        this.readingTaskState = res.Result.ReadingTaskState
        res.Result.VisitTaskInfoList.forEach(task => {
          var obj = {
            visitTaskId: task.VisitTaskId,
            armEnum: this.$fd('ArmEnum', task.ArmEnum),
            questions: [],
            questionForm: {}
          }
          task.TaskReadingQuestionList.map((v) => {
            if (v.Type === 'group' && v.Childrens.length === 0) return
            obj.questions.push(v)
            if (v.Type !== 'group' && v.Type !== 'summary') {
              this.$set(obj.questionForm, v.ReadingQuestionTrialId, v.Answer)
            }
            if (v.Childrens.length > 0) {
              this.setChild(obj.questionForm, v.Childrens)
            }
          })
          this.visitTaskInfoList.push(obj)
        })
        this.isRender = true
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    setChild(obj, question) {
      question.forEach(i => {
        // this.$set(obj, i.ReadingQuestionTrialId, i.Answer)
        if (i.Type !== 'group' && i.Type !== 'summary') {
          this.$set(obj, i.ReadingQuestionTrialId, i.Answer)
        }
        if (i.Childrens && i.Childrens.length > 0) {
          this.setChild(obj, i.Childrens)
        }
      })
    },

    handleSave() {
      this.$refs['adForm'].validate((valid) => {
        if (!valid) return
        this.loading = true

        this.adForm.visitTaskId = this.visitTaskId
        saveJudgeVisitTaskResult(this.adForm).then(res => {
          this.$message.success(this.$t('common:message:savedSuccessfully'))
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      })
    },
    handleSubmit() {
      this.$refs['questions'].validate((valid) => {
        if (!valid) return
        const { ImageAssessmentReportConfirmation } = const_.processSignature
        this.signCode = ImageAssessmentReportConfirmation
        this.signVisible = true
      })
    },
    // 关闭签名框
    closeSignDialog(isSign, signInfo) {
      if (isSign) {
        this.signConfirm(signInfo)
      } else {
        this.signVisible = false
      }
    },
    // 签名并确认
    signConfirm(signInfo) {
      this.loading = true
      var answers = []
      for (const k in this.questionForm) {
        answers.push({ readingQuestionTrialId: k, answer: this.questionForm[k] })
      }
      var params = {
        data: {
          visitTaskId: this.visitTaskId,
          judgeResultTaskId: this.adForm.judgeResultTaskId,
          judgeResultRemark: this.adForm.judgeResultRemark
        },
        signInfo: signInfo
      }
      submitJudgeVisitTaskResult(params).then(res => {
        this.loading = false
        if (res.IsSuccess) {
          this.$message.success(this.$t('common:message:savedSuccessfully'))
          this.isEdit = false
          this.$refs['signForm'].btnLoading = false
          this.signVisible = false
          window.location.reload()
          window.opener.postMessage('refreshTaskList', window.location)
        }
      }).catch(_ => {
        this.loading = false
        this.$refs['signForm'].btnLoading = false
      })
    },
    resetFormItemData() {}
  }
}
</script>
<style lang="scss" scoped>
.box-mr{
  margin:10px 0;
}
</style>
