<template>
  <div class="main-contain">
    <div class="titleOne">
      质控平台的上报对接，是为了解决手工录入的效率和易出错的问题，对接涉及各种技术，请勿随意配置，联系酷医云客服协助处理。
    </div>
    <div class="titleOne">微信:kuyicloud 电话：18682074632</div>
    <div class="formBox">
      <el-form
        :model="form"
        ref="ruleForm"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
        style="text-align: center"
      >
        <el-form-item label="网关地址" prop="gateway_address">
          <el-input v-model="form.gateway_address"></el-input>
        </el-form-item>

        <el-form-item label="AppID" prop="app_id">
          <el-input v-model="form.app_id"></el-input>
        </el-form-item>

        <el-form-item label="密钥" prop="key">
          <el-input v-model="form.key"></el-input>
        </el-form-item>

        <el-form-item label="自动上传周期" prop="time_type">
          <el-radio-group v-model="form.time_type">
            <el-radio label="1">一周</el-radio>
            <el-radio label="2">二周</el-radio>
            <el-radio label="3">一个月</el-radio>
            <el-radio label="4">三个月</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            class="changeLeft"
            >保存</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getConfig, postConfig, postModifyConfig } from "@/api/config";

export default {
  name: "Country",
  data() {
    return {
      signAndWeighBoxPatients: "sign-and-weigh-box-patients",
      form: {
        id: 0,
        gateway_address: "",
        app_id: "",
        key: "",
        time_type: "",
        config_type: 1,
        province: 0,
        city: 0
      },
      rules: {
        gateway_address: [
          { required: true, message: "请输入网关地址", trigger: "blur" }
        ],
        app_id: [{ required: true, message: "请输入AppID", trigger: "blur" }],
        key: [{ required: true, message: "请输入密钥", trigger: "blur" }],
        time_type: [
          { required: true, message: "请选择自动上传周期", trigger: "change" }
        ]
      }
    };
  },
  methods: {
    submitForm: function(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.form.id == 0) {
            postConfig(this.form)
              .then(response => {
                var res = response.data;
                if (res.state === 1) {
                  this.config = res.data.config;
                  this.form.id = this.config.id;
                  this.form.gateway_address = this.config.gateway_address;
                  this.form.app_id = this.config.app_id;
                  this.form.key = this.config.key;
                  this.form.time_type = this.config.time_quantum.toString();
                  this.$message.success("提交成功");
                }
              })
              .catch(e => {});
          } else {
            postModifyConfig(this.form)
              .then(response => {
                var res = response.data;
                if (res.state === 1) {
                  this.$message.success("修改成功");
                }
              })
              .catch(e => {});
          }
        } else {
          return false;
        }
      });
    },
    getConfig: function() {
      getConfig({ config_type: 1 })
        .then(response => {
          var res = response.data;
          if (res.state === 1) {
            this.config = res.data.config;
            this.form.id = this.config.id;
            this.form.gateway_address = this.config.gateway_address;
            this.form.app_id = this.config.app_id;
            this.form.key = this.config.key;
            this.form.time_type = this.config.time_quantum.toString();
          }
        })
        .catch(e => {});
    }
  },
  created() {
    this.getConfig();
  }
};
</script>

<style>
.changeLeft {
  margin-left: -254px !important;
}
.titleOne {
  line-height: 30px;
  margin: 0 auto;
  width: 100%;
  text-align: center;
}
.formBox {
  width: 46%;
  margin: 0 auto;
}
</style>
