<template>
  <el-form
    ref="pageBreakForm"
    v-loading="loading"
    :model="form"
    size="small"
    :rules="rules"
    label-width="110px"
  >
    <div class="base-dialog-body">
      <el-form-item
        label="分页名称"
        prop="PageName"
      >
        <el-input
          v-model="form.PageName"
        />
      </el-form-item>

      <!-- 分页类型 -->
      <el-form-item
        label="是否公共分页"
        prop="IsPublicPage"
      >
        <el-radio-group
          v-model="form.IsPublicPage"
        >
          <el-radio
            v-for="item of $d.YesOrNo"
            :key="'IsPublicPage'+item.value"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="序号" prop="ShowOrder">
        <el-input-number v-model="form.ShowOrder" controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item
        label="是否启用"
      >
        <el-radio-group
          v-model="form.IsEnable"
        >
          <el-radio
            v-for="item of $d.YesOrNo"
            :key="'IsEnable'+item.value"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

    </div>
    <div
      class="base-dialog-footer"
      style="text-align:right;margin-top:10px;"
    >
      <el-form-item>
        <!-- 取消 -->
        <el-button
          size="small"
          type="primary"
          @click="handleClose"
        >
          {{ $t('common:button:cancel') }}
        </el-button>
        <!-- 保存 -->
        <el-button
          size="small"
          type="primary"
          @click="handleSave"
        >
          {{ $t('common:button:save') }}
        </el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { addOrUpdateReadingCriterionPage } from '@/api/trials'
export default {
  name: 'PageBreakList',
  props: {
    data: {
      type: Object,
      default() { return {} }
    }
  },

  data() {
    return {
      form: {
        TrialId: '',
        Id: '',
        PageName: '',
        IsPublicPage: false,
        IsEnable: true,
        ShowOrder: 0
      },
      rules: {
        PageName: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur', 'change'] }],
        IsPublicPage: [{ required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }]
      },
      loading: false,
      trialId: ''
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.initForm()
  },
  methods: {
    initForm() {
      if (Object.keys(this.data).length > 0) {
        for (const k in this.form) {
          if (this.data.hasOwnProperty(k)) {
            this.form[k] = this.data[k]
          }
        }
        if (!this.data.Path) return
        this.fileList = [
          {
            name: this.data.FileName,
            path: this.data.Path
          }
        ]
      }
    },
    handleSave() {
      this.$refs.pageBreakForm.validate(valid => {
        if (!valid) return
        this.loading = true
        this.form.TrialId = this.trialId
        addOrUpdateReadingCriterionPage(this.form).then(res => {
          this.loading = false
          this.$emit('getList', true)
          this.$emit('close')
          this.$emit('reloadArbitrationRules')
          this.$message.success(this.$t('common:message:savedSuccessfully'))
        })
          .catch(() => {
            this.loading = false
          })
      })
    },
    handleClose() {
      this.$emit('close')
    }

  }
}
</script>
<style lang="scss" scoped>
</style>
