<script>
import { defineComponent } from 'vue';

import { PlusOutlined, MinusOutlined, SyncOutlined, CheckOutlined } from '@ant-design/icons-vue'
import Plugin from '../../mixins/plugin'

export default defineComponent({
  name: 'Git',
  components: { PlusOutlined, MinusOutlined, SyncOutlined, CheckOutlined },
  mixins: [Plugin],

  data () {
    return {
      key: 'plugin.git',
      labelCol: { span: 4 },
      wrapperCol: { span: 20 },
      noProxyUrls: [],
      needRestart: false,
    }
  },

  created () {
    console.log('status:', this.status)
  },

  mounted () {
  },

  methods: {
    ready () {
      this.initNoProxyUrls()
    },
    async applyBefore () {
      if (this.status.plugin.git.enabled) {
        await this.$api.plugin.git.close()
        this.needRestart = true
      } else {
        this.needRestart = false
      }
      this.submitNoProxyUrls()
    },
    async applyAfter () {
      if (this.needRestart) {
        await this.$api.plugin.git.start()
      }
    },
    initNoProxyUrls () {
      this.noProxyUrls = []
      for (const key in this.config.plugin.git.setting.noProxyUrls) {
        const value = this.config.plugin.git.setting.noProxyUrls[key]
        this.noProxyUrls.push({
          key,
          value,
        })
      }
    },
    addNoProxyUrl () {
      this.noProxyUrls.unshift({ key: '' })
      this.focusFirst(this.$refs.noProxyUrls)
    },
    delNoProxyUrl (item, index) {
      this.noProxyUrls.splice(index, 1)
    },
    submitNoProxyUrls () {
      const noProxyUrls = {}
      for (const item of this.noProxyUrls) {
        if (item.key) {
          const hostname = this.handleHostname(item.key)
          if (hostname) {
            noProxyUrls[hostname] = true
          }
        }
      }
      this.config.plugin.git.setting.noProxyUrls = noProxyUrls
    },
  },
});
</script>

<template>
  <ds-container>
    <template #header>
      Git.exe代理设置
    </template>
    <template #header-right>
      仅针对git命令行的代理设置，github网站的访问无需设置
    </template>

    <div v-if="config">
      <a-form layout="horizontal">
        <a-form-item label="启用Git代理" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-checkbox v-model:checked="config.plugin.git.enabled">
            随应用启动
          </a-checkbox>
          <a-tag v-if="status.plugin.git.enabled" color="green">
            当前已启动
          </a-tag>
          <a-tag v-else color="red">
            当前未启动
          </a-tag>
        </a-form-item>
        <a-form-item label="SSL校验" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-checkbox v-model:checked="config.plugin.git.setting.sslVerify">
            关闭sslVerify
          </a-checkbox>
          安装Git时未选择使用系统证书管理服务时必须关闭
        </a-form-item>
        <a-form-item label="排除仓库地址" :label-col="labelCol" :wrapper-col="wrapperCol">
          <div>
            <a-row :gutter="10">
              <a-col :span="22">
                <span><code>Git.exe</code>将不代理以下仓库；可以是根地址、组织/机构地址、完整地址</span>
              </a-col>
              <a-col :span="2">
                <a-button type="primary" @click="addNoProxyUrl()"><PlusOutlined /></a-button>
              </a-col>
            </a-row>
            <a-row v-for="(item, index) of noProxyUrls" ref="noProxyUrls" :key="index" :gutter="10">
              <a-col :span="22">
                <a-input v-model:value="item.key" class="mt-2" spellcheck="false" />
              </a-col>
              <a-col :span="2">
                <a-button type="danger" @click="delNoProxyUrl(item, index)"><MinusOutlined /></a-button>
              </a-col>
            </a-row>
          </div>
        </a-form-item>
      </a-form>
    </div>
    <template #footer>
      <div class="footer-bar">
        <a-button :loading="resetDefaultLoading" class="mr10" @click="resetDefault()">
          <SyncOutlined />恢复默认
        </a-button>
        <a-button :loading="applyLoading" type="primary" @click="apply()">
          <CheckOutlined />应用
        </a-button>
      </div>
    </template>
  </ds-container>
</template>
