<template>
  <div v-loading="loading" class="page-break-list-wrapper">
    <div
      style="text-align:left"
    >
      <el-button
        v-if="!isConfirm && hasPermi(['trials:trials-panel:setting:reading-unit:edit'])"
        size="mini"
        type="primary"
        @click="handleAdd"
      >
        新增
      </el-button>
      <el-button
        :disabled="tblList.length===0"
        size="mini"
        type="primary"
        @click="handlePreview"
      >
        预览
      </el-button>
    </div>

    <!-- 分页列表 -->
    <el-table
      :data="tblList"
      style="width: 100%"
      size="small"
    >
      <!-- <el-table-column type="index" width="40" /> -->
      <el-table-column
        prop="ShowOrder"
        label="序号"
        width="70"
      />
      <el-table-column
        prop="PageName"
        label="分页名称"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        prop="IsPublicPage"
        label="是否公共分页"
        show-overflow-tooltip
        width="180"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.IsPublicPage" type="danger">{{ $fd('YesOrNo', scope.row.IsPublicPage) }}</el-tag>
          <el-tag v-else type="primary">{{ $fd('YesOrNo', scope.row.IsPublicPage) }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="IsEnable"
        show-overflow-tooltip
        width="180"
        label="是否启用"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.IsEnable" type="danger">{{ $fd('YesOrNo', scope.row.IsEnable) }}</el-tag>
          <el-tag v-else type="primary">{{ $fd('YesOrNo', scope.row.IsEnable) }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="CreateTime"
        show-overflow-tooltip
        label="创建时间"
        width="200"
      />
      <el-table-column
        prop=""
        label="操作"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleCfgQuestions(scope.row)"
          >
            问题表单
          </el-button>
          <el-button
            v-if="!isConfirm && hasPermi(['trials:trials-panel:setting:reading-unit:edit'])"
            type="primary"
            size="mini"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            v-if="!isConfirm && hasPermi(['trials:trials-panel:setting:reading-unit:edit'])"
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-if="addOrEdit.visible"
      :visible.sync="addOrEdit.visible"
      :close-on-click-modal="false"
      :title="addOrEdit.title"
      width="500px"
      custom-class="base-dialog-wrapper"
    >
      <PageBreakForm
        :data="rowData"
        @close="addOrEdit.visible = false"
        @getList="getList"
      />
    </el-dialog>

    <el-dialog
      v-if="questions.visible"
      :visible.sync="questions.visible"
      :close-on-click-modal="false"
      :title="questions.title"
      width="1500px"
      custom-class="base-dialog-wrapper"
    >
      <QuestionsList
        ref="questions"
        :trial-criterion-id="trialCriterionId"
        :reading-criterion-page-id="rowData.Id"
        :is-confirm="isConfirm"
        :is-preview="false"
        :is-from-system="isFromSystem"
        @reloadArbitrationRules="reloadArbitrationRules"
      />
    </el-dialog>

    <el-dialog
      v-if="preview.visible"
      :visible.sync="preview.visible"
      :close-on-click-modal="false"
      :title="preview.title"
      width="1000px"
      append-to-body
      custom-class="base-dialog-wrapper"
    >
      <div class="base-dialog-body">
        <QuestionsPreview :criterion-id="trialCriterionId" :form-type="2" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getReadingCriterionPageList, deleteReadingCriterionPage } from '@/api/trials'
import PageBreakForm from './PageBreakForm'
import QuestionsList from './QuestionsList'
import QuestionsPreview from './QuestionsPreview'
export default {
  name: 'PageBreakList',
  components: { PageBreakForm, QuestionsList, QuestionsPreview },
  props: {
    trialReadingCriterionId: {
      type: String,
      default: ''
    },
    list: {
      type: Array,
      default() {
        return []
      }
    },
    trialCriterionId: {
      type: String,
      required: true
    },
    isConfirm: {
      type: Boolean,
      default: true
    },
    isFromSystem: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      loading: false,
      addOrEdit: { visible: false, title: '' },
      questions: { visible: false, title: '阅片问题' },
      preview: { visible: false, title: 'eCRF预览' },
      rowData: {},
      tblList: []
    }
  },
  mounted() {
    this.tblList = this.list
  },
  methods: {
    getList() {
      this.loading = true
      const trialId = this.$route.query.trialId
      getReadingCriterionPageList({ trialId, TrialReadingCriterionId: this.trialReadingCriterionId }).then(res => {
        this.loading = false
        this.tblList = res.Result
      }).catch(() => { this.loading = false })
    },
    handleAdd() {
      this.rowData = {
        ReadingQuestionCriterionTrialId: this.trialCriterionId
      }
      this.addOrEdit.title = '添加'
      this.addOrEdit.visible = true
    },
    handleEdit(row) {
      this.rowData = { ...row }
      this.rowData.ReadingQuestionCriterionTrialId = this.trialCriterionId
      this.addOrEdit.title = '编辑'
      this.addOrEdit.visible = true
    },
    handleCfgQuestions(row) {
      this.rowData = { ...row }
      this.questions.visible = true
      this.$nextTick(() => {
        this.$refs['questions'].getList()
      })
    },
    handleDelete(row) {
      this.$confirm('是否确认删除？', {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          this.loading = true
          deleteReadingCriterionPage(row.Id)
            .then(res => {
              this.loading = false
              if (res.IsSuccess) {
                this.getList()
                this.$message.success('删除成功！')
              }
            }).catch(() => { this.loading = false })
        })
    },
    handlePreview() {
      this.preview.visible = true
    },
    reloadArbitrationRules() {
      this.$emit('reloadArbitrationRules')
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
