<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="container_item">
        <span class="item_text">{{$t('errorcode.codeValue')}}:</span>
        <el-input v-model="query.code" placeholder="" class="filter-item input_item"   />
      </div>
      <div class="container_item">
        <span class="item_text">{{$t('errorcode.countryCode')}}:</span>
        <el-input v-model="query.countryCode" 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('error_code_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('error_code_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="code" :label="$t('errorcode.codeValue')"  align="left"></el-table-column>
        <el-table-column prop="countryCode" :label="$t('errorcode.countryCode')" align="left"></el-table-column>
        <el-table-column prop="codeDescribe" :label="$t('errorcode.codeDescribe')" align="left" ></el-table-column>
        <el-table-column :label="$t('table.actions')" align="left" width="120"  class-name="small-padding ">
          <template slot-scope="{row}">
              <div class="flex_d">
                  <el-button v-if="permission.includes('error_code_update')" type="text" size="mini" icon="el-icon-my-modify" @click="handleUpdate(row)">
                  {{ $t('table.modify') }}
                  </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('errorcode.codeValue')+'：'"  :class="handleType === 2 ? 'unabled':'require'">
                      <el-input v-model="temp.code" style="width:319px"  :disabled="handleType === 2" maxlength="100"/>
                  </el-form-item>
                  <el-form-item :label="$t('errorcode.countryCode')+'：'" class="require">
                      <el-input v-model="temp.countryCode" style="width:319px" maxlength="20" />
                  </el-form-item>
                  <el-form-item :label="$t('errorcode.codeDescribe')+'：'" class="require">
                      <el-input v-model="temp.codeDescribe" style="width:319px"  maxlength="255"/>
                  </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,
      temp: {
        code:'',
        countryCode:'',
        codeDescribe:''
      },
      dialogFormVisible: false,
      handleType:1,//1新增 2修改
      tableHeight: 0,
      redisAbled:false //禁用redis
    }
  },
  created() {
    this.getData()
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 240
    })
  },
  methods: {
    getData() {
      http.post(`/sys/code/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()
    },
    // 刷新Redis的SysConfig
    handleRedis(){
      this.redisAbled = true
      http.post(`/sys/code/syncSysCode`,{}).then(res=>{
        if (res.msg.success) {
          this.redisAbled = false
        }
      })
    },

    handleCreate() {
      Object.keys(this.temp).forEach(item=>{
        this.temp[item] = undefined
      })
      this.handleType = 1 //1新增
      this.dialogFormVisible = true
    },
    sbumitData() {
      let { code,countryCode ,codeDescribe } = this.temp
      if (code === '' || code === undefined) {
        this.$message.error('编码值不能为空')
        return false
      }
       if (countryCode === '' || countryCode === undefined) {
        this.$message.error('国家编码不能为空')
        return false
      }
      if (codeDescribe === '' || codeDescribe === undefined) {
        this.$message.error('编码描述不能为空')
        return false
      }

      if(this.handleType ===1){
        http.post(`/sys/code/create`,{ ...this.temp } ).then((res)=>{
            this.$message.success('提交成功！');
            this.getData();
            this.dialogFormVisible = false;
        }).catch(err=>{});
      }else{
        http.post(`/sys/code/update`,{ ...this.temp }).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
    },

    headerStyle() {
      return 'tableStyle'
    },
  }
}
</script>
<style scoped lang="scss">
  .addflex{
    display: flex;
    padding: 30px 30px 0px 30px;
    justify-content: center;
  }
  .item_text{
    padding-right: 8px;
  }
  ::v-deep .el-dialog__body{
    margin-bottom:0 !important;
  }
</style>
