<template>
  <basic-page-edit :desc="desc" :dataId="getDataId()" :options="pageOptions"></basic-page-edit>
</template>

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


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

  data() {

    return {
      pageOptions: {}
    };
  },

  computed: {
    desc() {
      return this.$t('editPageDesc')
    }
  },


  created() {

    this.optionsInit();
    this.dataInit(); 

  },
  // 函数
  methods: {



    optionsInit() {
      // 页面数据变量
      var pageData = {
        // 当前项目名称
        currentConfigName: "",
        // 当前项目ID
        currentBeid: 0,

        addPageUrl: "/Mold/MoldSchemeCategoryAdd",
        editPageUrl: "/Mold/MoldSchemeCategoryUpdate/",
        listPageUrl: "/Mold/MoldSchemeCategory", 
 

        uploadDefaultImg: null,

        detailDataFieldName: "mold_scheme_category",
 
        actions: {  
          get: `${BASE_URL.BASE_URL}/MoldScheme/v1/mold/scheme/category/detail`, 
          create: `${BASE_URL.BASE_URL}/MoldScheme/v1/mold/scheme/category/create`, 
          update: `${BASE_URL.BASE_URL}/MoldScheme/v1/mold/scheme/category/update`,  
        },

        keyName: 'id',
        // 是否编辑模式
        isEdit: false,
        // 表单数据
        formOptions: {
          data: { 

            id: 0,
            code: "", 
            name: "",
            create_uid: 0,
            update_uid: 0,
            create_time: new Date(2100, 1, 1).getTime() / 10000,
            update_time: new Date(2100, 1, 1).getTime() / 10000,
 
          },
          // 标题宽度
          titleWidth: 150,
          // 标题对齐方式
          titleAlign: 'right',

          // 表单校验规则
          rules: { 

            code: [
              { required: true, message: '请输入编码' }
            ], 
            name: [
              { required: true, message: '请输入名称' }
            ],
 
          },
          // 表单项
          items: [ 

            { field: 'code', title: '编码', span: 24, itemRender: { name: '$input' } }, 
            { field: 'name', title: '名称', span: 24, itemRender: { name: '$input' } },
 
            { field: 'sort',dataRule:{type:'integer'}, title: '排序', span: 24, itemRender: { name: '$input',props:{type:'number'} } }, 
            { field: 'remark', title: '备注', span: 24, itemRender: { name: '$input' } }, 
          ]
        },
        // 新增模式表单项
        addModeItems: [

        ],


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

      this.pageOptions = pageData;
    },

    dataInit() {
      // 获取路由的id参数
      let dataId = this.getDataId();
      // 如果有id参数，说明是编辑模式
      if (dataId) {
        const json = `{"id":${dataId}}`;
        this.$mk.post({
          url: this.pageOptions.actions.get,
          loading: "加载中...",
          data: json,
          config: {
            headers: {
              'Content-Type': 'application/json'
            }
          }
        }).then(a => {
  
          this.pageOptions.formOptions.data =  this.$mk.formatDetailData({data:a.data[this.pageOptions.detailDataFieldName]});
          
          this.$forceUpdate()
        });
 
        this.pageOptions.isEdit = true;

        this.$forceUpdate()
      } else {
        // 如果没有id参数，说明是新增模式
      if(this.addModeItems){
          this.addModeItems.forEach(item => {
          this.pageOptions.formOptions.items.push(item);
      })
      }

      }
    },

    // 获取路由的id参数
    getDataId() {
      let dataId = this.dataId;
      if (this.$route.params.id) {
        dataId = this.$route.params.id;
      }
      if (!dataId) {
        dataId = 0;
      }
      return dataId;
    } 
  },
  // 监听属性
  watch: {

  }
};

</script>
 