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



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

        addPageUrl: "/ProvideLoanApply/ProvideLoanApplyAdd",
        editPageUrl: "/ProvideLoanApply/ProvideLoanApplyUpdate/",
        listPageUrl: "/ProvideLoanApply/ProvideLoanApplyList",


        uploadDefaultImg: null,

        detailDataFieldName: "provide_loan_apply",

        actions: {

          ProvideLoanApplyDetail: `${BASE_URL.BASE_URL}/ProvideLoanApply/v1/provide/loan/apply/detail`,  
          ProvideLoanApplyCreate: `${BASE_URL.BASE_URL}/ProvideLoanApply/v1/provide/loan/apply/create`, 
          ProvideLoanApplyUpdate: `${BASE_URL.BASE_URL}/ProvideLoanApply/v1/provide/loan/apply/update`,  
          ProvideLoanApplyDelete: `${BASE_URL.BASE_URL}/ProvideLoanApply/v1/provide/loan/apply/detele`, 


        },

        keyName: 'id',
        // 是否编辑模式
        isEdit: false,
        // 表单数据
        formOptions: {
          data: { 
          },
          // 标题宽度
          titleWidth: 150,
          // 标题对齐方式
          titleAlign: 'right',

          // 表单校验规则
          rules: { 

            name: [
              { required: true, message: '请输入套餐名' }
            ] 
 
          },
          // 表单项
          items: [ 
 

            { field: 'name',   title: '姓名',  span: 24, itemRender: { name: '$input' } },
            { field: 'phone',  title: '手机号',span: 24 , itemRender: { name: '$input' } },
            { field: 'car_type',  title: '车型', span: 24, itemRender: { name: '$input' } },
            { field: 'work_city',  title: '城市', span: 24, itemRender: { name: '$input' } }, 

          ]
        },
        // 新增模式表单项
        addModeItems: [

        ],


      };


      pageData.formOptions.items.forEach(item=>{

        pageData.formOptions.data[item.field] = "";
      });



      pageData.actions.get = pageData.actions.ProvideLoanApplyDetail;
      pageData.actions.create = pageData.actions.ProvideLoanApplyCreate;
      pageData.actions.update = pageData.actions.ProvideLoanApplyUpdate;
      // 合并表单数据及配置
      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);
          }
          if (a.data[detailDataFieldName].valid_time) {
            a.data[detailDataFieldName].valid_time = new Date(a.data[detailDataFieldName].valid_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>
 