<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" class="base-search-form">
        <el-form-item
          :label="$t('trials:auditRecord:table:criterion')"
        >
          <el-input v-model="searchData.CriterionName" clearable style="width:120px;" />
        </el-form-item>
        <!-- 标准类型  -->
        <el-form-item
          :label="$t('dictionary:template:criterionConfig:table:criterionType')"
        >
          <el-select v-model="searchData.CriterionType" clearable style="width:120px;">
            <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:template:criterionConfig:table:criterionGroup')"
        >
          <el-select v-model="searchData.CriterionGroup" clearable style="width:120px;">
            <el-option
              v-for="item of $d.CriterionGroup"
              :key="item.id"
              :value="item.value"
              :label="item.label"
            />
          </el-select>
        </el-form-item>
        <!-- 是否配置完成 -->
        <el-form-item
          :label="$t('dictionary:template:criterionConfig:table:isCompleteConfig')"
        >
          <el-select v-model="searchData.IsCompleteConfig" clearable style="width:120px;">
            <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
          :label="$t('dictionary:template:criterionConfig:table:isEnable')"
        >
          <el-select v-model="searchData.IsEnable" clearable style="width:120px;">
            <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-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:45}"
      :data="list"
      stripe
      height="100"
      @sort-change="handleSortByColumn"
      ref="criterionsTbl"
    >
      <el-table-column type="index" width="60" />
      <!-- 标准名称 -->
      <el-table-column
        prop="CriterionName"
        :label="$t('trials:auditRecord:table:criterion')"
        show-overflow-tooltip
        sortable="custom"
        width="180"
      />
      <!-- 标准类型 -->
      <el-table-column
        prop="CriterionType"
        :label="$t('dictionary:template:criterionConfig:table:criterionType')"
        show-overflow-tooltip
        sortable="custom"
        width="180"
      >
        <template slot-scope="scope">
          {{ $fd('CriterionType',scope.row.CriterionType) }}
        </template>
      </el-table-column>
      <!-- 标准分组 -->
      <el-table-column
        prop="CriterionGroup"
        :label="$t('dictionary:template:criterionConfig:table:criterionGroup')"
        show-overflow-tooltip
        sortable="custom"
        width="160"
      >
        <template slot-scope="scope">
          {{ $fd('CriterionGroup',scope.row.CriterionGroup) }}
        </template>
      </el-table-column>
      <!-- 是否配置完成 -->
      <el-table-column
        prop="IsCompleteConfig"
        :label="$t('dictionary:template:criterionConfig:table:isCompleteConfig')"
        sortable="custom"
        width="220"
      >
        <template slot-scope="scope">

          <!-- <el-switch
            v-model="scope.row.IsCompleteConfig"
            :disabled="scope.row.IsBeUsed"
            @change="changeStatus($event, scope.row)"
          /> -->
          {{ $fd('YesOrNo', scope.row.IsCompleteConfig) }}
        </template>
      </el-table-column>
      <!-- 描述 -->
      <el-table-column
        prop="Description"
        :label="$t('dictionary:template:criterionConfig:table:description')"
        show-overflow-tooltip
      />
      <!-- 是否启用 -->
      <el-table-column
        prop="IsEnable"
        :label="$t('dictionary:template:criterionConfig:table:isEnable')"
        sortable="custom"
      >
        <template slot-scope="scope">
          {{ $fd('YesOrNo', scope.row.IsEnable) }}
        </template>
      </el-table-column>
      <!-- 显示序号 -->
      <el-table-column
        prop="ShowOrder"
        :label="$t('dictionary:template:criterionConfig:table:showOrder')"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- eICRF仅展示阅片Tab -->
      <el-table-column
        prop="IseCRFShowInDicomReading"
        :label="$t('dictionary:template:criterionConfig:table:isECRFShowInDicomReading')"
        show-overflow-tooltip
        sortable="custom"
        width="210"
      >
        <template slot-scope="scope">
          {{ $fd('YesOrNo', scope.row.IseCRFShowInDicomReading) }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('common:action:action')" min-width="220px">
        <template slot-scope="scope">
          <!-- 编辑 -->
          <el-button
            type="text"
            @click="handleEdit(scope.row)"
          >
            {{ $t('common:button:edit') }}
          </el-button>
          <!-- eCRF -->
          <el-button
            type="text"
            @click="handleConfig(scope.row)"
          >
            {{ $t('dictionary:template:criterionConfig:button:eCRF') }}
          </el-button>
          <!-- 基础数据 -->
          <el-button
            type="text"
            @click="handleBaseDataConfig(scope.row)"
          >
            {{ $t('dictionary:template:criterionConfig:button:basicData') }}
          </el-button>
          <!-- 删除 -->
          <el-button
            type="text"
            :disabled="scope.row.IsCompleteConfig"
            @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" />

    <el-dialog
      v-if="addDialog.visible"
      :title="addDialog.title"
      :visible.sync="addDialog.visible"
      :close-on-click-modal="false"
      append-to-body
      custom-class="base-dialog-wrapper"
      width="600px"
    >
      <add-criterion
        :data="rowData"
        @close="addDialog.visible = false"
        @getList="getList"
      />
    </el-dialog>

    <el-dialog
      v-if="configVisible"
      :title="'eCRF' + `(${rowData.CriterionName})`"
      :visible.sync="configVisible"
      :close-on-click-modal="false"
      :fullscreen="true"
      append-to-body
      custom-class="base-dialog-wrapper"
    >
      <criterions-config :criterion-id="rowData.Id" :data="rowData" />
    </el-dialog>

    <el-dialog
      v-if="configBaseDataVisible"
      :title="`${this.$t('trials:readingUnit:readingCriterion:title:baseDataCfg')} (${rowData.CriterionName})`"
      :visible.sync="configBaseDataVisible"
      :close-on-click-modal="false"
      :fullscreen="true"
      append-to-body
      custom-class="base-dialog-wrapper"
    >
      <CriterionsBaseData
        :criterion-id="rowData.Id"
        :criterion-type="rowData.CriterionType"
        :is-complete-config="rowData.IsCompleteConfig"
      />
    </el-dialog>

  </box-content>
</template>
<script>
import { getReadingQuestionCriterionSystemList, setSystemReadingQuestionCriterionIsCompleteConfig, deleteReadingQuestionCriterionSystem } from '@/api/dictionary'
import BoxContent from '@/components/BoxContent'
import Pagination from '@/components/Pagination'
import CriterionsConfig from './CriterionsConfig'
import CriterionsBaseData from './CriterionsBaseData'
import AddCriterion from './AddCriterion'
const searchDataDefault = () => {
  return {
    CriterionName: '',
    CriterionType: null,
    CriterionGroup: null,
    IsCompleteConfig: null,
    IsEnable: null,
    PageIndex: 1,
    PageSize: 20,
    Asc: true,
    SortField: ''
  }
}
export default {
  name: 'QcQuestions',
  components: { BoxContent, Pagination, CriterionsConfig, CriterionsBaseData, AddCriterion },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      rowData: {},
      configVisible: false,
      configBaseDataVisible: false,
      addDialog: { title: '', visible: false }
    }
  },

  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      getReadingQuestionCriterionSystemList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { this.loading = false })
    },
    handleAdd() {
      this.addDialog.title = this.$t('common:button:new')
      this.addDialog.visible = true
      this.rowData = {}
    },
    handleEdit(row) {
      this.addDialog.title = this.$t('common:button:edit')
      this.addDialog.visible = true
      this.rowData = { ...row }
    },
    handleDelete(row) {
      this.$confirm(this.$t('trials:readingUnit:baseDataCfg:message:deleteConfirm'), {
        distinguishCancelAndClose: true,
        type: 'warning'
      }).then(() => {
        this.loading = true
        deleteReadingQuestionCriterionSystem(row.Id).then(res => {
          this.loading = false
          if (res.IsSuccess) {
            this.$message.success(this.$t('common:message:deletedSuccessfully'))
            this.getList()
          }
        }).catch(() => { this.loading = false })
      }).catch(() => {})
    },
    changeStatus(callback, row) {
      var message = ''
      if (callback) {
        message = this.$t('trials:readingUnit:baseDataCfg:message:updateConfirm')
        row.IsCompleteConfig = false
      } else {
        message = this.$t('trials:readingUnit:baseDataCfg:message:updateConfirm')
        row.IsCompleteConfig = true
      }
      this.$confirm(message, {
        distinguishCancelAndClose: true,
        type: 'warning'
      }).then(() => {
        this.loading = true
        var params = {
          id: row.Id,
          isCompleteConfig: !row.IsCompleteConfig
        }
        setSystemReadingQuestionCriterionIsCompleteConfig(params).then(res => {
          this.loading = false
          if (res.IsSuccess) {
            this.$message.success(this.$t('common:message:savedSuccessfully'))
            this.getList()
          }
        }).catch(() => { this.loading = false })
      }).catch(() => {})
    },
    handleConfig(row) {
      this.rowData = { ...row }
      this.configVisible = true
    },
    handleBaseDataConfig(row) {
      this.rowData = { ...row }
      this.configBaseDataVisible = true
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.$refs['criterionsTbl'].clearSort()
      this.getList()
    },
    // 指定排序字段，对列表进行排序
    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()
    }
  }
}
</script>
