<template>
  <div class="config-box">
    <el-form :inline="true" :model="searchParam" class="demo-form-inline head-form">
      <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.type" size="small" placeholder="-请选择-"
                   style="width: 150px">
          <el-option v-for="value in typeList" :key="value" :label="value" :value="value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="输入类型">
        <el-select clearable v-model="searchParam.inputType" size="small" placeholder="-请选择-"
                   style="width: 150px">
          <el-option v-for="value in inputTypeList" :key="value" :label="value" :value="value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select clearable v-model="searchParam.status" size="small" placeholder="-请选择-"
                   style="width: 150px">
          <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </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="configList"
              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="title" label="配置标题" align="center"/>
      <el-table-column show-overflow-tooltip prop="name" label="配置名称" align="center"/>
      <el-table-column show-overflow-tooltip prop="value" label="配置值" align="center"/>
      <el-table-column show-overflow-tooltip prop="type" label="配置类型" align="center"/>
      <el-table-column show-overflow-tooltip prop="inputTypeText" label="输入类型" align="center"/>
      <el-table-column show-overflow-tooltip prop="status" label="状态" align="center" width="120">
        <template slot-scope="{row}">
          {{ row.status == 1 ? '正常' : '禁用' }}
          <el-switch v-if="isAdmin||btnRule.some(btn=>btn=='status')" v-model="row.status" :active-value="1" :inactive-value="2" size="small"
                     @change="changeStatus(row)"/>
        </template>
      </el-table-column>
      <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="edit(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>
  </div>
</template>

<script>
  import configApi from '@/api/system/config'
  export default {
    name: "configBox",
    data() {
      return {
        searchParam:{
          group:'',
          keywords:'',
          type:'',
          inputType:'',
          status:'',
          createUser:'',
          time:''
        },
        typeList:['string','text','int','bool','array','datetime','date','file'],
        inputTypeList:['text','textarea','switch','radio','checkbox','select'],
        statusList:[
          {value:1,label:'正常'},
          {value:2,label:'禁用'}
        ],
        tableLoading:false,
        currentPage:1,
        count:0,
        tableHeight: document.body.clientHeight - 340,
        configList:[]
      }
    },
    props: {
      addPerson: {
        type: Array,
        default () {
          return [];
        }
      },
      group: {
        type: String,
        default () {
          return '';
        }
      },
      btnRule: {
        type: Array,
        default () {
          return [];
        }
      },
      isAdmin: {
        type: Boolean,
        default () {
          return false;
        }
      },
    },
    created() {
      this.searchParam.group = parseInt(this.group)
      this.pageSize = parseInt((document.body.clientHeight - 384) / 50)
    },
    mounted() {
      this.tableLoading=true
      this.getAllList()
    },
    methods: {
      handleDelete(data) {
        this.$confirm('您确定要删除配置吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delBtnLoading = true
          configApi.drop({id: 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: '已取消删除'
          })
        })
      },
      changeStatus(data) {
        var content = ''
        if (data.status === 1) {
          content = '确定要启用吗？'
        } else {
          content = '确定要禁用吗？'
        }
        this.$confirm(content, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = {
            status: data.status == 1 ? '2' : '1',
            id: data.id
          }
          this.tableLoading = true
          configApi.setStatus(params).then(res => {
            this.$message({
              type: 'success',
              message: '操作成功！'
            })
            this.getAllList()
          }).catch(e => {
            let configList = JSON.parse(JSON.stringify(this.configList))
            for (let i = 0; i < configList.length; i++) {
              if (configList[i].id == data.id) {
                configList[i].status = data.status == 1 ? 2 : 1
              }
            }
            this.configList = Object.assign([], configList)
            this.tableLoading = false
          })
        }).catch(() => {
          let configList = JSON.parse(JSON.stringify(this.configList))
          for (let i = 0; i < configList.length; i++) {
            if (configList[i].id == data.id) {
              configList[i].status = data.status == 1 ? 2 : 1
            }
          }
          this.configList = Object.assign([], configList)
          this.$message({
            type: 'info',
            message: '已取消该操作！'
          })
        })
      },
      edit(row){
        this.$emit('edit',row)
      },
      initSearchParam() {
        this.searchParam = {
          group:'',
          keywords:'',
          type:'',
          inputType:'',
          status:'',
          createUser:'',
          time:'',
          pageSize:10,
        }
        this.searchParam.group = parseInt(this.group)
      },
      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
        configApi.getConfList(searchParam).then(res => {
          this.configList = res.data.records
          this.count = res.data.total
          this.tableLoading = false
        }).catch(err => {
          this.configList = []
          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()
      }
    }
  }
</script>

<style lang="less">
  .config-box{
    .page-current{
      text-align: center;
      padding: 15px 5px;
      padding-bottom: 0px;
    }
  }
</style>
