<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" size="mini" class="base-search-form">
        <el-form-item label="字典表名称:">
          <el-input v-model="searchData.Code" clearable style="width:120px;" />
        </el-form-item>
        <el-form-item label="字典分组:">
          <el-select v-model="searchData.ConfigTypeId" placeholder="字典分组" clearable size="small">
            <el-option v-for="item of basicDicList" :key="item.Id" :value="item.Id" :label="item.Code" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleReset">Reset</el-button>
          <el-button type="primary" @click="handleSearch">Search</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"
      @sort-change="handleSortByColumn"
    >
      <el-table-column type="index" width="60" />
      <el-table-column
        prop="Code"
        label="字典表名称"
        sortable="custom"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="ConfigType"
        label="字典分组"
        sortable="custom"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="Description"
        label="描述"
        sortable="custom"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="ShowOrder"
        label="显示顺序"
        sortable="custom"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column label="是否可用" width="100" fixed="right">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.IsEnable"
            :active-value="true"
            :inactive-value="false"
            @change="(event) => {return switchChange(event, scope.row)}"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="success"
            size="mini"
            @click="handleChild(scope.row)"
          >
            子项
          </el-button>
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination" style="text-align: right;margin-top:5px;">
      <pagination :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize" @pagination="getList" />
    </div>
    <DictionaryTypeConfigForm ref="DictionaryTypeConfigForm" :basic-dic-list="basicDicList" @getList="getList" />
    <el-drawer
      :title="drawer_cfg.title"
      :visible.sync="drawer_cfg.drawerChild"
      direction="rtl"
      size="80%"
    >
      <DictionaryChild v-if="drawer_cfg.drawerChild" ref="DictionaryChild" :parent="drawer_cfg.parent" />
    </el-drawer>
  </box-content>
</template>
<script>
import { getBasicDicList, deleteDictionary, addOrUpdateBasicDic } from '@/api/dictionary'
import DictionaryTypeConfigForm from '../components/DictionaryTypeConfigForm'
import DictionaryChild from '../components/DictionaryChild'
import Pagination from '@/components/Pagination'
import BoxContent from '@/components/BoxContent'

const searchDataDefault = () => {
  return {
    Asc: true,
    SortField: '',
    Code: '',
    KeyName: '',
    DataTypeEnum: 2,
    PageIndex: 1,
    PageSize: 20,
    ConfigTypeId: null
  }
}
export default {
  components: { BoxContent, Pagination, DictionaryTypeConfigForm, DictionaryChild },
  data() {
    return {
      searchData: searchDataDefault(),
      basicDicList: [],
      list: [],
      total: 0,
      loading: false,
      drawer_cfg: { drawerChild: false, parentId: '', title: '' },
      rowData: {},
      model_cfg: { visible: false, showClose: true, width: '600px', title: '', appendToBody: true }
    }
  },
  mounted() {
    this.getList()
    this.getDictionaryTypeConfig()
  },
  methods: {
    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()
    },
    handleChild(row) {
      this.drawer_cfg = { drawerChild: true, parentId: row.Id, title: row.Code, parent: row }
    },
    handleSetting() {
      this.$router.push({ path: '/dictionary/newDictionary/setting' })
    },
    switchChange(event, item) {
      this.loading = true
      addOrUpdateBasicDic(item).then(res => {
        this.$message.success('Saved successfully!')
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleEdit(row) {
      this.$nextTick(() => {
        this.$refs['DictionaryTypeConfigForm'].openDialog('DictionaryTypeConfig', row)
      })
    },
    handleAdd() {
      this.$nextTick(() => {
        this.$refs['DictionaryTypeConfigForm'].openDialog('DictionaryTypeConfig', { DataTypeEnum: 2 })
      })
    },
    handleDelete(row) {
      this.$confirm(this.$t('trials:uploadedDicoms:message:deleteMes'), {
        type: 'warning',
        distinguishCancelAndClose: true,

      })
        .then(() => {
          this.loading = true
          deleteDictionary(row.Id)
            .then(res => {
              this.loading = false
              if (res.IsSuccess) {
                this.list.splice(this.list.findIndex(item => item.Id === row.Id), 1)
                this.$message.success(this.$t('common:message:deletedSuccessfully'))
              }
            }).catch(() => { this.loading = false })
        })
    },
    getList() {
      this.loading = true
      getBasicDicList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { this.loading = false })
    },
    getDictionaryTypeConfig() {
      getBasicDicList({ DataTypeEnum: 0, SortField: 'Code', PageIndex: 1, PageSize: 500 }).then(res => {
        this.basicDicList = res.Result.CurrentPageData
        console.log(this.basicDicList)
      }).catch(() => { })
    },
    // 查询
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    }
  }
}
</script>
