<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="container_item">
        <span class="item_text">{{$t('systemvariable.key')}}:</span>
        <el-input v-model="query.configKey" placeholder="" class="filter-item input_item"   />
      </div>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-my-search" @click="handleSearch">
        {{ $t('table.search') }}
      </el-button>
       <el-button v-waves class="filter-item" type="primary" icon="el-icon-my-reset" @click="handleReset">
        {{ $t('table.reset') }}
      </el-button>
      <el-button v-if="permission.includes('sys_variable_add')" v-waves class="filter-item" type="primary" icon="el-icon-my-add" @click="handleCreate">
        {{ $t('table.add') }}
      </el-button>
      <el-button v-if="permission.includes('sys_variable_cache')" v-waves class="filter-item" type="primary" icon="el-icon-my-redis1" @click="handleRedis" :disabled="redisAbled ? true : false">
         {{ $t('table.cache') }} </el-button>
    </div>
    <div class="wrapper_table">
      <el-table
        :data="tableData"
        fit
        stripe
        highlight-current-row
        style="width: 100%;"
        :header-cell-class-name="headerStyle"
        :max-height="tableHeight"
      >
        <el-table-column prop="configKey" :label="$t('systemvariable.key')"  ></el-table-column>
        <el-table-column prop="configValue" :label="$t('systemvariable.value')" show-overflow-tooltip></el-table-column>
        <el-table-column prop="configDescribe" :label="$t('systemvariable.instructions')"  show-overflow-tooltip></el-table-column>
        <el-table-column :label="$t('table.actions')"  width="120"  class-name="small-padding ">
          <template slot-scope="{row}">
              <div class="flex_d">
                    <el-button v-if="permission.includes('sys_variable_update')" type="text" size="mini" icon="el-icon-my-modify" @click="handleUpdate(row)">
                    {{ $t('table.modify') }}
                    </el-button>
                    <!-- <el-button  size="mini" type="text" icon="el-icon-my-del-y" @click="handleChangeDel(row)">
                    {{ $t('table.delete') }}
                    </el-button> -->
              </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" @pagination="getData" />
    <el-dialog
      :title="handleType==1?'新增':'修改'"
      :visible.sync="dialogFormVisible"
      v-el-drag-dialog
      :close-on-click-modal="false"
      width="460px">
        <div class="addflex">
            <div >
                <el-form ref="dataForm"  :model="temp" label-position="right" label-width="100px" >
                  <el-form-item :label="$t('systemvariable.key')+'：'"  :class="handleType === 2 ? 'unabled':'require'">
                      <el-input v-model="temp.configKey" style="width:319px"  :disabled="handleType === 2"/>
                  </el-form-item>
                  <el-form-item :label="$t('systemvariable.value')+'：'" class="require">
                      <el-input v-model="temp.configValue" style="width:319px"  />
                  </el-form-item>
                  <el-form-item :label="$t('systemvariable.instructions')+'：'" class="require">
                      <el-input v-model="temp.configDescribe" style="width:319px"  />
                  </el-form-item>
                </el-form>
            </div>
        </div>
      <div slot="footer" class="dialog-footer" style="padding-right:26px">
        <el-button v-waves class="filter-item flex_align" type="primary" icon="el-icon-my-tijiao" @click="sbumitData">
            {{ $t('table.sbumit') }}
        </el-button>
        <el-button v-waves class="filter-item flex_align" type="primary" icon="el-icon-my-quxiao" @click="dialogFormVisible=false">
            {{ $t('table.cancel') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import elDragDialog from '@/directive/el-drag-dialog'
import { mapGetters } from 'vuex'
import http from '@/api/http'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: '',
  components: { Pagination },
  directives: { waves, elDragDialog },
  computed: {
    ...mapGetters([
      'permission',
    ])
  },
  data() {
    return {
      total: 0,
      query:{},
      tableData:[],
      pageSize:10,
      pageNum:1,
      dialogFormVisible: false,
      handleType:1,//1新增 2修改
      tableHeight: 0,
      temp:{
        configKey:'',
        configValue:'',
        configDescribe:''
      },
      redisAbled:false //禁用redis
    }
  },
  created() {
    this.getData()
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 240
    })
  },
  methods: {
    getData() {
      http.post(`/sys/config/pageList`,{cpage:this.pageNum,pageSize:this.pageSize,...this.query}).then((res)=>{
          this.tableData = res.data.list;
          this.total = res.data.total || 0;
      }).catch(err=>{});
    },
    handleSearch(){
      this.pageNum = 1
      this.getData()
    },
    handleReset(){
      this.query = {}
      this.pageNum = 1
      this.getData()
    },
    refresh(){
      http.post(`/share/currency/refresh`).then(res=>{})
    },

    // handleChangeDel(row){
    //    this.$confirm('是否删除该信息, 是否继续?', '提示', {
    //       confirmButtonText: '确定',
    //       cancelButtonText: '取消',
    //       type: 'warning'
    //     }).then(() => {
    //       let params = {
    //         id: row.id
    //       }
    //       http.post(`/share/currency/delete`, params).then( res=>{
    //       this.$message.success(res.msg.info);
    //       this.getData()
    //       this.refresh()
    //       })
    //     }).catch(() => {});
    // },

    handleCreate() {
      this.temp = { }
      this.handleType = 1 //1新增
      this.dialogFormVisible = true
    },
    sbumitData() {
        let {configKey,configValue,configDescribe,id} = this.temp
         if(configKey ==='' || configKey === undefined){
          this.$message.error('配置key不能为空，请输入！')
          return false
        }
         if(configValue ==='' || configValue === undefined){
          this.$message.error('配置value不能为空，请输入！')
          return false
        }
        if(configDescribe ==='' || configDescribe === undefined){
          this.$message.error('说明不能为空，请输入！')
          return false
        }
        let params = {
            configKey: configKey,
            configValue: configValue,
            configDescribe: configDescribe
        }
      if(this.handleType ===1){
        http.post(`/sys/config/create`,params ).then((res)=>{
            this.$message.success('提交成功！');
            this.getData();
            this.dialogFormVisible = false;
        }).catch(err=>{});
      }else{
        http.post(`/sys/config/update`,Object.assign(params,{id:id})).then((res)=>{
            this.$message.success('提交成功！');
            this.getData();
            this.dialogFormVisible = false;
        }).catch(err=>{});
      }
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.handleType = 2
      this.dialogFormVisible = true
    },

    // 刷新Redis的SysConfig
    handleRedis(){
      this.redisAbled = true
      http.post(`/sys/config/syncSysConfig`,{}).then(res=>{
        if (res.msg.success) {
          this.redisAbled = false
        }
      })
    },

    headerStyle() {
      return 'tableStyle'
    },
  }
}
</script>
<style scoped lang="scss">
  .el-table__header-wrapper{
    border-radius: 10px 10px 0 0;
  }
  .addflex{
    display: flex;
    padding: 30px 30px 0px 30px;
    justify-content: center;
  }

  .filter-container{
    button{
      &.is-disabled{
        background: url('../../assets/images/add_hui.png') no-repeat !important;
        background-size: 100% 100% !important;
      }
    }
  }
</style>
