<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="container_item">
        <span class="item_text">{{$t('risk.countryCode')}}：</span>
        <el-input v-model="query.countryCode" 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('risk_add')" v-waves class="filter-item" type="primary" icon="el-icon-my-add" @click="handleCreate">
        {{ $t('table.add') }}
      </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="countryCode" :label="$t('risk.countryCode')"  align="left"></el-table-column>
        <el-table-column prop="chineseDescription" :label="$t('risk.chineseDescription')" align="left" show-overflow-tooltip></el-table-column>
        <el-table-column prop="englishDescription" :label="$t('risk.englishDescription')" align="left" show-overflow-tooltip></el-table-column>
        <el-table-column prop="quotaDisplayStr" :label="$t('risk.quotaDisplay')" 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('risk_update')" type="text" size="mini" icon="el-icon-my-modify" @click="handleUpdate(row)">
                    {{ $t('table.modify') }}
                    </el-button>
                    <el-button v-if="permission.includes('risk_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('risk.countryCode')+'：'" class="require">
                      <el-input v-model="temp.countryCode" style="width:319px"  maxlength="10"
                      @input="temp.countryCode = temp.countryCode.replace(/[^A-Z|a-z]/g,'')"/>
                  </el-form-item>
                  <el-form-item :label="$t('risk.chineseDescription')+'：'" class="require">
                      <el-input v-model="temp.chineseDescription" style="width:319px"  maxlength="100"/>
                  </el-form-item>
                  <el-form-item :label="$t('risk.englishDescription')+'：'" class="require">
                      <el-input v-model="temp.englishDescription" style="width:319px" maxlength="100"/>
                  </el-form-item>
                </el-form>
            </div>
            <div >
                <el-form ref="dataForm"  :model="temp" label-position="right" label-width="121px" >
                  <el-form-item :label="$t('risk.quotaDisplay')+'：'" class="require">
                      <el-switch v-model="temp.quotaDisplay" :class="temp.quotaDisplay ? 'open':'close'"></el-switch>
                  </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:{},
      tableData:[],
      pageSize:20,
      pageNum:1,
      form:{},
      temp: {
        countryCode:'',
        chineseDescription:'',
        englishDescription:'',
        quotaDisplay:'',
        updateUserName:'',
        updateTime:''
      },
      dialogFormVisible: false,
      handleType:1,
      tableHeight: 0,
    }
  },
  created() {
    this.getData()
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 240
    })
  },
  methods: {
    getData() {
      let params = {
        cpage: this.pageNum,
        pageSize: this.pageSize,
        ...this.query
      }
      http.post(`/share/CountryCode/queryList`,params).then((res)=>{
          this.tableData = res.data.list.filter(item=>{
            return item.status === 1
          })
          this.total = res.data.total || 0;
          this.tableData.forEach(item=>{
            item.quotaDisplayStr =  item.quotaDisplay === 1 ? '是' : '否'
            item.quotaDisplay =  item.quotaDisplay === 1 ? true : false
          })

      }).catch(err=>{});
    },
    handleSearch(){
      this.pageNum = 1
      this.getData()
    },
    handleReset(){
      this.query = {}
      this.pageNum = 1
      this.getData()
    },

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

  },

    handleCreate() {
      Object.keys(this.temp).forEach(item=>{
        this.temp[item] = undefined
      })
      this.handleType = 1
      this.dialogFormVisible = true
    },
    sbumitData() {
      delete this.temp.quotaDisplayStr
      this.temp.quotaDisplay = this.temp.quotaDisplay === true ? 1 : 0
      let { countryCode, chineseDescription, englishDescription} = this.temp
      if (countryCode === '' || countryCode === undefined){
        this.$message.error('国家代码不能为空')
        return false
      } else if(chineseDescription === '' || chineseDescription === undefined){
        this.$message.error('国家中文名称不能为空')
        return false
      } else if(englishDescription === '' || englishDescription === undefined){
        this.$message.error('国家英文名称不能为空')
        return false
      }
      if(this.handleType==1){
        this.temp.status = 1 //币种的有效状态
        http.post(`/share/CountryCode/create`,Object.assign(this.temp,{status: 1})).then((res)=>{
            this.$message.success('提交成功！');
            this.getData();
            this.dialogFormVisible = false;
        }).catch(err=>{});
      }else{
        http.post(`/share/CountryCode/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">
  .el-table__header-wrapper{
    border-radius: 10px 10px 0 0;
  }
  .addflex{
    display: flex;
    padding: 30px 30px 0px 30px;
    justify-content: center;
  }
  ::v-deep .el-form-item__content{
      margin-left: 121px !important;
  }

</style>
