<template>
  <div class="page-body">

    <vxe-form :data="formOptions.data" ref="xForm" :title-width="formOptions.titleWidth"
      :title-align="formOptions.titleAlign" :rules="formOptions.rules" :items="formOptions.items" titleColon>
      <template #myregion="{}">
        <a-input-search placeholder="input search text" enter-button />
      </template>
    </vxe-form>


    <div class="footerbar">
      <a-button type="primary" @click="ok">确定</a-button> 
    </div>

  </div>
</template>

<script>
import BASE_URL from '@/services/base/api.js';

export default {
  i18n: require('./i18n'),

  props: {
    pageMode: {
      type: String,
      default: "edit"
    },
    dataId: {
    }

  },

  data() {

    // 页面数据变量
    var pageData = {

      actions: {
        update: `${BASE_URL.BASE_URL}/SqlServer/v1/sql/server/update`,
        get: `${BASE_URL.BASE_URL}/SqlServer/v1/sql/server/detail`
      },

      formOptions: {
        data: {
          company_id:0,
          ipaddress:'',
          port:'',
          username:'',
          password:'',
          database:'',
        },

        titleWidth: 100,
        titleAlign: 'right',

        rules: {
        },

        items: [
          { field: 'ipaddress', title: 'IP', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入IP' } } },
          { field: 'port', title: '端口', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入端口' } } },
          { field: 'username', title: '用户名', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入用户名' } } },
          { field: 'password', title: '密码', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入密码' } } },
          { field: 'database', title: '数据库', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入数据库' } } }
        ]
      }

    };


    pageData.formOptions = Object.assign({}, this.$mk.config.defaults.formOptions, pageData.formOptions);

    return pageData;
  },
  created() {
    if (this.dataId) {
      this.$mk.post({
        url: this.actions.get,
        loading: "加载中...",
        data: {
          company_id: this.dataId
        }
      }).then(a => {
        if(a.data && a.data.sql_server){
          this.formOptions.data = a.data.sql_server;
        }else{
          this.formOptions.data.company_id = this.dataId;
        }
       
      }).catch((a) => {
        console.log(a)
        //this.$mk.error(a.data.msg);
      });
    } else {

      this.formOptions.data.company_id = this.dataId;
    }

  },
  // 函数
  methods: {


    loadData() {
    },

    ok() {

      let save = () => {
        let action = this.actions.update;
        let postdata = Object.assign({}, this.formOptions.data);

        postdata.company_id = this.dataId;
        this.$mk.post({
          url: action,
          loading: "保存中...",
          data: postdata,
          useBigInt: true
        }).then(() => {
          this.$mk.success("SQL配置保存成功");
          this.$emit("callback", { success: true });
        }).catch((a) => {
          this.$mk.error(a.data.msg);
        });
      };

      this.$refs.xForm.validate((a) => {

        if (a) {
          let count = 0;
          for (let name in a) {
            a[name];
            count++;
          }
          this.$mk.error(`存在${count}项错误，请检查`);
        } else {
          save();
        }
      });


    },

    cancel() {
      this.$emit("callback", {});
    }
  },
  // 监听属性
  watch: {

  }
};

</script>
<style></style>
