<template>
    <a-card :bordered="false">
      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button type="primary" icon="import">导入</a-button>
        <a-button type="primary" icon="sync" @click="checkSyncParams" :loading="checkLoading">校验</a-button>
        <a-button type="primary" icon="poweroff" @click="runAutoSync" :loading="runAutoLoading">启用同步</a-button>
        <a-button type="primary" icon="stop" @click="closeAutoSync" :loading="closeAutoLoading">关闭同步</a-button>
        <a-button type="primary" icon="sync" @click="handleSync" :loading="handleLoading">手动同步</a-button>
      </div>
      <a-form-model :label-col="{ span: 6 }" :wrapper-col="{ span: 12 }" :model="form" :rules="rules" ref="ruleForm">
        <a-form-model-item label="国网地址" prop="ims">
          <a-input placeholder="输入国网地址" v-model="form.ims">
            <!-- <a-select slot="addonBefore" default-value="http://" style="width: 90px" v-model="imsHead">
              <a-select-option value="http://">http://</a-select-option>
              <a-select-option value="https://">https://</a-select-option>
            </a-select> -->
            <!-- <a-select slot="addonAfter" default-value=".com" style="width: 80px" v-model="imsFoot">
              <a-select-option value=".com">.com</a-select-option>
              <a-select-option value=".jp">.jp</a-select-option>
              <a-select-option value=".cn">.cn</a-select-option>
              <a-select-option value=".org">.org</a-select-option>
              <a-select-option value="">空域名</a-select-option>
            </a-select> -->
          </a-input>
        </a-form-model-item>
        <a-form-model-item label="金蝶地址" prop="kingdee">
          <a-input placeholder="输入金蝶地址" v-model="form.kingdee">
            <!-- <a-select slot="addonBefore" default-value="http://" style="width: 90px" v-model="kingdeeHead">
              <a-select-option value="http://">http://</a-select-option>
              <a-select-option value="https://">https://</a-select-option>
            </a-select> -->
            <!-- <a-select slot="addonAfter" default-value=".com" style="width: 80px" v-model="kingdeeFoot">
              <a-select-option value=".com">.com</a-select-option>
              <a-select-option value=".jp">.jp</a-select-option>
              <a-select-option value=".cn">.cn</a-select-option>
              <a-select-option value=".org">.org</a-select-option>
              <a-select-option value="">空域名</a-select-option>
            </a-select> -->
          </a-input>
        </a-form-model-item>
        <a-form-model-item label="数据库ID" prop="dbid">
          <a-input placeholder="请输入数据库ID" v-model="form.dbid"></a-input>
        </a-form-model-item>
        <a-form-model-item label="语言" prop="lang">
          <a-input-number style="width:100%;" placeholder="请输入语言" v-model="form.lang"></a-input-number>
        </a-form-model-item>
        <a-form-model-item label="账号" prop="uid">
          <a-input placeholder="请输入账号" v-model="form.uid"></a-input>
        </a-form-model-item>
        <a-form-model-item label="密码" prop="pwd">
          <a-input placeholder="请输入密码" v-model="form.pwd"></a-input>
        </a-form-model-item>
        <a-form-model-item :label-col="{ span: 4 }" :wrapperCol="{ span: 8, offset: 6 }">
          <a-button type="primary" icon="download" @click="saveSyncParams" :loading="saveLoading">保存参数</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-card>
</template>
<script>
  import {postAction, getAction} from '@/api/manage'
  export default{
    data(){
      return {
        imsHead:'http://',
        // imsFoot:'',
        kingdeeHead:'http://',
        kingdeeFoot:'',
        form:{
          ims:'',
          kingdee:'',
          dbid:'',
          lang:'',
          uid:'',
          pwd:''
        },
        rules:{
          ims:[
            {
              required:true,
              message:"请输入国网地址",
              trigger: "blur",
            }
          ],
          kingdee:[
            {
              required:true,
              message:"请输入金蝶地址",
              trigger: "blur",
            }
          ],
          dbid:[
            {
              required:true,
              message:"请输入数据库ID",
              trigger: "blur",
            }
          ],
          lang:[
            {
              required:true,
              message:"请输入语言",
              trigger: "blur",
            }
          ],
          uid:[
            {
              required:true,
              message:"请输入账号",
              trigger: "blur",
            }
          ],
          pwd:[
            {
              required:true,
              message:"请输入密码",
              trigger: "blur",
            }
          ]
        },
        runAutoLoading:false,
        saveLoading:false,
        closeAutoLoading:false,
        handleLoading:false,
        checkLoading:false
      }
    },
    created(){
      getAction('/API/KingdeeInit/GetSaveParm').then((res) => {
        let kingdeeUrl = (res.result.kingdeeConnectParm.k3CloudURL)
        let imsUrl = (res.result.imsConParm.url)
        this.form.dbid = res.result.kingdeeConnectParm.dbId
        this.form.uid = res.result.kingdeeConnectParm.uid
        this.form.lang = res.result.kingdeeConnectParm.lang
        this.form.pwd = res.result.kingdeeConnectParm.pwd
        this.form.kingdee = kingdeeUrl
        this.form.ims = imsUrl
      })
    },
    computed:{
      ImsUrl:function(){
        return this.form.ims
      },
      KingdeeUrl:function(){
        return this.form.kingdee
      },
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods:{
      //启动自动同步
      runAutoSync(){
        this.runAutoLoading = true
        postAction('/API/KingdeeInit/ManualSyn').then((res) => {
          if(res.code == "200"){
            this.$message.success("启动自动同步成功(每分钟同步一次)")
            this.runAutoLoading = false
          }else{
            this.$message.error(res.message)
            this.runAutoLoading = false
          }
        })
      },
      closeAutoSync(){
        this.closeAutoLoading = true
        postAction('/API/KingdeeInit/ManualSynStop').then((res) => {
          if(res.code == "200"){
            this.$message.success(res.result)
            this.closeAutoLoading = false
          }else{
            this.$message.error(res.message)
            this.closeAutoLoading = false
          }
        })
      },
      //手动同步
      handleSync(){
        this.handleLoading = true
        postAction('/API/KingdeeInit/synchronization').then((res) => {
          if(res.code == "500"){
            this.$message.error(res.message)
            this.handleLoading = false
          }else{
            this.$message.success(res.message)
            this.handleLoading = false
          }
        })
      },
      //保存同步参数
      saveSyncParams(){
        this.$refs.ruleForm.validate(valid => {
          if(valid == true){
            this.saveLoading = true
            let params = {
              imsConParm:{
                url:this.ImsUrl
              },
              kingdeeConnectParm:{
                dbId:this.form.dbid,
                k3CloudURL:this.KingdeeUrl,
                lang:this.form.lang,
                pwd:this.form.pwd,
                uid:this.form.uid
              }
            }
            postAction('/API/KingdeeInit/ManualSave',params).then((res) => {
              if(res.success == true){
                this.saveLoading = false
                this.$message.success(res.message)
              }else{
                this.saveLoading = false
                this.$message.error(res.message)
              }
            })
          }else{
            console.log("请输入必填项")
          }
        })
      },
      //校验
      checkSyncParams(){
        this.$refs.ruleForm.validate(valid => {
          if(valid == true){
            this.checkLoading = true
            let params = {
              imsConParm:{
                url:this.ImsUrl
              },
              kingdeeConnectParm:{
                dbId:this.form.dbid,
                k3CloudURL:this.KingdeeUrl,
                lang:this.form.lang,
                pwd:this.form.pwd,
                uid:this.form.uid
              }
            }
            postAction('/API/KingdeeInit/ManualCheck',params).then((res) => {
              console.log(res)
              if(res.success == true){
                this.checkLoading = false
                this.$message.success(res.result)
              }else{
                this.checkLoading = false
                this.$message.error(res.message)
              }
            })
          }else{
            console.log("请输入必填项")
          }
        })
      }
    },
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>