<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input
        v-model.trim="listQuery.countryName"
        clearable
        :placeholder="$t('pages.countryCode.filter.countryName')"
        style="width: 200px;"
        class="filter-item"
      />
      <el-input
        v-model.trim="listQuery.aliasName"
        clearable
        :placeholder="$t('pages.countryCode.filter.aliasName')"
        style="width: 200px;"
        class="filter-item"
      />
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch"
      >
        {{ $t('common.search') }}
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('pages.countryCode.loading')"
      fit
      highlight-current-row
    >
      <el-table-column prop="countryName" :label="$t('pages.countryCode.table.countryName')" align="center" width="200">
        <template slot-scope="scope">
          <div v-if="scope.row.top === 1" class="mark-block mark-danger">{{ $t('pages.countryCode.word.top') }}</div>
          <div>{{ scope.row.countryName }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="aliasName" :label="$t('pages.countryCode.table.aliasName')" align="center" />
      <el-table-column width="200" :label="$t('pages.countryCode.table.flag')" align="center">
        <template slot-scope="scope">
          <!--          <flag-icon :code="scope.row.alphaTwo" :tooltip="scope.row.enName" />-->
          <el-image
            style="width: 45px; height: 30px"
            :src="scope.row.nationalFlag"
            :preview-src-list="[scope.row.nationalFlag]"
          >
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline" />
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="open" :label="$t('pages.countryCode.table.openStatus')" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.open === true">
            {{ $t('pages.countryCode.status.open') }}
          </span>
          <span v-else>{{ $t('pages.countryCode.status.closed') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="phonePrefix" :label="$t('pages.countryCode.table.phonePrefix')" align="center" />
      <el-table-column prop="sort" :label="$t('pages.countryCode.table.sort')" align="center" />
      <el-table-column prop="createTime" :label="$t('pages.countryCode.table.createdAt')" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('pages.countryCode.table.actions')" align="center" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click.native="handleUpdate(scope.row)">{{ $t('pages.countryCode.action.edit') }}</el-button>
          <el-button v-if="scope.row.top === 0" type="text" @click="toTop(scope.row.id,1)">{{ $t('pages.countryCode.action.top') }}</el-button>
          <el-button v-if="scope.row.top === 1" type="text" @click="toTop(scope.row.id,0)">{{ $t('pages.countryCode.action.cancelTop') }}</el-button>
          <el-button v-if="scope.row.open === false" type="text" @click="toOpen(scope.row.id,true)">{{ $t('pages.countryCode.action.openCountry') }}</el-button>
          <el-button v-if="scope.row.open === true" type="text" @click="toOpen(scope.row.id,false)">{{ $t('pages.countryCode.action.cancelOpen') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />

    <el-dialog
      :title="textOptTitle"
      :visible.sync="formVisible"
      :before-close="handleClose"
      width="400px"
    >
      <div v-loading="submitLoading">
        <el-form ref="form" :model="form" :rules="rules" label-width="110px">
          <el-form-item :label="$t('pages.countryCode.form.flag')" prop="nationalFlag">
            <el-upload
              class="upload-demo"
              action=""
              :http-request="httpRequest"
              :show-file-list="false"
              accept="image/png,image/jpg,image/jpeg"
            >
              <el-image v-if="form.nationalFlag" :src="form.nationalFlag" fit="fill" style="width: 50px; height: 50px" />
              <el-button v-loading="uploadLoading" size="small" type="primary">{{ $t('pages.countryCode.action.upload') }}</el-button>
            </el-upload>
          </el-form-item>
          <el-form-item :label="$t('pages.countryCode.form.countryName')" prop="countryName">
            <el-input v-model.trim="form.countryName" type="text" />
          </el-form-item>
          <el-form-item :label="$t('pages.countryCode.form.aliasName')">
            <el-input v-model.trim="form.aliasName" type="text" />
          </el-form-item>
          <el-form-item :label="$t('pages.countryCode.form.phonePrefix')" prop="phonePrefix">
            <el-input v-model.trim="form.phonePrefix" type="number" />
          </el-form-item>
          <el-form-item :label="$t('pages.countryCode.form.sort')" prop="sort">
            <el-input v-model.trim="form.sort" type="number" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm()">{{ $t('pages.countryCode.action.save') }}</el-button>
            <el-button @click="handleClose()">{{ $t('common.cancel') }}</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { updateSysCountryCode, pageSysCountryCode } from '@/api/sys-dictionary'
import Pagination from '@/components/Pagination'
import { getAddressUrl } from '@/api/oss'

function getFormData() {
  return {
    id: '',
    nationalFlag: '',
    enName: '',
    aliasName: '',
    phonePrefix: '',
    sort: ''
  }
}

export default {
  name: 'CountryCodeInfo',
  components: { Pagination },
  data() {
    return {
      formVisible: false,
      textOptTitle: '',
      list: [],
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 30,
        countryName: '',
        aliasName: ''
      },
      form: getFormData(),
      submitLoading: false,
      listLoading: true,
      uploadLoading: false
    }
  },
  computed: {
    rules() {
      return {
        nationalFlag: [
          { required: true, message: this.$t('pages.countryCode.validation.uploadFlag'), trigger: 'change' }
        ],
        enName: [
          { required: true, message: this.$t('pages.countryCode.validation.enterCountryName'), trigger: 'blur' }
        ],
        phonePrefix: [
          { required: true, message: this.$t('pages.countryCode.validation.enterPhonePrefix'), trigger: 'blur' }
        ],
        sort: [
          { required: true, message: this.$t('pages.countryCode.validation.enterSort'), trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.renderData()
  },
  methods: {
    renderData(isClean) {
      const that = this
      that.listLoading = true
      if (isClean === true) {
        that.list = []
        that.listQuery.cursor = 1
      }
      pageSysCountryCode(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.listLoading = false
      })
    },
    handleSearch() {
      this.renderData(true)
    },
    handleClose() {
      // 去除校验
      this.$refs.form.clearValidate()
      this.formVisible = false
    },
    handleUpdate(row) {
      this.textOptTitle = this.$t('pages.countryCode.dialog.editTitle')
      this.formVisible = true
      this.form = Object.assign(this.form, row)
    },
    httpRequest(file) {
      const that = this
      that.uploadLoading = true
      this.$simpleUploadFlie(file).then(res => {
        that.uploadLoading = false
        getAddressUrl(res.name).then((result) => {
          that.form.nationalFlag = result.result
        })
      }).catch(er => {
        that.uploadLoading = false
      })
    },
    submitForm() {
      const that = this
      that.$refs.form.validate(valid => {
        if (valid) {
          that.submitLoading = true
          if (that.form.id) {
            updateSysCountryCode(that.form).then(res => {
              that.submitLoading = false
              that.formVisible = false
              that.form = getFormData()
              that.renderData()
            }).catch(er => {
              that.submitLoading = false
              that.$emit('fial', er)
            })
          }
        } else {
          console.error('error submit!!')
          return false
        }
      })
    },
    toTop(id, top) {
      const that = this
      that.listLoading = true
      updateSysCountryCode({
        id: id,
        top: top
      }).then((res) => {
        that.listLoading = false
        that.$opsMessage.success()
        that.renderData(false)
      }).catch(() => { that.listLoading = false })
    },
    toOpen(id, open) {
      const that = this
      that.listLoading = true
      updateSysCountryCode({
        id: id,
        open: open
      }).then((res) => {
        that.listLoading = false
        that.$opsMessage.success()
        that.renderData(false)
      }).catch(() => { that.listLoading = false })
    }
  }
}
</script>
<style scoped lang="scss">
  .bottom {
    color: #666;
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 25px;
    .line {
      display: flex;
      >div {
        width: 100%;
      }
    }
  }

</style>
