<template>
  <div>
    <div class="search-form" style="display:flex;justify-content: space-between;">
      <div>
      </div>
      <div>
        <el-button
          size="mini"
          type="primary"
          @click="handleAdd"
          style="margin-right: 10px;"
        >
          配置
        </el-button>
      </div>
    </div>
    <el-table
      v-loading="loading"
      style="width: 100%"
      :data="list"
      stripe
    >
      <el-table-column
        prop=""
        label="序号"
        width="50"
      >
        <template slot-scope="scope">
          {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column
        prop="Code"
        label="字典表名"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="Description"
        label="描述"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="Count"
        label="子项数量"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="ShowOrder"
        label="排序"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="Description"
        label="操作"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="openChildren(scope.row)">
            配置
          </el-button>
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-drawer
      :title="drawer_cfg.title"
      :append-to-body="true"
      :modal-append-to-body="false"
      :visible.sync="drawer_cfg.drawerChild"
      direction="rtl"
      size="80%"
    >
      <CriterionDictionaryConfig v-if="drawer_cfg.drawerChild" @getList="getList" :criterionId="criterionId" :parentCode="drawer_cfg.title"></CriterionDictionaryConfig>
    </el-drawer>
    <el-drawer
      :title="drawer_cfg2.title"
      :append-to-body="true"
      :modal-append-to-body="false"
      :visible.sync="drawer_cfg2.drawerChild"
      direction="rtl"
      size="80%"
    >
      <div style="text-align: right;">
        <el-button
          size="mini"
          type="primary"
          @click="handleSave"
          v-loading="loading"
        >
          保存
        </el-button>
      </div>
      <el-table
        v-loading="loading"
        v-adaptive="{bottomOffset:0}"
        height="100"
        ref="multipleTable"
        style="width: 100%"
        :data="dicList"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          width="55">
          <template slot-scope="scope">
            {{ scope.$index + 1}}
          </template>
        </el-table-column>
        <el-table-column
          prop="Code"
          label="字典表名"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="Description"
          label="描述"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="ShowOrder"
          label="排序"
          show-overflow-tooltip
        >
        </el-table-column>
      </el-table>
    </el-drawer>
  </div>
</template>

<script>
import { getAssessType, setAssessType, getCriterionDictionaryList, getBasicConfigSelect, addSystemCriterionDictionaryCode, deleteSystemCriterionDictionary } from '@/api/dictionary'
import CriterionDictionaryConfig from './CriterionDictionaryConfig'
export default {
  props: {
    criterionId: {
      type: String,
      required: true
    }
  },
  components: {
    CriterionDictionaryConfig
  },
  data() {
    return {
      list: [],
      total: 0,
      loading: false,
      rowData: {},
      activeName: '0',
      addOrEdit: { visible: false, title: '' },
      preview: { visible: false, title: 'eCRF预览' },
      config: { visible: false, title: '全局配置' },
      drawer_cfg: { drawerChild: false, parentId: '', title: '' },
      drawer_cfg2: { drawerChild: false, parentId: '', title: '' },
      selectedList: [],
      dicList: []
    }
  },
  mounted() {
    this.getList()
    this.getBasicConfigSelect()
  },
  methods: {
    getBasicConfigSelect() {
      getBasicConfigSelect('Reading_eCRF_Criterion').then(res => {
        this.dicList = res.Result
      })
    },
    openChildren(row) {
      this.drawer_cfg = { drawerChild: true, title: row.Code, parent: row }
    },
    toggleSelection(rows) {
      console.log(this.$refs.multipleTable)
      this.$refs.multipleTable.clearSelection();
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {

      }
    },
    handleSelectionChange(val) {
      this.selectedList = val
    },
    getList() {
      this.loading = true
      getCriterionDictionaryList({
        CriterionId: this.criterionId,
        SystemCriterionId: this.criterionId
      }).then(res => {
        this.loading = false
        this.list = res.Result
        console.log(this.$d.GlobalAssessType)
      })
    },
    handleAdd() {
      this.drawer_cfg2 = { drawerChild: true, title: '选择标准字典' }
      this.config.visible = true
      this.$nextTick(() => {
        var a = this.dicList.filter(v => {
          return !!this.list.find(v1 => {
            return v1.Code === v.Code
          })
        })
        console.log(a)
        this.toggleSelection(a)
      })
    },
    handleSave() {
      this.loading = true
      var params = {
        SystemCriterionId: this.criterionId,
        CodeList: this.selectedList.map(v => v.Code) ? this.selectedList.map(v => v.Code) : null,
      }
      addSystemCriterionDictionaryCode(params).then(res => {
        this.$message.success(this.$t('common:message:savedSuccessfully'))
        this.drawer_cfg2.drawerChild = false
        this.loading = false
        this.getList()
      })
      // setAssessType(params).then(res => {
      //   this.loading = false
      //   this.$message.success(this.$t('common:message:savedSuccessfully'))
      //   this.config.visible = false
      //   this.selectedList = []
      //   this.getList()
      // }).catch(() => {
      // })
    },
    handleDelete(row) {
      this.loading = true
      this.$confirm('确定要删除该系统标准字典吗？').then(() => {
        deleteSystemCriterionDictionary({Id: row.Id}).then(res => {
          this.$message.success('删除成功')
          this.loading = false
          this.getList()
        }).catch(() => { this.loading = false })
      })

    }
  }
}
</script>

<style scoped>
/deep/ .el-form-item__content{
  width: calc(100% - 110px);
}
</style>
