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

    <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>
      <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 {

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

  },

  data() {

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

      actions: {
        create: `${BASE_URL.BASE_URL}/BasePermission/v1/base/permission/create`,
        update: `${BASE_URL.BASE_URL}/BasePermission/v1/base/permission/update`,
        get: `${BASE_URL.BASE_URL}/BasePermission/v1/base/permission/detail`,
        list: `${BASE_URL.BASE_URL}/BasePermission/v1/base/permission/list`
      },

      service_id: 0,
      service: '',
      parentName: '',

      formOptions: {
        data: {
          "title": "",
          "service_id": 0,
          "service": "",
          "permission_code": "",
          "desc": ""
        },

        titleWidth: 100,
        titleAlign: 'right',

        rules: {
          title: [
            { required: true, message: '请输入标题' }
          ], 
          permission_code: [
            { required: true, message: '请输入权限编号' }
          ], 
        },

        items: [
          {
            title: '左侧',
            children: [
              { field: 'title', title: '权限标题', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入权限标题' } } },
              { field: 'permission_code', title: '权限编号', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入权限编号' } } },
              { field: 'desc', title: '描述', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入描述' } } }
            ]
          }
        ]
      }

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

    return pageData;
  },
  created() {

    this.service_id = this.pageOptions.service_id;
    this.service = this.pageOptions.service;

    if (this.dataId) {
      this.$mk.post({
        url: this.actions.get,
        loading: "加载中...",
        data: {
          id: this.dataId
        }
      }).then(a => {
        this.formOptions.data = a.data.router;
        if (this.formOptions.data.parent_router) {
          this.parentName = this.formOptions.data.parent_router.name;
        }

      }).catch((a) => {
        this.$mk.error(a.data.msg);
      });


    }

  },
  // 函数
  methods: {


    loadData() {
    },

    loadParentGrid({ params }) {


      params.start_time = 0;
      params.end_time = 0;
      params.service_id = this.service_id;

      return this.$mk.getPagedData({ url: this.actions.list, data: params });
    },

    ok() {

      let save = () => {
        let action = this.pageMode == "add" ? this.actions.create : this.actions.update;
        let postdata = this.pageMode == "add" ? Object.assign({ id: this.dataId }, this.formOptions.data) : Object.assign({}, this.formOptions.data);

        postdata.service_id = this.service_id;
        postdata.service = this.service;
        if (this.pageMode != "add") {
          //postdata =  { BaseService:postdata}
        }
        this.$mk.post({
          url: action,
          loading: "保存中...",
          data: postdata,
        }).then(() => {
          this.$mk.success("保存成功");
          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 scoped lang="less">
.router-edit-page .vxe-pulldown {
  width: 100%;
}
</style>
