<template>
  <el-dialog
    class="config_dialog"
    :class="{'enClass': $i18n.locale === 'en-us'}"
    :title="$t('Configuration')"
    :visible.sync="dialogVisible"
    width="55%"
    :before-close="handleClose">
    <el-tabs v-model="activeName">
      <el-tab-pane :label="$t('Basicconfiguration')" name="first">
        <el-form :model="dataForm" ref="dataForm" :rules="dataRule" :label-width="$i18n.locale === 'en-us' ? '1.4rem' : '1.1rem'" size="mini">
          <p class="tip">{{$t('Languageselection')}}</p>
          <el-form-item :label="$t('Language')" prop="selectedLanguage">
            <el-select v-model="dataForm.selectedLanguage" :placeholder="$t('Pleaseselect')" id="selectedLanguage" style="width: 100%;">
              <el-option
                v-for="item in languages"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <p class="tip">{{$t('Parameterconfiguration')}}</p>
          <el-form-item :label="$t('ServiceIP')" prop="serverIp">
            <el-input :placeholder="$t('PleaseServiceIP')" v-model="dataForm.serverIp" id="ip"></el-input>
          </el-form-item>
          <el-form-item :label="$t('SchedulingIP')" prop="ip">
            <el-input :placeholder="$t('PleaseIP')" v-model="dataForm.ip" id="ip"></el-input>
          </el-form-item>
          <!-- <el-form-item label="WIFI" prop="wifi">
            <el-input :placeholder="$t('PleaseWIFI')" v-model="dataForm.wifi" id="wifi"></el-input>
          </el-form-item> -->
        </el-form>
        <el-row type="flex" justify="space-around" style="margin-top: .3rem">
          <el-col :span="7"><button class="button_control button_control_disabled" @click="exitUser"><p>{{$t('Cancel')}}</p></button></el-col>
          <el-col :span="7"><button class="button_control" @click="dataFormSubmit"><p>{{$t('Save')}}</p></button></el-col>
        </el-row>
      </el-tab-pane>
      <el-tab-pane :label="$t('Systemsettings')" name="second">
        <div class="tab_wraper">
          <div class="contorl_title">AGV</div>
          <el-row type="flex" class="contorl_box">
            <button class="agv_button" @click="toReboot">
              <img src="../../images/new/RF5.png" alt="">
              <p>{{ $t('Shutdownrestart') }}</p>
            </button>
            <button class="agv_button" @click="synchronizedMapConfirm">
              <img src="../../images/new/RF6.png" alt="">
              <p>{{ $t('Synchronizemap') }}</p>
            </button>
          </el-row>
          <div class="contorl_title">{{$t('Control')}}</div>
          <el-row type="flex" class="contorl_box">
            <p class="contorl_title_2">{{$t('Obstacleforklifts')}}</p>
            <div class="switch_button" :class="{'switch_button_on': backIoStatus === '1'}">
              <div class="switch_on_off"></div>
              <input type="checkbox" @click="_backIoStatus">
          </div>
          </el-row>
        </div>
      </el-tab-pane>
    </el-tabs>
  </el-dialog>
</template>

<script>
import { rebootVehicle, synchronizedMap, backIoStatus } from '../../config/getData.js'
import { mapGetters, mapActions } from 'vuex'
export default {
  data () {
    return {
      activeName: 'first',
      dialogVisible: false,
      dataForm: {
        selectedLanguage: '',
        serverIp: '',
        ip: '',
        wifi: ''
      },
      dataRule: {
        serverIp: [
          { required: true, message: this.$t('ServiceIPnotempty'), trigger: 'blur' }
        ],
        ip: [
          { required: true, message: this.$t('Schedulingnotempty'), trigger: 'blur' }
        ]
      },
      languages: [
        {label: '中文', value: 'zh-cn'},
        {label: 'English', value: 'en-us'}
        // {label: 'Español', value: 'es'}
      ]
    }
  },
  computed: {
    ...mapGetters(['serverUrl', 'backIoStatus']),
  },
  watch: {
    serverUrl (newVal) {
      if (newVal) {
        this.dataForm.serverIp = newVal
      }
    }
  },
  methods: {
    ...mapActions(['setServerUrl']),
    init () {
      if (this.$i18n.locale === 'zh-cn') {
        this.dataForm.selectedLanguage = 'zh-cn'
      } else if (this.$i18n.locale === 'en-us') {
        this.dataForm.selectedLanguage = 'en-us'
      }
      this.dataForm.serverIp = this.serverUrl
      this.activeName = 'first'
      this.dialogVisible = true
    },
    exitUser () {
      this.dialogVisible = false
    },
    dataFormSubmit () {
      this.dialogVisible = false
      this.$i18n.locale = this.dataForm.selectedLanguage
      window.localStorage.setItem('locale', this.dataForm.selectedLanguage)
      this.setServerUrl(this.dataForm.serverIp)
      this.$emit('refreshWebsocket')
    },
    handleClose (done) {
      done()
    },
    toReboot () {
      this.$confirm(this.$t('Aresureshutrestart'), this.$t('Prompt'), {
        confirmButtonText: this.$t('Confirm'),
        cancelButtonText: this.$t('Cancel'),
        type: 'warning'
      }).then(() => {
        this._rebootVehicle()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: this.$t('shutrestartcancel')
        })
      })
    },
    async _rebootVehicle () {
      try {
        this.loading = this.$loading({
          lock: true,
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.6)'
        })
        let res = await rebootVehicle()
        if (res) {
          this.initLink()
        }
        this.loading.close()
      } catch (e) {
        this.$message.error(e)
        this.loading.close()
      }
    },
    initLink () {
      let link = 'stservice://systech.com:8088/router?data=reboot'
      const a = document.createElement('a')
      a.href = link
      document.body.appendChild(a)
      a.click()
    },
    synchronizedMapConfirm () {
      this.$confirm(this.$t('Aresuremap'), this.$t('Prompt'), {
        confirmButtonText: this.$t('Confirm'),
        cancelButtonText: this.$t('Cancel'),
        type: 'warning'
      }).then(() => {
        this._synchronizedMap()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: this.$t('Mapbeencancel')
        })
      })
    },
    async _synchronizedMap () {
      try {
        this.loading = this.$loading({
          lock: true,
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.6)'
        })
        let res = await synchronizedMap()
        if (res) {
          this.$message(this.$t('Syncsuccessfully'))
        }
        this.loading.close()
      } catch (e) {
        this.$message.error(e)
        this.loading.close()
      }
    },
    async _backIoStatus () {
      try {
        this.loading = this.$loading({
          lock: true,
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.6)'
        })
        const type = this.backIoStatus === '1' ? '0' : '1'
        let res = await backIoStatus(type)
        if (res) {
          this.$message(res.message)
        }
        this.loading.close()
      } catch (e) {
        this.$message.error(e)
        this.loading.close()
      }
    },
  }
}
</script>

<style lang="stylus" scoped>
.tip
  font-size .2rem
  font-weight 700
  font-family 'SourceHanSansCN-Bold'
  line-height .2rem
  color #E54F29
  margin-bottom .1rem
.tab_wraper
  width 100%
  height calc(2.2rem + 72px)
.reset
  cursor pointer
  width .72rem
  height .71rem
  background center / 100% auto url(../../images/new/reset.png) no-repeat
.contorl_title
  width 2rem
  height .4rem
  padding-left .4rem
  font-size .24rem
  color #fff
  line-height .4rem
  margin-bottom .2rem
  background center / 100% 100% url(../../images/new/state_title_bg.png) no-repeat
.agv_button
  min-width .9rem
  height .9rem
  padding: 0.05rem;
  font-size: .16rem;
  color: #fff;
  border 1px solid #3D93F0
  background linear-gradient(0deg, #3483DA, #1E59CD)
  img
    width .5rem
    height .5rem
  +.agv_button
    margin-left .2rem
.contorl_box
  padding 0 .4rem
  margin-bottom .2rem
.contorl_title_2
  font-size: .2rem;
  line-height: .4rem;
  color: #fff;
  padding-right: 0.1rem;
.switch_button
  position relative
  width .9rem
  height .4rem
  border 1px solid #357ED1
  background linear-gradient(0deg, #286495, #0F294D)
  box-sizing border-box
  input[type="checkbox"]
    position absolute
    width 100%
    height 100%
    opacity: 0;
    margin: 0;
.switch_on_off
  position absolute
  width .4rem
  height 100%
  left 0
  background #4380AF
.switch_button_on
  border-color #3D93F0
  background linear-gradient(0deg, #3483DA, #1E59CD)
  .switch_on_off
    background #A2FAFE
    left auto
    right 0
.enClass
  .el-tabs__item
    font-size .2rem
</style>
