<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" class="base-search-form">
        <!-- 问题名称 -->
        <el-form-item :label="$t('trials:MIMqcCfg:table:questionName')">
          <el-input v-model="searchData.QuestionName" clearable style="width:120px;" />
        </el-form-item>
        <!-- 语言类型 -->
        <el-form-item
          :label="$t('common:title:languageType')"
        >
          <el-select
            v-model="searchData.LanguageType"
            clearable
          >
            <el-option
              v-for="item of $d.LanguageType"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            />
          </el-select>
        </el-form-item>
        <!-- 类型 -->
        <el-form-item :label="$t('trials:medicalFeedbackCfg:title:taskType')">
          <el-input v-model="searchData.Type" clearable style="width:120px;" />
        </el-form-item>
        <el-form-item
          :label="$t('trials:auditRecord:table:criterion')"
        >
          <el-select v-model="searchData.CriterionTypeEnum" clearable>
            <el-option v-for="item of $d.CriterionType" :key="item.id" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <!-- 是否通用 -->
        <el-form-item :label="$t('dictionary:medicalAudit:label:IsGeneral')">
          <el-select v-model="searchData.IsGeneral" clearable>
            <el-option v-for="item of $d.YesOrNo" :key="item.id" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">
            {{ $t('common:button:search') }}
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t('common:button:reset') }}
          </el-button>
          <el-button type="primary" @click="handleExport">
            {{ $t('common:button:export') }}
          </el-button>
        </el-form-item>
      </el-form>
      <span style="margin-left:auto">
        <el-button type="primary" @click="handleAdd">
          {{ $t('common:button:new') }}
        </el-button>
      </span>
    </div>

    <el-table
      v-loading="loading"
      v-adaptive="{bottomOffset:50}"
      :data="list"
      stripe
      height="100"
      @sort-change="handleSortByColumn"
    >
      <!-- 序号 -->
      <el-table-column
        prop="ShowOrder"
        :label="$t('trials:qcCfg:table:order')"
        width="60"
      />
      <!-- 审核问题 -->
      <el-table-column
        prop="QuestionName"
        :label="$t('trials:MIMqcCfg:table:questionName')"
        show-overflow-tooltip
        min-width="140"
        sortable="custom"
      />
      <!-- 审核问题 -->
      <el-table-column
        prop="LanguageType"
        :label="$t('common:title:languageType')"
        show-overflow-tooltip
        min-width="160"
        sortable="custom"
      >
        <template slot-scope="scope">
          {{ $fd('LanguageType', scope.row.LanguageType) }}
        </template>
      </el-table-column>
      <!-- 类型 -->
      <el-table-column
        prop="Type"
        :label="$t('trials:qcCfg:table:type')"
        show-overflow-tooltip
        min-width="140"
        sortable="custom"
      >
        <template slot-scope="scope">
          {{ $fd('QcType', scope.row.Type) }}
        </template>
      </el-table-column>
      <!-- 选项 -->
      <el-table-column
        prop="TypeValue"
        :label="$t('trials:qcCfg:table:typeValue')"
        show-overflow-tooltip
        min-width="140"
        sortable="custom"
      />
      <!-- 任务类型 -->
      <el-table-column
        prop="ReadingCategory"
        :label="$t('trials:medicalFeedbackCfg:title:taskType')"
        show-overflow-tooltip
        min-width="140"
        sortable="custom"
      >
        <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="ReadingCategory"
        :label="$t('trials:auditRecord:table:criterion')"
        min-width="160"
        show-overflow-tooltip
        sortable="custom"
      >
        <template slot-scope="scope">
         <div>
           {{$fd('CriterionType', scope.row.CriterionTypeEnum)}}
         </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="IsGeneral"
        :label="$t('dictionary:medicalAudit:label:IsGeneral')"
        min-width="120"
        show-overflow-tooltip
        sortable="custom"
      >
        <template slot-scope="scope">
          <div>
            {{$fd('YesOrNo', scope.row.IsGeneral)}}
          </div>
        </template>
      </el-table-column>
      <!-- 父问题 -->
      <el-table-column
        prop="ParentShowOrder"
        :label="$t('trials:qcCfg:table:parentQs')"
        show-overflow-tooltip
        min-width="140"
        sortable="custom"
      />
      <!-- 父问题触发值 -->
      <el-table-column
        prop="ParentTriggerValue"
        :label="$t('trials:qcCfg:table:parentTriggerValue')"
        show-overflow-tooltip
        min-width="160"
        sortable="custom"
      />
      <!-- 是否必填 -->
      <el-table-column
        prop="IsRequired"
        :label="$t('trials:qcCfg:table:isRequired')"
        min-width="120"
        sortable="custom"
      >
        <template slot-scope="scope">
          {{ $fd('YesOrNo', scope.row.IsRequired) }}
        </template>
      </el-table-column>
      <!-- 启用状态 -->
      <el-table-column
        prop="IsEnable"
        :label="$t('trials:qcCfg:table:isEnable')"
        min-width="120"
        sortable="custom"
      >
        <template slot-scope="scope">
          {{ $fd('YesOrNo', scope.row.IsEnable) }}
        </template>
      </el-table-column>

      <el-table-column :label="$t('common:action:action')" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleEdit(scope.row)"
          >
            {{ $t('common:button:edit') }}
          </el-button>
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
          >
            {{ $t('common:button:delete') }}
          </el-button>

        </template>
      </el-table-column>
    </el-table>
    <Pagination class="page" :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize" @pagination="getList" />
    <MedicalAuditForm ref="medicalAuditForm" @getList="getList" />

  </box-content>
</template>
<script>
import { getReadingMedicineSystemQuestionList, deleteReadingMedicineSystemQuestion } from '@/api/dictionary'
import { GetReadingMedicineSystemQuestionList_Export } from '@/api/export'
import BoxContent from '@/components/BoxContent'
import Pagination from '@/components/Pagination'
import MedicalAuditForm from './MedicalAuditForm'

const searchDataDefault = () => {
  return {
    PageIndex: 1,
    PageSize: 20,
    QuestionName: '',
    LanguageType: null,
    Type: '',
    CriterionTypeEnum: null,
    IsGeneral: null,
    Asc: false,
    SortField: ''
  }
}
export default {
  name: 'MedicalAudit',
  components: { BoxContent, MedicalAuditForm, Pagination },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      loading: false,
      rowData: {},
      model_cfg: { visible: false, showClose: true, width: '600px', title: '' },
      total: 0
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async handleExport() {
      await GetReadingMedicineSystemQuestionList_Export(this.searchData)
    },
    // 排序
    handleSortByColumn(column) {
      if (column.order === 'ascending') {
        this.searchData.Asc = true
      } else {
        this.searchData.Asc = false
      }
      this.searchData.SortField = column.prop
      this.searchData.PageIndex = 1
      this.getList()
    },
    // 获取受试者列表信息
    getList() {
      this.loading = true
      getReadingMedicineSystemQuestionList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { this.loading = false })
    },
    // 新增受试者
    handleAdd() {
      this.$nextTick(() => {
        this.$refs['medicalAuditForm'].openDialog(this.$t('common:button:new'), {})
      })
    },
    // 编辑受试者信息
    handleEdit(row) {
      this.$nextTick(() => {
        this.$refs['medicalAuditForm'].openDialog(this.$t('common:button:edit'), row)
      })
    },
    // 删除受试者
    handleDelete(row) {
      this.$confirm(this.$t('trials:staffResearch:message:confirmDel'), {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          this.loading = true
          deleteReadingMedicineSystemQuestion(row.Id)
            .then(res => {
              this.loading = false
              if (res.IsSuccess) {
                this.getList()
                this.$message.success(this.$t('common:message:deletedSuccessfully'))
              }
            }).catch(() => { this.loading = false })
        })
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    }
  }
}
</script>
