<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" :disabled="isSynchronize">启用同步</a-button>
        <a-button type="primary" icon="stop" @click="closeAutoSync" :loading="closeAutoLoading" :disabled="!isSynchronize">关闭同步</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="IMS地址" prop="ims">
          <a-input placeholder="输入ims地址" v-model="form.ims" :disabled="isSynchronize"></a-input>
        </a-form-model-item>
        <a-form-model-item label="金蝶地址" prop="kingdee">
          <a-input placeholder="输入金蝶地址" v-model="form.kingdee" :disabled="isSynchronize"></a-input>
        </a-form-model-item>
        <a-form-model-item label="数据库ID" prop="dbid">
          <a-input placeholder="请输入数据库ID" v-model="form.dbid" :disabled="isSynchronize"></a-input>
        </a-form-model-item>
        <a-form-model-item label="语言" prop="lang">
          <a-input-number style="width:100%;" placeholder="请输入语言" v-model="form.lang" :disabled="isSynchronize"></a-input-number>
        </a-form-model-item>
        <a-form-model-item label="账号" prop="uid">
          <a-input placeholder="请输入账号" v-model="form.uid" :disabled="isSynchronize"></a-input>
        </a-form-model-item>
        <a-form-model-item label="密码" prop="pwd">
          <a-input-password  placeholder="请输入密码" v-model="form.pwd" :disabled="isSynchronize"></a-input-password>
        </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" :disabled="isSynchronize">保存参数</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-card>
</template>
<script>
  import {postAction, getAction} from '@/api/manage'
  export default{
    data(){
      return {
        form:{
          ims:'',
          kingdee:'',
          dbid:'',
          lang:'',
          uid:'',
          pwd:''
        },
        rules:{
          ims:[
            {
              required:true,
              message:"请输入IMS地址",
              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,
        isSynchronize:false
      }
    },
    created(){
      getAction('/WebService/GetSaveParm').then((res) => {
        this.form.kingdee = res.result.kingdeeConnectParm.k3CloudURL
        this.form.ims = 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.isSynchronize = res.result.isSynchronize
      })
    },
    methods:{
      //启动自动同步
      runAutoSync(){
        this.runAutoLoading = true
        postAction('/WebService/ManualSyn').then((res) => {
          if(res.code == "200"){
            this.$message.success("启动自动同步成功(每分钟同步一次)")
            this.runAutoLoading = false
            this.isSynchronize = true
          }else{
            this.$message.error(res.message)
            this.runAutoLoading = false
          }
        })
      },
      closeAutoSync(){
        this.closeAutoLoading = true
        postAction('/WebService/ManualSynStop').then((res) => {
          if(res.code == "200"){
            this.$message.success(res.result)
            this.closeAutoLoading = false
            this.isSynchronize = false
          }else{
            this.$message.error(res.message)
            this.closeAutoLoading = false
          }
        })
      },
      //手动同步
      handleSync(){
        this.handleLoading = true
        postAction('/syn/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('/WebService/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('/WebService/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>