<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>
    </vxe-form>  


    <div class="footerbar">
      <a-button type="primary" @click="ok">确定</a-button>
      <a-button @click="cancel">取消</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}/Sms/v1/sms/update`,
        get: `${BASE_URL.BASE_URL}/Sms/v1/sms/detail`
      },


      formOptions: {
        data: {
          sign_name:'',
          access_key_secret:'',
          accessKey_id:'',
          limit_count:0,
          limit_count_warning:0,
          endpoint:'',
        },

        titleWidth: 150,
        titleAlign: 'right',

        rules: {
        },

        items: [
          { field: 'sign_name', title: '签名', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入签名' } } },
          { field: 'access_key_secret', title: 'access_key_secret', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入access_key_secret' } } },
          { field: 'accessKey_id', title: 'accessKey_id', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入accessKey_id' } } },
          { field: 'limit_count',dataRule:{type:"integer"}, title: 'limit_count', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入limit_count',type:"number" } } },
          { field: 'limit_count_warning',dataRule:{type:"integer"},  title: 'limit_count_warning', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入limit_count_warning',type:"number"  } } },
          { field: 'endpoint', title: 'endpoint', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入endpoint' } } }
        ]
      }

    };

 

    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: {
          beid: this.dataId
        }
      }).then(a => {
        if(a.data && a.data.sms){
          this.formOptions.data = a.data.sms;
        }else{
          this.formOptions.data.beid = this.dataId;
        }
       
      }).catch((a) => {
        console.log(a)
        //this.$mk.error(a.data.msg);
      });
    } else {

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

  },
  // 函数
  methods: {


    loadData() {
    },

    ok() {

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

        this.$mk.formatFormData({ data: postdata, rules: this.formOptions.items });
        postdata.beid = this.dataId;
        this.$mk.post({
          url: action,
          loading: "保存中...",
          data: postdata,
          useBigInt: true
        }).then(() => {
          this.$mk.success("SMS配置保存成功");
          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>
