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

  props: {
    dataId: {
    }

  },

  data() {

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

      actions: {
        create: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/sort/create`,
        update: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/sort/update`,
        get: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/sort/detail`,
        list: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/sort/list`,
      },
 

      formOptions: {
        data: {
          "name": "",
          "e_name": "" 
        },

        titleWidth: 150,
        titleAlign: 'right',

        rules: {
          name: [
            { required: true, message: '请输入名称' }
          ],
          e_name: [
            { required: true, message: '请输入名称(英文)' }
          ]
        },

        items: [
          { field: 'name', title: '名称', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入名称' } } },
          { field: 'e_name', title: '名称(英文)', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入名称(英文)' } } },
          { field: 'code', 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: {
          id: this.dataId
        },
        useBigInt: true
      }).then(a => { 
        this.formOptions.data = a.data.mes_processes_sort;

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

  },
  // 函数
  methods: {

    

    loadData() {
    },

    ok() {

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

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

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


    },

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

  }
};

</script>
<style></style>
