<template>
  <div class="dicttype-container">
    <el-tabs v-model="activeName" type="border-card">
      <el-tab-pane label="分类列表" name="1">
        <el-form :inline="true" :model="searchParam" class="demo-form-inline">
          <el-form-item label="关键词">
            <el-input clearable v-model="searchParam.keywords" size="small" placeholder="ID或名称或标示"
                      style="width: 200px"/>
          </el-form-item>
          <el-form-item label="添加人">
            <el-select clearable v-model="searchParam.createUser" size="small" placeholder="-请选择-"
                       style="width: 150px">
              <el-option v-for="item in addPerson" :key="item.id" :label="item.username" :value="item.id"/>
            </el-select>
          </el-form-item>
          <el-form-item label="添加时间">
            <el-date-picker
              placeholder="选择的时间"
              style="width: 220px"
              size="small"
              clearable
              value-format="yyyy-MM-dd"
              v-model="searchParam.time"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button :loading="tableLoading" @click="search" size="small" icon="el-icon-search" type="primary">搜索</el-button>
            <el-button @click="initSearchParam();search()" icon="el-icon-refresh-right"
                       type="primary" size="small"
                       style="background-color: #fff; border: 1px solid #ccc ; color: #666">清空
            </el-button>
          </el-form-item>
        </el-form>
        <el-table stripe ref="table" :height="tableHeight" v-loading="tableLoading"
                  :data="dicttypeList"
                  border row-key="id">
          <el-table-column show-overflow-tooltip prop="id" label="ID" align="center" width="100"/>
          <el-table-column show-overflow-tooltip prop="sort" label="排序" align="center" width="100"/>
          <el-table-column show-overflow-tooltip prop="name" label="名称" align="center"/>
          <el-table-column show-overflow-tooltip prop="nid" label="标识符" align="center"/>
          <el-table-column show-overflow-tooltip prop="createUserName" label="添加人" align="center"/>
          <el-table-column show-overflow-tooltip prop="formatCreateTime" label="添加时间" align="center" width="160"/>
          <el-table-column v-if="isAdmin||(btnRule.filter(value=>value=='edit'||value=='drop').length>0)"  width="100" label="操作" >
            <template slot-scope="scoped">
              <el-row>
                <el-tooltip v-if="isAdmin||btnRule.some(btn=>btn=='edit')" class="item" effect="dark" content="编辑"
                            placement="bottom">
                  <el-button @click="handleEdit(scoped.row)" type="primary" size="mini" icon="el-icon-edit"/>
                </el-tooltip>
                <el-tooltip v-if="isAdmin||btnRule.some(btn=>btn=='drop')" class="item" effect="dark" content="删除"
                            placement="bottom">
                  <el-button @click="handleDelete(scoped.row)" type="danger" size="mini"
                             class="el-icon-delete"></el-button>
                </el-tooltip>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="page-current">
          <el-pagination
            background
            :current-page="currentPage"
            :page-size="pageSize"
            prev-text="上一页"
            next-text="下一页"
            layout="total, prev, pager, next, jumper"
            style="display: inline-block;"
            @current-change="handleCurrentChange"
            :total="count"/>
        </div>
      </el-tab-pane>
      <el-tab-pane v-if="isAdmin||btnRule.some(btn=>btn=='add')" label="添加分类" name="2">
        <dictForm :ruleForm="ruleForm" style="width: 50%" v-if="activeName==2" @submit="submit"></dictForm>
      </el-tab-pane>
    </el-tabs>
    <el-dialog :close-on-press-escape="false" :close-on-click-modal="false" center :visible.sync="addDialogVisible" title="编辑配置">
      <dictForm ref="dialogForm" :ruleForm="ruleForm" style="width: 100%" @submit="submit" @onBack="onBack"></dictForm>
    </el-dialog>
  </div>
</template>

<script>
  import dicttypeApi from '@/api/system/dicttype'
  import adminApi from '@/api/permission/admin'
  import dictForm from './dicttypeForm'

  export default {
    name: "dicttype",
    data() {
      return {
        activeName: '1',
        addPerson: [],
        searchParam: {
          keywords: '',
          createUser: '',
          time: ''
        },
        btnRule: [],
        isAdmin: false,
        addDialogVisible: false,
        ruleForm: {
          name: '',
          nid:'',
          sort: 1,
        },
        currentPage: 1,
        pageSize: 10,
        count: 0,
        dicttypeList: [],
        tableLoading: false,
        tableHeight: document.body.clientHeight - 290,
      }
    },
    components: {dictForm},
    created() {
      const {btnRule, isAdmin} = this.$store.state.user
      this.btnRule = btnRule
      this.isAdmin = isAdmin
      this.pageSize = parseInt((document.body.clientHeight - 344) / 50)
    },
    mounted() {
      this.tableLoading = true
      this.getAllList()
      this.getAdminList()
    },
    watch: {
      activeName() {
        this.initRuleForm()
      }
    },
    methods: {
      handleDelete(data) {
        this.$confirm('您确定要删除配置吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delBtnLoading = true
          dicttypeApi.drop(data.id).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
            this.delBtnLoading = false
            this.tableLoading = true
            this.getAllList()
          }).catch(e => {
            this.delBtnLoading = false
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      handleEdit(data){
        this.initRuleForm()
        this.addDialogVisible=true
        this.$nextTick(function () {
          try {
            this.ruleForm = JSON.parse(JSON.stringify(data))
            this.$refs.dialogForm.$refs['ruleForm'].resetFields()
          } catch (e) {
          }
        })
      },
      initRuleForm() {
        this.ruleForm = {
          name: '',
          nid:'',
          sort: 1
        }
      },
      getAllList() {
        let searchParam = JSON.parse(JSON.stringify(this.searchParam))
        if (searchParam.time && searchParam.time.length > 0) {
          searchParam.startTime = new Date(searchParam.time[0]).getTime() / 1000
          searchParam.endTime = new Date(searchParam.time[1]).getTime() / 1000
          delete searchParam.time
        }
        searchParam.page = this.currentPage
        searchParam.pageSize = this.pageSize
        dicttypeApi.getDicttypeList(searchParam).then(res => {
          this.dicttypeList = res.data.records
          this.count = res.data.total
          this.tableLoading = false
        }).catch(err => {
          this.dicttypeList = []
          this.tableLoading = false
        })
      },
      search() {
        this.currentPage = 1
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getAllList()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getAllList()
      },
      initSearchParam() {
        this.searchParam = {
          keywords: '',
          createUser: '',
          time: ''
        }
      },
      getAdminList() {
        adminApi.getAdminList().then(res => {
          let addPerson = res.data ? res.data : []
          this.addPerson = addPerson.filter(item => item.status == 1)
        }).catch(e => {
          this.addPerson = []
        })
      },
      submit(type, group) {
        if (type == 1) {
          this.search()
        } else {
          this.tableLoading=true
          this.getAllList()
          this.$refs.table.bodyWrapper.scrollTop = 0;
          this.addDialogVisible = false
        }
      },
      onBack() {
        this.initRuleForm()
        this.addDialogVisible = false
      }
    }
  }
</script>

<style lang="less">
  .dicttype-container {
    width: 100%;
  }
</style>
