<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" size="mini" class="base-search-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
        v-if="!~$route.path.indexOf('select')"
        prop="Code"
        label="字典键值"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="ValueCN"
        label="中文值"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="Value"
        label="英文值"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="ChildGroup"
        label="分组"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="ShowOrder"
        label="显示顺序"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="Description"
        label="描述"
        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="danger"
            size="mini"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <DictionaryChildAddOrUpdateForm ref="DictionaryChildAddOrUpdateForm" @getList="getList" />
  </box-content>
</template>

<script>
import { deleteDictionary, addOrUpdateBasicDic, getDictionaryChildList } from '@/api/dictionary'
import BoxContent from '@/components/BoxContent'
import DictionaryChildAddOrUpdateForm from './DictionaryChildAddOrUpdateForm'

const searchDataDefault = () => {
  return {
    parentId: ''
  }
}
export default {
  components: { BoxContent, DictionaryChildAddOrUpdateForm },
  props: {
    parent: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      searchData: searchDataDefault(),
      basicDicList: [],
      list: [],
      total: 0,
      loading: false,
      drawerChild: false,
      rowData: {},
      childList: [],
      model_cfg: { visible: false, showClose: true, width: '600px', title: '', appendToBody: true }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleChild(row) {
      this.drawerChild = true
      this.getDictionaryChildList(row.Id)
    },
    getDictionaryChildList(id) {
      getDictionaryChildList(id).then((res) => {
        this.childList = res.Result.CurrentPageData
      }).catch(() => {
        this.loading = false
      })
    },
    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['DictionaryChildAddOrUpdateForm'].openDialog('DictionaryChild Edit', row, { DataTypeEnum: this.parent.DataTypeEnum })
      })
    },
    handleAdd() {
      this.$nextTick(() => {
        this.$refs['DictionaryChildAddOrUpdateForm'].openDialog('DictionaryChild Add', {}, { ConfigTypeId: this.parent.ConfigTypeId, parentId: this.parent.Id, DataTypeEnum: this.parent.DataTypeEnum })
      })
    },
    handleDelete(row) {
      this.$confirm('Sure to delete?', {
        type: 'warning',
        distinguishCancelAndClose: true,
        confirmButtonText: 'Ok',
        cancelButtonText: 'Cancel'
      })
        .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('Deleted successfully!')
              }
            }).catch(() => { this.loading = false })
        })
    },
    // 获取匿名化配置信息
    getList() {
      this.loading = true
      getDictionaryChildList(this.parent.Id).then(res => {
        this.loading = false
        this.list = res.Result
      }).catch(() => { this.loading = false })
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    }
  }
}
</script>

<style scoped>

</style>
