<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="container_item">
        <span class="item_text">{{$t('currencypair.currencypair')}}：</span>
        <el-input v-model="query.currencyPair" 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('currency_pair_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('currency_pair_cache')" v-waves class="filter-item" type="primary" icon="el-icon-my-redis1" @click="handleRedis" :loading="redisAbled">
        {{ $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="currencyPair" :label="$t('currencypair.currencypair')"  align="left"></el-table-column>
        <el-table-column prop="referenceCurrency" :label="$t('currencypair.referenceCurrency')" align="left"></el-table-column>
        <el-table-column prop="quoteCurrency" :label="$t('currencypair.quoteCurrency')" align="left" ></el-table-column>
        <el-table-column prop="exchangeRateDps" :label="$t('currencypair.exchangeRateDps')" align="left"></el-table-column>
        <el-table-column prop="rateUnit" :label="$t('currencypair.rateUnit')" align="left"></el-table-column>
        <el-table-column prop="sortLevel" :label="$t('currencypair.sortLevel')" 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('currency_pair_update')" type="text" size="mini" icon="el-icon-my-modify" @click="handleUpdate(row)">
                    {{ $t('table.modify') }}
                    </el-button>
                    <el-button v-if="permission.includes('currency_pair_del')" size="mini" type="text" icon="el-icon-my-del-y" @click="handleDel(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="960px">
        <div class="addflex">
            <div >
                <el-form ref="dataForm"  :model="temp" label-position="right" label-width="121px" >
                  <el-form-item :label="$t('currencypair.currencypair')+'：'" class="require">
                      <el-input v-model="temp.currencyPair" style="width:319px"
                      maxlength="6"
                      @input="temp.currencyPair = temp.currencyPair.replace(/[^A-Z|a-z]/g,'')"
                      @blur="assingCurrency" />
                  </el-form-item>
                  <!-- replace(/[^A-Z|a-z|/|[]]/g,'') -->
                  <el-form-item :label="$t('currencypair.referenceCurrency')+'：'" class="unabled">
                      <el-input v-model="temp.referenceCurrency" style="width:319px" disabled />
                  </el-form-item>
                  <el-form-item :label="$t('currencypair.quoteCurrency')+'：'" class="unabled">
                      <el-input v-model="temp.quoteCurrency" style="width:319px"  disabled/>
                  </el-form-item>
                  <el-form-item :label="$t('currencypair.exchangeRateDps')+'：'" class="require">
                      <el-input v-model="temp.exchangeRateDps" style="width:319px" @input="temp.exchangeRateDps = temp.exchangeRateDps.replace(/[^\d]/g,'')"/>
                  </el-form-item>
                </el-form>
            </div>
            <div >
                <el-form ref="dataForm"  :model="temp" label-position="right" label-width="121px" >
                  <el-form-item :label="$t('currencypair.rateUnit')+'：'" class="require">
                      <el-input v-model="temp.rateUnit"  style="width:319px" @input="temp.rateUnit = temp.rateUnit.replace(/[^\d]/g,'')" maxlength="2"/>
                  </el-form-item>
                  <el-form-item :label="$t('currencypair.sortLevel')+'：'" class="require">
                      <el-input v-model="temp.sortLevel"  style="width:319px"
                      @input="temp.sortLevel = temp.sortLevel.replace(/[^\d]/g,'')"/>
                  </el-form-item>
                  <el-form-item :label="$t('currencytype.maintenanceuser')+'：'" class="unabled">
                      <el-input v-model="temp.updateUserName"   style="width:319px" disabled/>
                  </el-form-item>
                  <el-form-item :label="$t('currencytype.maintenancetime')+'：'" class="unabled">
                      <el-input v-model="temp.updateTime"   style="width:319px" disabled/>
                  </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:{},
      listRole:[],
      tableData:[],
      pageSize:10,
      pageNum:1,
      orgList:[],
      form:{},
      temp: {
        currencyPair:'',
        quoteCurrency:'',
        referenceCurrency:''
      },
      dialogFormVisible: false,
      handleType:1,
      tableHeight: 0,
      chpwdVisible:false,
      redisAbled: false
    }
  },
  created() {
    this.getData()
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 240
    })
  },
  methods: {
    handleRedis() {
      this.redisAbled = true
      http.post(`/share/currencypair/refresh`,{}).then(()=>{
        this.$message.success('缓存成功')
        this.redisAbled = false
      }).catch(() => { this.redisAbled = false })
    },
    getData() {
      http.post(`/share/currencypair/queryList`,{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()
    },

    assingCurrency(){
      let reg = /^[A-Z]/g
      let bool =  reg.test(this.temp.currencyPair)
      !bool && (this.temp.currencyPair =  this.temp.currencyPair && this.temp.currencyPair.toUpperCase())
      let currencyPair = this.temp.currencyPair
      let referenceCurrency = currencyPair && currencyPair.slice(0,3)
      this.$set(this.temp,'referenceCurrency',referenceCurrency)
      let quoteCurrency = currencyPair && currencyPair.slice(-3)
      this.$set(this.temp,'quoteCurrency',quoteCurrency)
    },

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

  },

    handleCreate() {
      this.temp = {
        status: 1
      }
      this.handleType = 1
      this.dialogFormVisible = true
    },
    sbumitData() {
      let { exchangeRateDps,sortLevel } = this.temp
      if ( exchangeRateDps && parseInt(exchangeRateDps) > 10){
        this.$message.error('即期全价精准位数不得大于10')
        this.temp.exchangeRateDps = ''
        return false
      }

      if (sortLevel && parseInt(sortLevel) > 1000){
        this.$message.error('排序最大不能超过10000，请重新输入');
        this.temp.sortLevel = ''
        return false
      }
       let {
            updateUser,
            updateTime,
            ...param } = this.temp
      if(this.handleType==1){
        this.temp.status = 1 //币种的有效状态
        http.post(`/share/currencypair/create`,this.temp).then((res)=>{
            this.$message.success('提交成功！');
            this.getData();
            this.dialogFormVisible = false;
        }).catch(err=>{});
      }else{
        http.post(`/share/currencypair/update`,param).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
    },
    handleReset(){
      this.query = {}
      this.pageNum = 1
      this.getData()
    },

    headerStyle() {
      return 'tableStyle'
    },
  }
}
</script>
<style scoped lang="scss">
  .el-table__header-wrapper{
    border-radius: 10px 10px 0 0;
  }
  .userinfo{
    margin: 20px 50px;
    width: 464px;
    // height: 313px;
    background: rgba(212,235,254,.1);
    border: 1px solid rgba(61,85,150,.1);
    p{
      margin-left: 20px;
      font-size: 14px;
      font-family: "SourceHanSansCN";
      font-weight: 500;
      color: #212121;
      border-left: 3px solid #3D5596;
      padding-left: 8px;
    }
    .input_item{
      width: 319px !important;
    }
    .tab{
      margin: 0 30px 20px;
    }
  }
  .userinfo ::v-deep .el-form-item__label{
    color: #212121 !important;
  }
  .addflex{
    display: flex;
    padding: 30px 30px 0px 30px;
    justify-content: center;
  }
  ::v-deep .el-form-item__content{
      margin-left: 121px !important;
  }
</style>
