<template> 
    <div class="page-body">

      <vxe-form :data="formOptions.data" ref="xForm" :title-width="formOptions.titleWidth"
                :title-align="formOptions.titleAlign" :rules="formOptions.rules" :items="formOptions.items" titleColon>
      <!--  =========================== 自定义区域 Start  ======================= -->

      <!--  =========================== 自定义区域 End  ======================= -->
      </vxe-form>

      <div class="mk-toolbar" v-if="isEdit">

      </div>

      <div class="footerbar">
        <a-button type="primary" @click="ok">确定</a-button>
        <a-button @click="cancel">取消</a-button>
      </div>

    </div> 
</template>

<script>
import BASE_URL from '@/services/Middle/bathroom/BathroomPackaging/api.js';
//const statusOptions = [];

export default {

  name: 'BathroomPackagingUpdate',
  i18n: require('./i18n'),
  components: {  },
  props: {
    pageMode: {
      type: String,
      default: "edit"
    },
    dataId: {
    }

  },

  data() {

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

      uploadDefaultImg: null,
      detailDataFieldName: "bathroom_packaging",

      actions: {
        // =============================== 接口地址 自动生成 Start ===============================
					BathroomPackagingList: `${BASE_URL.BASE_URL}/BathroomPackaging/v1/bathroom/packaging/list`, // 包装材料列表
					BathroomPackagingDetail: `${BASE_URL.BASE_URL}/BathroomPackaging/v1/bathroom/packaging/detail`, // 包装材料详情
					BathroomPackagingCreate: `${BASE_URL.BASE_URL}/BathroomPackaging/v1/bathroom/packaging/create`, // 创建包装材料
					BathroomPackagingUpdate: `${BASE_URL.BASE_URL}/BathroomPackaging/v1/bathroom/packaging/update`, // 更新包装材料
					BathroomPackagingDelete: `${BASE_URL.BASE_URL}/BathroomPackaging/v1/bathroom/packaging/detele`, // 删除包装材料
					BathroomPackagingBatchDelete: `${BASE_URL.BASE_URL}/BathroomPackaging/v1/bathroom/packaging/batchDelete`, // 批量删除包装材料
					BathroomPackagingBatchUpdate: `${BASE_URL.BASE_URL}/BathroomPackaging/v1/bathroom/packaging/batchUpdate`, // 批量更新包装材料
					BathroomPackagingBatchCreate: `${BASE_URL.BASE_URL}/BathroomPackaging/v1/bathroom/packaging/batchCreate`, // 批量创建包装材料
					BathroomPackagingExportExcel: `${BASE_URL.BASE_URL}/BathroomPackaging/v1/bathroom/packaging/export/excel`, // 导出包装材料数据到EXCEL
					BathroomPackagingImportExcel: `${BASE_URL.BASE_URL}/BathroomPackaging/v1/bathroom/packaging/import/excel`, // 批量导入EXCEL包装材料数据
					BathroomPackagingOpen: `${BASE_URL.BASE_URL}/BathroomPackaging/v1/bathroom/packaging/open`, // 打开包装材料
					BathroomPackagingClose: `${BASE_URL.BASE_URL}/BathroomPackaging/v1/bathroom/packaging/close`, // 关闭包装材料

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

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

					id: 0,
					code: "",
					name: "",
					type: "",
					unit: "",
					valuation: 0,
					image: "",
					remark: "",
					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 ===============================
 
					name: [
						{ required: true, message: '请输入包装材料名称' }
					] 

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

					{ field: 'name', title: '包装材料名称', span: 24, itemRender: { name: '$input' } },
					{ field: 'code', title: '编码', span: 24, itemRender: { name: '$input' } },
				//	{ field: 'type', title: '包装材料类型', span: 12, itemRender: { name: '$input' } },
					{ field: 'unit', title: '包装材料单位', span: 24, itemRender: { name: '$input' } },
					{ field: 'default_quantity', title: '默认数量', span: 24, itemRender: { name: '$input' } },

          
				//	{ field: 'valuation', title: '计价方式: 1.按重量计价,2.按体积计价,3.按数量计价,4.按面积计价,5.按长度计价', span: 12, itemRender: { name: '$input' } },
				//	{ field: 'image', title: '图片', span: 12, itemRender: { name: '$input' } },
					{ field: 'remark', title: '备注', span: 24, itemRender: { name: '$input' } },
				//	{ field: 'status', title: '状态', span: 12, itemRender: { name: '$select', props: { options: statusOptions } } },

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

      ],


    };

    pageData.actions.get = pageData.actions.BathroomPackagingDetail;
    pageData.actions.create = pageData.actions.BathroomPackagingCreate;
    pageData.actions.update = pageData.actions.BathroomPackagingUpdate;

    // 合并表单数据及配置
    pageData.formOptions = Object.assign({}, this.$mk.config.defaults.formOptions, pageData.formOptions);

    return pageData;
  },

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


  created() {


    // 获取路由的id参数
    let dataId = this.getDataId();

    // 如果有id参数，说明是编辑模式
    if (dataId) {
      const json = `{"id":${dataId}}`;
      this.$mk.post({
        url: this.actions.get,
        loading: "加载中...",
        data: json,
        config: {
          headers: {
            'Content-Type': 'application/json'
          }
        }
      }).then(a => {
        // =============================== 时间字段判断 自动生成 Start ===============================

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

        // =============================== 时间字段判断 自动生成 End ===============================
        this.formOptions.data = a.data[this.detailDataFieldName];
      }).catch((a) => {
        this.$mk.error(a.data.msg);
      });

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

    }

  },
  // 函数
  methods: {


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



    // 返回
    back() {
      // 如果是新增模式，关闭当前页面
      if (!this.isEdit) {
        this.$closePage({
          closeRoute: "/BathroomPackaging/BathroomPackagingAdd"
        });
      } else {
        // 如果是编辑模式，关闭当前页面
        this.$closePage({
          closeRoute: "/BathroomPackaging/BathroomPackagingUpdate"
        });
      }
      // 打开列表页面
      this.$openPage('/BathroomPackaging/BathroomPackagingList')
    },

    // 保存
    ok() {

      let save = () => {


        // 如果是新增模式，提交新增接口 如果是编辑模式，提交编辑接口
        let action = !this.isEdit ? this.actions.create : this.actions.update;
        // 如果是新增模式，提交的数据中加入id 如果是编辑模式，提交的数据中不加入id
        let postdata = Object.assign({}, this.formOptions.data);
        // 如果是编辑模式
        if (this.isEdit) {
          // postdata = { BathroomPackaging: postdata }
        }
        // 格式化提交的数据
        this.$mk.formatFormData({ data: postdata, rules: this.formOptions.items });


        // 提交数据
        this.$mk.post({
          url: action,
          loading: "保存中...",
          data: postdata,
          useBigInt: true,
        }).then(() => { // 成功回调
          this.$mk.success("保存成功");
          if (!this.isEdit) { // 如果是新增模式，关闭当前页面
            this.back();
          }
        }).catch((a) => { // 失败回调
          this.$mk.error(a.data.msg); // 显示错误信息
        });


      };


      // 验证表单
      this.$mk.validateForm({ form: this.$refs.xForm }).then(() => { // 验证表单
        save(); // 提交保存
      }).catch(count => { // 验证失败
        this.$mk.error(`存在${count}项错误，请检查`);
      });


    },


    // 取消 返回
    cancel() {
      this.back();
    }
  },
  // 监听属性
  watch: {

  }
};

</script>


<style scoped lang="less">
.page-body {
  padding: 30px;
  background: @base-bg-color;
}

.formtabs .ant-tabs-tabpane {
  /* background: white; */
  padding: 12px;
}

.gridPanel {
  height: calc(100vh - 600px);
}

.footerbar {
  padding-left: 10px;
}

.imagePanel {
  cursor: pointer;
  padding: 10px;
  width: 100px;
  img {
    width: 80px;
    height: 80px;
  }
}
</style>
