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

      pageKeys: ['/MesProcesses/MesProcessesUpdate/', '/MesProcesses/MesProcessesAdd'],
      preventTabClose: true,
      pageOptions: {},
      lastFormDataJson: ""
    };
  },

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


  created() {

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

    setTimeout(() => {
      console.log(this)
    }, 1000);

  },
  // 函数
  methods: {



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

        //保存后提示
        action_afterSave: "confirm",

        addPageUrl: "/MesProcesses/MesProcessesAdd",
        editPageUrl: "/MesProcesses/MesProcessesUpdate/",
        listPageUrl: "/MesProcesses/MesProcessesList",

        uploadDefaultImg: null,
        detailDataFieldName: "mes_processes",

        actions: {
          // =============================== 接口地址 自动生成 Start ===============================
          MesProcessesList: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/list`, // 工序列表
          MesProcessesDetail: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/detail`, // 工序详情
          MesProcessesCreate: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/create`, // 创建工序
          MesProcessesUpdate: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/update`, // 更新工序
          MesProcessesDelete: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/detele`, // 删除工序
          MesProcessesBatchDelete: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/batchDelete`, // 批量删除工序
          MesProcessesBatchUpdate: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/batchUpdate`, // 批量更新工序
          MesProcessesBatchCreate: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/batchCreate`, // 批量创建工序
          MesProcessesExportExcel: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/export/excel`, // 导出工序数据到EXCEL
          MesProcessesImportExcel: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/import/excel`, // 批量导入EXCEL工序数据
          MesProcessesOpen: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/open`, // 打开工序
          MesProcessesClose: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/close`, // 关闭工序
          ProcessProgrammeList: `${BASE_URL.BASE_URL}/MesProcesses/v1/process/programme/list`, // 工序方案列表
          ProcessProgrammeDetail: `${BASE_URL.BASE_URL}/MesProcesses/v1/process/programme/detail`, // 工序方案详情
          ProcessProgrammeCreate: `${BASE_URL.BASE_URL}/MesProcesses/v1/process/programme/create`, // 创建工序方案
          ProcessProgrammeUpdate: `${BASE_URL.BASE_URL}/MesProcesses/v1/process/programme/update`, // 更新工序方案
          ProcessProgrammeDelete: `${BASE_URL.BASE_URL}/MesProcesses/v1/process/programme/detele`, // 删除工序方案
          ProcessProgrammeBatchDelete: `${BASE_URL.BASE_URL}/MesProcesses/v1/process/programme/batchDelete`, // 批量删除工序方案
          ProcessProgrammeBatchUpdate: `${BASE_URL.BASE_URL}/MesProcesses/v1/process/programme/batchUpdate`, // 批量更新工序方案
          ProcessProgrammeBatchCreate: `${BASE_URL.BASE_URL}/MesProcesses/v1/process/programme/batchCreate`, // 批量创建工序方案
          ProcessProgrammeExportExcel: `${BASE_URL.BASE_URL}/MesProcesses/v1/process/programme/export/excel`, // 导出工序方案数据到EXCEL
          ProcessProgrammeImportExcel: `${BASE_URL.BASE_URL}/MesProcesses/v1/process/programme/import/excel`, // 批量导入EXCEL工序方案数据
          ProgrammeItemList: `${BASE_URL.BASE_URL}/MesProcesses/v1/programme/item/list`, // 工序方案工序ID列表
          ProgrammeItemDetail: `${BASE_URL.BASE_URL}/MesProcesses/v1/programme/item/detail`, // 工序方案工序ID详情
          ProgrammeItemCreate: `${BASE_URL.BASE_URL}/MesProcesses/v1/programme/item/create`, // 创建工序方案工序ID
          ProgrammeItemUpdate: `${BASE_URL.BASE_URL}/MesProcesses/v1/programme/item/update`, // 更新工序方案工序ID
          ProgrammeItemDelete: `${BASE_URL.BASE_URL}/MesProcesses/v1/programme/item/detele`, // 删除工序方案工序ID
          ProgrammeItemBatchDelete: `${BASE_URL.BASE_URL}/MesProcesses/v1/programme/item/batchDelete`, // 批量删除工序方案工序ID
          ProgrammeItemBatchUpdate: `${BASE_URL.BASE_URL}/MesProcesses/v1/programme/item/batchUpdate`, // 批量更新工序方案工序ID
          ProgrammeItemBatchCreate: `${BASE_URL.BASE_URL}/MesProcesses/v1/programme/item/batchCreate`, // 批量创建工序方案工序ID
          ProgrammeItemExportExcel: `${BASE_URL.BASE_URL}/MesProcesses/v1/programme/item/export/excel`, // 导出工序方案工序ID数据到EXCEL
          ProgrammeItemImportExcel: `${BASE_URL.BASE_URL}/MesProcesses/v1/programme/item/import/excel`, // 批量导入EXCEL工序方案工序ID数据

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

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

            id: 0,
            process_segment_id: 0,
            title: "",
            code: "",
            name: "",
            desc: "",
            remark: "",
            work_type: "",
            car_limit: 0,
            is_quality_inspection: 0,
            quality_inspection_scheme_id: 0,
            raw_material_type_id: 0,
            status: 1,
            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 ===============================


            name: [
              { required: true, message: '请输入工序英文名称' }
            ]

            // =============================== 表单校验 自动生成 End ===============================
          },
          // 表单项
          items: [
            { field: 'name', title: '工序名称', span: 12, itemRender: { name: '$input' } },
            {
              title: '工序分类', span: 12,
              field: 'mes_processes_sort',
              dataRule: {
                fromField: "id",
                saveField: "sort_id" // 如果表单项存储的是对象，将从fromField中取得值保存到saveField
              },
              itemRender: {
                name: 'MkFormDataSelector', props: {
                  params: {
                    dataType: "object",
                    valueField: "id",
                    textField: "name",
                    listdataFieldName: 'MesProcessesSort',
                    dataUrl: `${BASE_URL.BASE_URL}/MesProcesses/v1/mes/processes/sort/list`
                  }
                }
              }
            },

            {
              title: '部门', span: 12,
              field: 'department_detail',
              dataRule: {
                fromField: "id",
                saveField: "department_id" // 如果表单项存储的是对象，将从fromField中取得值保存到saveField
              },
              itemRender: {
                name: 'MkFormDataSelector', props: {
                  params: {
                    dataType: "object",
                    valueField: "id",
                    textField: "name",
                    listdataFieldName: 'BaseDepartment',
                    dataUrl: `${BASE_URL.BASE_URL}/BaseDepartment/v1/base/department/list`
                  }
                }
              }
            },

            { field: 'code', title: '工序编码', span: 12, itemRender: { name: '$input' } },
            { field: 'desc', title: '工序描述', span: 12, itemRender: { name: '$input' } },
            { field: 'remark', title: '工序备注', span: 12, itemRender: { name: '$input' } },

            { field: 'work_type', title: '计工方式', span: 12, itemRender: { name: '$select', props: { options: settings.options_work_type } } },
            { field: 'car_limit', title: '领车限制数', span: 12, itemRender: { name: '$input', props: { type: 'number' } } },
            { field: 'is_quality_inspection', title: '是否质检工序', span: 12, itemRender: { name: '$select', props: { options: settings.options_yesorno } } },
            { field: 'status', title: '状态', span: 12, itemRender: { name: '$select', props: { options: settings.options_status_process } } },
          ]
        },
        // 新增模式表单项
        addModeItems: [
        ],

      };

      pageData.actions.get = pageData.actions.MesProcessesDetail;
      pageData.actions.create = pageData.actions.MesProcessesCreate;
      pageData.actions.update = pageData.actions.MesProcessesUpdate;
      // 合并表单数据及配置
      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.lastFormDataJson = JSON.stringify(this.pageOptions.formOptions.data);
          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);
        })

        this.lastFormDataJson = JSON.stringify(this.pageOptions.formOptions.data);

      }
    },

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

    afterSave({ data }) {

      this.lastFormDataJson = JSON.stringify(data);
    },
    isDataChanged() {

      return this.lastFormDataJson != JSON.stringify(this.pageOptions.formOptions.data);
    },
    beforeTabClose() {

      let _this = this;
      return new Promise((resolve) => {

        if (_this.isDataChanged()) {
          _this.$confirm({
            title: '提示',
            content: '当前内容未保存，是否保存',
            okText: '保存',
            cancelText: '取消',
            onOk() {
              _this.$refs.editpage.save({
                afterSave: () => {

                  resolve();
                }
              })

            },
            onCancel() {
              resolve();
            },
          });
        } else {
          resolve();
        }


      });

    },
  },
  // 监听属性
  watch: {

  }
};

</script>
 