<template>
  <el-form
    ref="subjectStatusForm"
    v-loading="loading"
    :model="form"
    :rules="rules"
    size="small"
    label-width="160px"
  >
    <div class="base-dialog-body">
      <!-- Status -->
      <el-form-item :label="$t('trials:subject:table:status')" prop="Status">
        <el-radio-group v-model="form.Status" :disabled="originalStatus === 2">
          <el-radio
            v-for="item of $d.Subject_Visit_Status"
            :label="item.value"
            :key="item.id"
            >{{ item.label }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <!-- Out Visit Date -->
      <el-form-item
        v-if="form.Status == 2"
        :label="$t('trials:subject:table:outVisitDate')"
        prop="VisitOverTime"
      >
        <el-date-picker
          v-model="form.VisitOverTime"
          type="date"
          :picker-options="pickerOption"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
        />
      </el-form-item>
      <!-- 末次访视 -->
      <el-form-item
        v-if="form.Status === 2"
        :label="$t('trials:subject:table:finalSubjectVisit')"
        prop="FinalSubjectVisitId"
      >
        <el-select
          v-model="form.FinalSubjectVisitId"
          clearable
          filterable
          style="width: 100%"
          :disabled="originalStatus === 2"
        >
          <el-option
            v-for="item in subjectVisitOptions"
            :key="item.Id"
            :label="item.VisitName"
            :value="item.Id"
          />
        </el-select>
      </el-form-item>
      <!-- 结束访视的原因 -->
      <el-form-item
        v-if="form.Status === 2"
        :label="$t('trials:subject:table:outVisitReason')"
        prop="Reason"
      >
        <el-input v-model="form.Reason" type="textarea" autosize />
      </el-form-item>
    </div>
    <div class="base-dialog-footer" style="text-align: right; margin-top: 10px">
      <el-form-item style="text-align: right">
        <!-- 取消 -->
        <el-button
          :disabled="btnLoading"
          size="small"
          type="primary"
          @click="handleCancel"
        >
          {{ $t("common:button:cancel") }}
        </el-button>
        <el-button
          size="small"
          type="primary"
          :loading="btnLoading"
          @click="handleSave"
        >
          {{ $t("common:button:save") }}
        </el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import {
  getSubjectVisitSelectOptions,
  updateSubjectStatus,
} from "@/api/trials";
export default {
  name: "SubjectStatusForm",
  props: {
    data: {
      type: Object,
      default() {
        return {};
      },
    },
  },

  data() {
    return {
      form: {
        SubjectId: "",
        Status: 1,
        OutEnrollmentTime: "",
        Reason: "",
        VisitOverTime: "",
        FinalSubjectVisitId: "",
      },
      rules: {
        OutEnrollmentTime: [
          {
            required: true,
            message: this.$t("common:ruleMessage:select"),
            trigger: ["blur"],
          },
        ],
        FinalSubjectVisitId: [
          {
            required: true,
            message: this.$t("common:ruleMessage:select"),
            trigger: ["blur"],
          },
        ],
        Reason: [
          {
            max: 500,
            message: `${this.$t("common:ruleMessage:maxLength")} 500`,
          },
        ],
        VisitOverTime: [
          {
            required: true,
            message: this.$t("common:ruleMessage:select"),
            trigger: ["blur"],
          },
        ],
      },
      pickerOption: {
        disabledDate: (time) => {
          return time.getTime() > Date.now();
        },
      },
      subjectVisitOptions: [],
      btnLoading: false,
      loading: false,
      originalStatus: null,
      trialId: "",
    };
  },
  mounted() {
    this.trialId = this.$route.query.trialId;
    this.initForm();
  },
  methods: {
    async initForm() {
      this.loading = true;
      const { Result } = await getSubjectVisitSelectOptions(this.data.Id);
      this.subjectVisitOptions = Result;
      if (Object.keys(this.data).length > 0) {
        for (const k in this.form) {
          if (this.data.hasOwnProperty(k)) {
            this.form[k] = this.data[k];
          }
        }
        this.form.SubjectId = this.data.Id;
        this.originalStatus = this.data.Status;
      }
      this.form.TrialId = this.trialId;
      this.loading = false;
    },
    handleSave() {
      this.$refs.subjectStatusForm.validate((valid) => {
        if (!valid) return;
        this.btnLoading = true;

        this.form.TrialId = this.trialId;
        this.form.SiteId = this.data.SiteId;
        updateSubjectStatus(this.form)
          .then((res) => {
            this.btnLoading = false;
            this.$emit("getList");
            // this.$message.success('Saved successfully!')
            this.$message.success(this.$t("common:message:savedSuccessfully"));
            this.$emit("closeDialog");
          })
          .catch(() => {
            this.btnLoading = false;
          });
      });
    },
    handleCancel() {
      this.$emit("closeDialog");
    },
  },
};
</script>
