<template>
  <el-form
    ref="feedBackForm"
    v-loading="loading"
    :model="form"
    label-width="150px"
    size="small"
    :rules="rules"
  >
    <div class="base-dialog-body" style="min-height: 500px;">

      <el-form-item
        label="是否认可医学反馈"
        prop="DoctorUserIdeaEnum"
      >
        <el-radio-group
          v-model="form.DoctorUserIdeaEnum"
        >
          <el-radio
            v-for="item of $d.MedicalReviewDoctorUserIdea"
            :key="item.id"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <!-- 类型 -->
      <el-form-item
        v-if="form.DoctorUserIdeaEnum===2"
        label="原因"
        prop="DisagreeReason"
      >
        <el-input :maxLength="$inputMax"
          v-model="form.DisagreeReason"
        />
      </el-form-item>
      <!-- 是否申请重阅 -->
      <el-form-item
        v-if="form.DoctorUserIdeaEnum===1"
        label="是否申请重阅"
        prop="IsApplyHeavyReading"
      >
        <el-radio-group
          v-model="form.IsApplyHeavyReading"
          @change="handleIsApplyHeavyReadingChange"
        >
          <el-radio
            v-for="item of $d.YesOrNo"
            :key="`IsApplyHeavyReading${item.value}`"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        v-if="form.IsApplyHeavyReading"
        label="申请原因"
        prop="ReReadingReason"
        :rules="[
          { required: true, message: $t('common:ruleMessage:select')},
        ]"
      >
        <el-radio-group v-model="form.ReReadingReason">
          <el-radio label="阅片记录错误" />
          <el-radio label="其他" />
        </el-radio-group>
      </el-form-item>
      <!-- 原因 -->
      <el-form-item
        v-if="form.ReReadingReason === '其他'"
        label="备注"
        prop="OtherReason"
        :rules="[
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { max: 500, message: `${this.$t('common:ruleMessage:maxLength')} 500` }
        ]"
      >
        <el-input :maxLength="$inputMax"
          v-model="form.OtherReason"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4}"
          :placeholder="$t('common:ruleMessage:specify')"
          maxlength="500"
          show-word-limit
        />
      </el-form-item>
      <el-form-item
        v-if="form.IsApplyHeavyReading"
        label="是否复制访视阅片表单"
        prop="IsCopyOrigenalForms"
        :rules="[
          { required: true, message: $t('common:ruleMessage:select')},
        ]"
      >
        <el-radio-group v-model="form.IsCopyOrigenalForms">
          <el-radio
            v-for="item of $d.YesOrNo"
            :key="'IsCopyOrigenalForms' + item.value"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
<!--      <el-form-item-->
<!--        v-if="form.IsCopyOrigenalForms"-->
<!--        label="是否复制后续表单"-->
<!--        prop="IsCopyFollowForms"-->
<!--        :rules="[-->
<!--          { required: true, message: $t('common:ruleMessage:select')},-->
<!--        ]"-->
<!--      >-->
<!--        <el-radio-group v-model="form.IsCopyFollowForms">-->
<!--          <el-radio :label="true">是</el-radio>-->
<!--          <el-radio :label="false">否</el-radio>-->
<!--        </el-radio-group>-->
<!--      </el-form-item>-->
      <el-form-item
        label="相关截图"
      >
        <el-upload
          :action="accept"
          :limit="1"
          :on-preview="handlePictureCardPreview"
          :before-upload="handleBeforeUpload"
          :http-request="uploadScreenshot"
          list-type="picture-card"
          :on-remove="handleRemove"
          :file-list="fileList"
          :class="{disabled:uploadDisabled}"
        >
          <i slot="default" class="el-icon-plus" />
          <div slot="file" slot-scope="{file}">
            <img
              class="el-upload-list__item-thumbnail"
              :src="file.url"
              alt=""
            >
            <span class="el-upload-list__item-actions">
              <span
                class="el-upload-list__item-preview"
                @click="handlePictureCardPreview(file)"
              >
                <i class="el-icon-zoom-in" />
              </span>

              <span
                class="el-upload-list__item-delete"
                @click="handleRemove(file)"
              >
                <i class="el-icon-delete" />
              </span>
            </span>
          </div>
        </el-upload>
        <el-dialog
          append-to-body
          :visible.sync="imgVisible"
          width="600px"
        >
          <!-- <img width="100%" :src="imageUrl" alt="图片未找到"> -->
          <el-image :src="imageUrl" width="100%">
            <div slot="placeholder" class="image-slot">
              加载中<span class="dot">...</span>
            </div>
          </el-image>
        </el-dialog>
      </el-form-item>
      <!--      <el-form-item v-if="form.IsApplyHeavyReading" label="重阅影响任务列表">-->
      <!--      </el-form-item>-->
      <div v-if="form.IsApplyHeavyReading" style="margin-bottom: 10px;padding-left: 5px;font-weight: bold;color: #666;">
        重阅影响任务列表
      </div>
      <el-table
        v-if="form.IsApplyHeavyReading"
        v-loading="listLoading"
        :data="InfluenceTaskList"
        stripe
        border
      >
        <el-table-column
          prop="TaskCode"
          label="任务编号"
          show-overflow-tooltip
        />
        <el-table-column
          prop="TrialSiteCode"
          label="中心编号"
          show-overflow-tooltip
        />
        <el-table-column
          prop="SubjectCode"
          label="受试者编号"
          show-overflow-tooltip
        />
        <el-table-column
          prop="TaskBlindName"
          label="访视盲态名称"
          show-overflow-tooltip
        />
        <el-table-column
          prop="TaskState"
          label="任务状态"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.TaskState === 0"
              type="primary"
            >
              {{ $fd('TaskState', scope.row.TaskState) }}
            </el-tag>
            <el-tag
              v-if="scope.row.TaskState === 1"
              type="info"
            >
              {{ $fd('TaskState', scope.row.TaskState) }}
            </el-tag>
            <el-tag
              v-if="scope.row.TaskState === 3"
              type="danger"
            >
              {{ $fd('TaskState', scope.row.TaskState) }}
            </el-tag>
            <el-tag
              v-if="scope.row.TaskState === 4"
              type="warning"
            >
              {{ $fd('TaskState', scope.row.TaskState) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="ReadingCategory"
          label="任务类型"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.ReadingCategory === 1"
              type="primary"
            >
              {{ $fd('ReadingCategory', scope.row.ReadingCategory) }}
            </el-tag>
            <el-tag
              v-if="scope.row.ReadingCategory === 2"
              type="info"
            >
              {{ $fd('ReadingCategory', scope.row.ReadingCategory) }}
            </el-tag>
            <el-tag
              v-if="scope.row.ReadingCategory === 4"
              type="danger"
            >
              {{ $fd('ReadingCategory', scope.row.ReadingCategory) }}
            </el-tag>
            <el-tag
              v-if="scope.row.ReadingCategory === 5"
              type="warning"
            >
              {{ $fd('ReadingCategory', scope.row.ReadingCategory) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="ReadingTaskState"
          label="阅片状态"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.ReadingTaskState === 2"
              type="primary"
            >
              {{ $fd('ReadingTaskState', scope.row.ReadingTaskState) }}
            </el-tag>
            <el-tag
              v-else
              type="danger"
            >
              {{ $fd('ReadingTaskState', scope.row.ReadingTaskState) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="OptType"
          label="生效后影响"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.OptType === 0"
              type="primary"
            >
              {{ $fd('ReReadingOrBackOptType', scope.row.OptType) }}
            </el-tag>
            <el-tag
              v-if="scope.row.OptType === 1"
              type="danger"
            >
              {{ $fd('ReReadingOrBackOptType', scope.row.OptType) }}
            </el-tag>
            <el-tag
              v-if="scope.row.OptType === 2"
              type="warning"
            >
              {{ $fd('ReReadingOrBackOptType', scope.row.OptType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="UserName"
          label="阅片人"
          show-overflow-tooltip
        >
          <template v-if="scope.row.DoctorUser" slot-scope="scope">
            {{ scope.row.DoctorUser.UserName }}({{ scope.row.DoctorUser.FullName }})
          </template>
        </el-table-column>
      </el-table>


    </div>
    <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
      <el-form-item style="text-align:right;">
        <!-- 取消 -->
        <el-button
          size="small"
          type="primary"
          @click="handleClose"
        >
          {{ $t('common:button:cancel') }}
        </el-button>
        <!-- Save -->
        <el-button
          size="small"
          type="primary"
          @click="handleSave"
        >
          发送
        </el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { iRSendMedicalReviewDialog, UploadMedicalReviewImage } from '@/api/trials'
import { getReReadingOrBackInfluenceTaskList } from '@/api/trials/reading'
export default {
  name: 'FeedbackFrom',
  props: {
    taskMedicalReviewId: {
      type: String,
      required: true
    },
    visitTaskId: {
      type: String,
      required: true
    }
  },

  data() {
    return {
      form: {
        TaskMedicalReviewId: '',
        DoctorUserIdeaEnum: null,
        DisagreeReason: '',
        IsApplyHeavyReading: null,
        IsCopyOrigenalForms: null,
        IsCopyFollowForms: null,
        RequestReReadingType: 0,
        RequestReReadingReason: '',
        ReReadingReason: '',
        OtherReason: '',
        ImagePath: '',
        FileName: ''
      },
      InfluenceTaskList: [],
      rules: {
        DoctorUserIdeaEnum: [{ required: true, message: this.$t('common:ruleMessage:select'), trigger: 'blur' }],
        DisagreeReason: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { max: 500, message: `${this.$t('common:ruleMessage:maxLength')} 500` }],
        IsApplyHeavyReading: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: 'blur' }
        ]
      },
      loading: false,
      accept: '.png,.jpg,.jpeg',
      imgVisible: false,
      imageUrl: '',
      uploadDisabled: false,
      listLoading: false,
      fileList: []
    }
  },
  mounted() {

  },
  methods: {
    // 更新qc问题配置信息
    handleSave() {
      this.$refs.feedBackForm.validate(valid => {
        if (!valid) return
        this.loading = true
        this.form.TaskMedicalReviewId = this.taskMedicalReviewId
        this.form.RequestReReadingReason = this.form.OtherReason ? this.form.OtherReason : this.form.ReReadingReason
        this.form.TrialId = this.$route.query.trialId
        iRSendMedicalReviewDialog(this.form).then(res => {
          this.loading = false
          if (res.IsSuccess) {
            // 关闭弹窗并刷新
            this.$emit('close')
            this.$emit('getList')
            this.$message.success(this.$t('common:message:savedSuccessfully'))
          }
        }).catch(() => { this.loading = false })
      })
    },
    handleClose() {
      this.$emit('close')
    },
    handleIsApplyHeavyReadingChange(val) {
      if (val === true) {
        this.listLoading = true
        getReReadingOrBackInfluenceTaskList(this.visitTaskId, true).then(res => {
          this.InfluenceTaskList = res.Result
          this.listLoading = false
        }).catch(() => { this.listLoading = false })
      }
    },
    handleBeforeUpload(file) {
      // 检测文件类型是否符合要求
      if (this.checkFileSuffix(file.name)) {
        this.fileList = []
        return true
      } else {
        this.$message.error(`必须是 ${this.accept} 格式`)
        return false
      }
    },
    checkFileSuffix(fileName) {
      var index = fileName.lastIndexOf('.')
      var suffix = fileName.substring(index + 1, fileName.length)
      if (this.accept.toLocaleLowerCase().search(suffix.toLocaleLowerCase()) === -1) {
        return false
      } else {
        return true
      }
    },
    uploadScreenshot(param) {
      this.loading = true
      this.uploadDisabled = false
      const formData = new FormData()
      formData.append('file', param.file)
      UploadMedicalReviewImage(this.$route.query.trialId, this.taskMedicalReviewId, formData).then(res => {
        this.loading = false
        if (res.IsSuccess) {
          this.fileList.push({ name: res.Result.FileName, url: `/api/${res.Result.Path}` })
          this.imageUrl = `/api/${res.Result.Path}`
          this.form.ImagePath = res.Result.Path
          this.form.FileName = res.Result.FileName
        }
        this.uploadDisabled = true
      }).catch(() => {
        this.loading = false
      })
    },
    // 预览图片
    handlePictureCardPreview() {
      this.imgVisible = true
      this.uploadDisabled = true
    },
    // 删除图片
    handleRemove(file, fileList) {
      this.uploadDisabled = false
      this.imageUrl = ''
      this.fileList = []
      this.form.ImagePath = ''
      this.form.FileName = ''
    }
  }
}
</script>

<style lang="scss" scoped>
.disabled{
  >>>.el-upload--picture-card {
    display: none;
  }
}

</style>
