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

  i18n: require('./i18n'),
  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: "/BaseDepartment/BaseDepartmentAdd",
        editPageUrl: "/BaseDepartment/BaseDepartmentUpdate/",
        listPageUrl: "/BaseDepartment/BaseDepartmentList",


        uploadDefaultImg: null,

        detailDataFieldName: "base_department",

        actions: {

          BaseDepartmentDetail: `${BASE_URL.BASE_URL}/BaseDepartment/v1/base/department/detail`, // 部门详情
          BaseDepartmentCreate: `${BASE_URL.BASE_URL}/BaseDepartment/v1/base/department/create`, // 创建部门
          BaseDepartmentUpdate: `${BASE_URL.BASE_URL}/BaseDepartment/v1/base/department/update`, // 更新部门
          BaseDepartmentDelete: `${BASE_URL.BASE_URL}/BaseDepartment/v1/base/department/detele`, // 删除部门


        },

        keyName: 'id',
        // 是否编辑模式
        isEdit: false,
        // 表单数据
        formOptions: {
          data: {
            // =============================== 表单数据 自动生成 Start ===============================

            id: 0,
            code: "",
            title: "",
            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,

            // =============================== 表单数据 自动生成 End ===============================
          },
          // 标题宽度
          titleWidth: 150,
          // 标题对齐方式
          titleAlign: 'right',

          // 表单校验规则
          rules: {
            // =============================== 表单校验 自动生成 Start ===============================

            code: [
              { required: true, message: '请输入编码' }
            ],
            title: [
              { required: true, message: '请输入部门名称' }
            ],
            name: [
              { required: true, message: '请输入部门英文名称' }
            ],

            // =============================== 表单校验 自动生成 End ===============================
          },
          // 表单项
          items: [
            // =============================== 表单项 自动生成 Start ===============================

            { field: 'code', title: '编码', span: 24, itemRender: { name: '$input' } },
            //{ field: 'title', title: '部门名称', span: 24, itemRender: { name: '$input' } },
            { field: 'name', title: '部门名称', span: 24, itemRender: { name: '$input' } },

            {
              title: '部门', span: 24,
              field: 'parent_department',
              dataRule: {
                fromField: "id",
                saveField: "parent_id" // 如果表单项存储的是对象，将从fromField中取得值保存到saveField
              },
              itemRender: {
                name: 'MkFormDataSelector', props: {
                  params: {
                    dataType: "object",
                    valueField: "id",
                    textField: "name",
                    listdataFieldName: 'BaseDepartment',
                    treeConfig: {
                      transform: true,
                      expandAll: true,
                      rowField: 'id',
                      parentField: 'parent_id'
                    },
                    columns: [
                      { field: 'name', sortable: true, title: '部门名', showHeaderOverflow: true, treeNode: true },
                      { field: 'code', sortable: true, title: '编号', showHeaderOverflow: true }
                    ],
                    dataUrl: `${BASE_URL.BASE_URL}/BaseDepartment/v1/base/department/list`
                  }
                }
              }
            },

            // =============================== 表单项 自动生成 End ===============================
          ]
        },
        // 新增模式表单项
        addModeItems: [

        ],


      };

      pageData.actions.get = pageData.actions.BaseDepartmentDetail;
      pageData.actions.create = pageData.actions.BaseDepartmentCreate;
      pageData.actions.update = pageData.actions.BaseDepartmentUpdate;
      // 合并表单数据及配置
      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 => {

          let detailDataFieldName = this.pageOptions.detailDataFieldName;

          if (a.data[detailDataFieldName].create_time) {
            a.data[detailDataFieldName].create_time = new Date(a.data[detailDataFieldName].create_time * 1000);
          }
          if (a.data[detailDataFieldName].update_time) {
            a.data[detailDataFieldName].update_time = new Date(a.data[detailDataFieldName].update_time * 1000);
          }

          this.pageOptions.formOptions.data = a.data[detailDataFieldName];

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

        this.pageOptions.isEdit = true;

        this.$forceUpdate()
      } else {
        // 如果没有id参数，说明是新增模式
        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>
 