<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 Vue from 'vue'
import VXETable from 'vxe-table'

import BASE_URL from '@/services/Middle/bathroom/BathroomBanding/api.js';
import FormEditExpressions from '../../basic/FormEditExpressions.vue'
Vue.component(FormEditExpressions.name, FormEditExpressions)
VXETable.renderer.add('FormEditExpressions', {
	// 项内容模板
	renderItemContent(h, renderOpts, params) {
		const { data, field } = params
		const props = renderOpts.props || {}
		const options = renderOpts.options || [] 
		return <form-edit-expressions options={options} v-model={data[field]} {...{ props }}></form-edit-expressions>
	}
})

export default {

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

  },

  data() {

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

      uploadDefaultImg: null,
      detailDataFieldName: "bathroom_banding",

      actions: {
        // =============================== 接口地址 自动生成 Start ===============================
					BathroomBandingList: `${BASE_URL.BASE_URL}/BathroomBanding/v1/bathroom/banding/list`, // 浴室柜封边列表
					BathroomBandingDetail: `${BASE_URL.BASE_URL}/BathroomBanding/v1/bathroom/banding/detail`, // 浴室柜封边详情
					BathroomBandingCreate: `${BASE_URL.BASE_URL}/BathroomBanding/v1/bathroom/banding/create`, // 创建浴室柜封边
					BathroomBandingUpdate: `${BASE_URL.BASE_URL}/BathroomBanding/v1/bathroom/banding/update`, // 更新浴室柜封边
					BathroomBandingDelete: `${BASE_URL.BASE_URL}/BathroomBanding/v1/bathroom/banding/detele`, // 删除浴室柜封边
					BathroomBandingBatchDelete: `${BASE_URL.BASE_URL}/BathroomBanding/v1/bathroom/banding/batchDelete`, // 批量删除浴室柜封边
					BathroomBandingBatchUpdate: `${BASE_URL.BASE_URL}/BathroomBanding/v1/bathroom/banding/batchUpdate`, // 批量更新浴室柜封边
					BathroomBandingBatchCreate: `${BASE_URL.BASE_URL}/BathroomBanding/v1/bathroom/banding/batchCreate`, // 批量创建浴室柜封边
					BathroomBandingExportExcel: `${BASE_URL.BASE_URL}/BathroomBanding/v1/bathroom/banding/export/excel`, // 导出浴室柜封边数据到EXCEL
					BathroomBandingImportExcel: `${BASE_URL.BASE_URL}/BathroomBanding/v1/bathroom/banding/import/excel`, // 批量导入EXCEL浴室柜封边数据

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

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

					id: 0,
					code: "",
					name: "",
					type: "",
					spec: "",
					unit: "",
					formula: "",
					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: 130,
        // 标题对齐方式
        titleAlign: 'right',

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

				
					name: [
						{ required: true, message: '请输入封边名称' }
					],
					code: [
						{ required: true, message: '请输入人工费用方案编码' }
					]

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

					{ field: 'name', title: '封边名称', span: 24, itemRender: { name: '$input' } },
					{ field: 'formula', title: '封边用量公式', span: 24, itemRender: { name: 'FormEditExpressions' } },
					{ field: 'code', title: '编码', span: 24, itemRender: { name: '$input' } },
					{ field: 'type', title: '类型', span: 24, itemRender: { name: '$input' } },
					{ field: 'spec', title: '规格', span: 24, itemRender: { name: '$input' } },
					{ field: 'unit', title: '单位', span: 24, itemRender: { name: '$input' } },

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

      ],


    };

    pageData.actions.get = pageData.actions.BathroomBandingDetail;
    pageData.actions.create = pageData.actions.BathroomBandingCreate;
    pageData.actions.update = pageData.actions.BathroomBandingUpdate;

    // 合并表单数据及配置
    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: "/BathroomBanding/BathroomBandingAdd"
        });
      } else {
        // 如果是编辑模式，关闭当前页面
        this.$closePage({
          closeRoute: "/BathroomBanding/BathroomBandingUpdate"
        });
      }
      // 打开列表页面
      this.$openPage('/BathroomBanding/BathroomBandingList')
    },

    // 保存
    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 = { BathroomBanding: 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>
