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

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

const settings = require('../../basic/settings.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: "/MesWarehouse/MesWarehouseAdd",
        editPageUrl: "/MesWarehouse/MesWarehouseUpdate/",
        listPageUrl: "/MesWarehouse/MesWarehouseList",


      uploadDefaultImg: null,
      detailDataFieldName: "mes_warehouse",

      actions: {
        // =============================== 接口地址 自动生成 Start ===============================
					MesWarehouseList: `${BASE_URL.BASE_URL}/MesWarehouse/v1/mes/warehouse/list`, // 仓库列表
					MesWarehouseDetail: `${BASE_URL.BASE_URL}/MesWarehouse/v1/mes/warehouse/detail`, // 仓库详情
					MesWarehouseCreate: `${BASE_URL.BASE_URL}/MesWarehouse/v1/mes/warehouse/create`, // 创建仓库
					MesWarehouseUpdate: `${BASE_URL.BASE_URL}/MesWarehouse/v1/mes/warehouse/update`, // 更新仓库
					MesWarehouseDelete: `${BASE_URL.BASE_URL}/MesWarehouse/v1/mes/warehouse/detele`, // 删除仓库
					MesWarehouseBatchDelete: `${BASE_URL.BASE_URL}/MesWarehouse/v1/mes/warehouse/batchDelete`, // 批量删除仓库
					MesWarehouseBatchUpdate: `${BASE_URL.BASE_URL}/MesWarehouse/v1/mes/warehouse/batchUpdate`, // 批量更新仓库
					MesWarehouseBatchCreate: `${BASE_URL.BASE_URL}/MesWarehouse/v1/mes/warehouse/batchCreate`, // 批量创建仓库
					MesWarehouseExportExcel: `${BASE_URL.BASE_URL}/MesWarehouse/v1/mes/warehouse/export/excel`, // 导出仓库数据到EXCEL
					MesWarehouseImportExcel: `${BASE_URL.BASE_URL}/MesWarehouse/v1/mes/warehouse/import/excel`, // 批量导入EXCEL仓库数据
					WarehouseLocationList: `${BASE_URL.BASE_URL}/MesWarehouse/v1/warehouse/location/list`, // 仓库库位列表
					WarehouseLocationDetail: `${BASE_URL.BASE_URL}/MesWarehouse/v1/warehouse/location/detail`, // 仓库库位详情
					WarehouseLocationCreate: `${BASE_URL.BASE_URL}/MesWarehouse/v1/warehouse/location/create`, // 创建仓库库位
					WarehouseLocationUpdate: `${BASE_URL.BASE_URL}/MesWarehouse/v1/warehouse/location/update`, // 更新仓库库位
					WarehouseLocationDelete: `${BASE_URL.BASE_URL}/MesWarehouse/v1/warehouse/location/detele`, // 删除仓库库位
					WarehouseLocationBatchDelete: `${BASE_URL.BASE_URL}/MesWarehouse/v1/warehouse/location/batchDelete`, // 批量删除仓库库位
					WarehouseLocationBatchUpdate: `${BASE_URL.BASE_URL}/MesWarehouse/v1/warehouse/location/batchUpdate`, // 批量更新仓库库位
					WarehouseLocationBatchCreate: `${BASE_URL.BASE_URL}/MesWarehouse/v1/warehouse/location/batchCreate`, // 批量创建仓库库位
					WarehouseLocationExportExcel: `${BASE_URL.BASE_URL}/MesWarehouse/v1/warehouse/location/export/excel`, // 导出仓库库位数据到EXCEL
					WarehouseLocationImportExcel: `${BASE_URL.BASE_URL}/MesWarehouse/v1/warehouse/location/import/excel`, // 批量导入EXCEL仓库库位数据

        // =============================== 接口地址 自动生成 End ===============================
      },

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

					id: 0,
					code: "",
					warehouse_title: "",
					warehouse_name: "",
					warehouse_code: "",
					warehouse_type: "",
					warehouse_address: "",
					warehouse_uid: 0,
					warehouse_uname: "",
					warehouse_mobile: "",
					warehouse_phone: "",
					warehouse_remark: "",
					warehouse_negative_stock: 0,
					warehouse_location: 1,
					warehouse_batch: 1,
					warehouse_serial: 1,
					warehouse_storage_type: 1,
					warehouse_effective_date: new Date(2100, 1, 1).getTime() / 10000,
					warehouse_invalid_date: new Date(2100, 1, 1).getTime() / 10000,
					warehouse_status: 0,
					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: '请输入编码' }
            ] ,
            warehouse_name: [
              { required: true, message: '请输入仓库名称' }
            ] 
            // =============================== 表单校验 自动生成 End ===============================
          },
          // 表单项
          items: [
            // =============================== 表单项 自动生成 Start ===============================

					{ field: 'code', title: '编码', span: 12, itemRender: { name: '$input' } },
					//{ field: 'warehouse_title', title: '仓库名称', span: 12, itemRender: { name: '$input' } },
					{ field: 'warehouse_name', title: '仓库名称', span: 12, itemRender: { name: '$input' } },

					{ field: 'warehouse_code', title: '仓库编码', span: 12, itemRender: { name: '$input' } },
					{ field: 'warehouse_type', title: '仓库类型', span: 12, itemRender: { name: '$select',props:{options:settings.options_warehouse_type} } },
					{ field: 'warehouse_address', title: '仓库地址', span: 12, itemRender: { name: '$input' } },
					//{ field: 'warehouse_uid', title: '仓库负责人UID', span: 12, itemRender: { name: '$input' } },
					{ field: 'warehouse_uname', title: '仓库负责人姓名', span: 12, itemRender: { name: '$input' } },
					{ field: 'warehouse_mobile', title: '仓库负责人手机号', span: 12, itemRender: { name: '$input' } },
					{ field: 'warehouse_phone', title: '仓库电话', span: 12, itemRender: { name: '$input' } },
					{ field: 'warehouse_remark', title: '仓库备注', span: 12, itemRender: { name: '$input' } },
					{ field: 'warehouse_negative_stock', title: '仓库是否允许负库存', span: 12, itemRender: { name: '$select',props:{options:settings.options_warehouse_negative_stock} } },
					{ field: 'warehouse_location', title: '是否启用库位管理', span: 12, itemRender: { name: '$select',props:{options:settings.options_is_open} } },
					{ field: 'warehouse_batch', title: '是否启用批次管理', span: 12, itemRender: { name: '$select',props:{options:settings.options_is_open} } },
					{ field: 'warehouse_serial', title: '是否启用序列号', span: 12, itemRender: { name: '$select',props:{options:settings.options_is_open} } },
					{ field: 'warehouse_storage_type', title: '存储类型', span: 12, itemRender: { name: '$select',props:{options:settings.options_warehouse_storage_type} } },
					{ field: 'warehouse_effective_date',dataRule:{type:"timestamp"}, title: '仓库生效日期', span: 12, itemRender: { name: '$input',props:{type:"date"} } },
					{ field: 'warehouse_invalid_date', dataRule:{type:"timestamp"},title: '仓库失效日期', span: 12, itemRender: { name: '$input',props:{type:"date"} } },
					{ field: 'warehouse_status', title: '仓库状态', span: 12, itemRender: { name: '$select',props:{options:settings.options_is_enabled} } },

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

        ],


      };

    pageData.actions.get = pageData.actions.MesWarehouseDetail;
    pageData.actions.create = pageData.actions.MesWarehouseCreate;
    pageData.actions.update = pageData.actions.MesWarehouseUpdate;

    // 合并表单数据及配置
    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].warehouse_effective_date){
						a.data[detailDataFieldName].warehouse_effective_date = new Date(a.data[detailDataFieldName].warehouse_effective_date * 1000);
					}
					if(a.data[detailDataFieldName].warehouse_invalid_date){
						a.data[detailDataFieldName].warehouse_invalid_date = new Date(a.data[detailDataFieldName].warehouse_invalid_date * 1000);
					}
          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>
 