<template>
  <basic-page-edit :desc="desc" :dataId="getDataId()" @callback="handleCallback" :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: {

    handleCallback(e) {

      this.$emit("callback", e);
    },


    optionsInit() {


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


        uploadDefaultImg: null,
        detailDataFieldName: "production_order_array",

        actions: {
          get: `${BASE_URL.BASE_URL}/MesProductionOrder/v1/production/order/array/detail`,
          create: `${BASE_URL.BASE_URL}/MesProductionOrder/v1/production/order/array/create`,
          update: `${BASE_URL.BASE_URL}/MesProductionOrder/v1/production/order/array/update`,

        }, 

        saveIgFields:['create','update','update_time','create_time','create_uid','update_uid'],
        keyName: 'id',
        // 是否编辑模式
        isEdit: false,
        // 表单数据
        formOptions: {
          data: {

            batch_no: ''

          },
          // 标题宽度
          titleWidth: 100,
          // 标题对齐方式
          titleAlign: 'right',

          // 表单校验规则
          rules: {

            batch_no: [
              { required: true, message: '请输入批次编号' }
            ]

          },
          // 表单项
          items: [

            { field: 'batch_no', title: '批次编号', span: 12, itemRender: { name: '$input' } },

            { field: 'start_time', dataRule: { type: "timestamp" }, title: '开始日期', span: 12, itemRender: { name: '$input', props: { type: 'date' } } },
            { field: 'pick_time', dataRule: { type: "timestamp" }, title: '领料日期', span: 12, itemRender: { name: '$input', props: { type: 'date' } } },
            { field: 'finish_time', dataRule: { type: "timestamp" }, title: '结束日期', span: 12, itemRender: { name: '$input', props: { type: 'date' } } },


            {
              title: '料品', span: 12,
              field: 'mes_materials',
              dataRule: { 
                getter:(data)=>{
                  return {
                    id : data.materials_id,
                    name : data.materials_name
                  }
                },
                fromField: "id",
                saveField: "materials_id" // 如果表单项存储的是对象，将从fromField中取得值保存到saveField
              },
              itemRender: {
                name: 'MkFormDataSelector', props: {
                  params: {
                    dataType: "object",
                    valueField: "id",
                    textField: "name",
                    listdataFieldName: 'MesMaterials',
                    dataUrl: `${BASE_URL.BASE_URL}/MesMaterials/v1/mes/materials/list`,
                    popup: {
                      page: () => import("../../MesMaterials/MesMaterials/List")
                    }
                  }
                }
              }
            },

            {
              title: '布产单', span: 12,
              field: 'production_order',
              dataRule: {
                getter:(data)=>{
                  return {
                    id : data.production_order_id,
                    production_number : data.production_number
                  }
                },
                fromField: "id",
                saveField: "production_order_id" // 如果表单项存储的是对象，将从fromField中取得值保存到saveField
              },
              itemRender: {
                name: 'MkFormDataSelector', props: {
                  params: {
                    dataType: "object",
                    valueField: "id",
                    textField: "production_number",
                    listdataFieldName: 'MesProductionOrder',
                    columns:[
                    {field:"production_number",title:"布产单编号"}
                    ],
                    dataUrl: `${BASE_URL.BASE_URL}/MesProductionOrder/v1/mes/production/order/list`,
                    popup: {
                      page: () => import("../../MesProductionOrder/MesProductionOrder/List")
                    }
                  }
                }
              }
            },

          ]
        },
        // 新增模式表单项
        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 => {

          let data = a.data[this.pageOptions.detailDataFieldName]

          // 格式化要展示的数据
          this.$mk.formatInitFormData({ data: data, rules: this.pageOptions.formOptions.items });
 
          this.pageOptions.formOptions.data = data;
console.log(data)

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

        this.pageOptions.isEdit = true;

        this.$forceUpdate()
      }
    },

    // 获取路由的id参数
    getDataId() {
      return this.dataId;
    }
  },
  // 监听属性
  watch: {

  }
};

</script>
 