<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" size="mini" 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>
          <el-button type="primary" @click="handleReset">重置</el-button>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
      <span style="margin-left:auto">
        <el-button type="primary" size="mini" @click="handleAdd">添加</el-button>
      </span>
    </div>

    <el-table
      v-loading="loading"
      v-adaptive="{bottomOffset:45}"
      :data="list"
      stripe
      size="small"
      height="100"
    >
      <el-table-column type="index" width="60" />

      <el-table-column
        prop="CriterionName"
        label="标准名称"
        show-overflow-tooltip
      />
      <el-table-column
        prop="CriterionType"
        label="标准类型"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ $fd('CriterionType',scope.row.CriterionType) }}
        </template>
      </el-table-column>

      <el-table-column
        prop="IsCompleteConfig"
        label="是否配置完成"
      >
        <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="描述"
        show-overflow-tooltip
      />

      <el-table-column
        prop="ShowOrder"
        label="显示序号"
        show-overflow-tooltip
      />

      <el-table-column
        prop="IsEnable"
        label="是否启用"
      >
        <template slot-scope="scope">
          {{ $fd('YesOrNo', scope.row.IsEnable) }}
        </template>
      </el-table-column>

      <el-table-column label="Action" min-width="200px">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="primary"
            size="mini"
            @click="handleConfig(scope.row)"
          >
            eCRF
          </el-button>
          <el-button
            type="primary"
            size="mini"
            @click="handleBaseDataConfig(scope.row)"
          >
            基础数据
          </el-button>
          <el-button
            type="danger"
            size="mini"
            :disabled="scope.row.IsCompleteConfig"
            @click="handleDelete(scope.row)"
          >
            删除
          </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="500px"
    >
      <AddCriterion
        :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="'基础数据配置' + `(${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: '',
    PageIndex: 1,
    PageSize: 20
  }
}
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.addDialog.visible = true
      this.rowData = {}
    },
    handleEdit(row) {
      this.addDialog.title = '编辑'
      this.addDialog.visible = true
      this.rowData = { ...row }
    },
    handleDelete(row) {
      this.$confirm('是否确认删除', {
        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:savedSuccessfully'))
            this.getList()
          }
        }).catch(() => { this.loading = false })
      }).catch(() => {})
    },
    changeStatus(callback, row) {
      var message = ''
      if (callback) {
        message = '是否确认更改？'
        row.IsCompleteConfig = false
      } else {
        message = '是否确认更改？'
        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.getList()
    }
  }
}
</script>
