<template>
  <div class="app-container">
    <div v-loading="loading" class="white-box">
      <el-table :data="conversionsList" stripe style="width: 100%;">
        <el-table-column prop="NAME" :label="$t('transportationConfig.countryName')" />
        <el-table-column prop="currency.CODE" :label="$t('exchangeRateConfig.currencyType')" />
        <el-table-column prop="free_shipping" :label="$t('transportationConfig.freeShipping')" min-width="90px;" />
        <el-table-column prop="freight" :label="$t('transportationConfig.free')" min-width="90px;" />
        <el-table-column :label="$t('common.action')" min-width="100px;">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" plain @click="edit(scope.row)">{{ $t('common.edit') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 编辑	-->
    <el-dialog v-loading="editLoading" :title="$t('config.modifyShippingRates')" :visible.sync="dialog" :width="screenWidth" style="margin-top: -80px">
      <el-form ref="editForm" :model="editForm" :label-position="labelPosition" label-width="130px" style="width: 100%; margin-top: -30px; margin-bottom: -15px;">
        <el-row :gutter="3">
          <el-col :xs="24" :sm="24" :lg="12">
            <el-input v-show="false" v-model="editForm.ID" size="small" type="text" />
            <el-form-item :label="$t('transportationConfig.freeShipping')" prop="freeShipping" style="margin-bottom: 10px; width: 100%;">
              <el-input-number v-model.trim="editForm.freeShipping" :precision="0" :controls="false" :min="0" size="small" style="min-width: 300px;" />
            </el-form-item>
            <el-form-item :label="$t('transportationConfig.free')" prop="free" style="margin-bottom: 10px; width: 100%;">
              <el-input-number v-model.trim="editForm.freight" :precision="0" :controls="false" :min="0" size="small" style="min-width: 300px;" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item style="margin-bottom: 15px;">
          <el-button type="warning" size="mini" @click="dialog = false">{{ $t('table.cancel') }}</el-button>
          <el-button type="primary" size="mini" @click="editSubmit">{{ $t('table.confirm') }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import {
  fetchTransportationList, setTransportation
} from '@/api/config'
import { getScreenWidth } from '@/utils'

export default {
  name: 'transportation',
  data() {
    return {
      form: {
        CONFIG_NAME: '',
        VALUE: 0,
        TITLE: ''
      },
      loading: true,
      submitButtonStat: false,
      synchronize: false,
      screenWidth: getScreenWidth() > 600 ? '500px' : getScreenWidth() + 'px',
      labelPosition: getScreenWidth() > 600 ? 'right' : 'top',
      conversionsList: [],
      editLoading: false,
      editForm: {
        countryId: "",
        freight: "",
        freeShipping: "",
      },
      dialog: false,

    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.loading = true
      fetchTransportationList().then(response => {
        this.conversionsList = response
        setTimeout(() => {
          this.loading = false
        }, 0.5 * 1000)
      })
    },
    edit(data){
      this.editForm = {
            countryId: data.ID,
            freight: data.freight,
            freeShipping: data.free_shipping,
      }
      this.dialog = true
    },
    editSubmit(){
      setTransportation(this.editForm).then(response => {
        this.$message({
          message: response.data,
          type: 'success'
        })
        this.dialog = false
        this.fetchData()
      }).catch(error => {
        this.$message({
          message: error,
          type: 'warning'
        })
      })
    },
  }
}
</script>

<style scoped>
.app-main {
	padding: 15px;
}
.app-container {
	padding: 0;
}
.white-box {
	padding: 15px;
}
.form-page {
	width: 100%;
}
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
</style>
