<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: "/ChatgptCard/ChatgptCardAdd",
        editPageUrl: "/ChatgptCard/ChatgptCardUpdate/",
        listPageUrl: "/ChatgptCard/ChatgptCardList",


        uploadDefaultImg: null,

        detailDataFieldName: "chatgpt_card",

        actions: {

          ChatgptCardDetail: `${BASE_URL.BASE_URL}/ChatgptCard/v1/chatgpt/card/detail`,  
          ChatgptCardCreate: `${BASE_URL.BASE_URL}/ChatgptCard/v1/chatgpt/card/create`, 
          ChatgptCardUpdate: `${BASE_URL.BASE_URL}/ChatgptCard/v1/chatgpt/card/update`,  
          ChatgptCardDelete: `${BASE_URL.BASE_URL}/ChatgptCard/v1/chatgpt/card/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: 'code',  title: '编码',span: 24 , itemRender: { name: '$input' } },
            { field: 'description',  title: '描述', span: 24, itemRender: { name: '$input' } },
            { field: 'count', dataRule:{type:"integer"},  title: '次数', span: 24 , itemRender: { name: '$input',props:{type:"integer"} } },
            { field: 'valid_time', dataRule: { type: "timestamp" },  title: '有效期',  span: 24 , itemRender: { name: '$input',props:{type:"date"} } },

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

        ],


      };


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

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



      pageData.actions.get = pageData.actions.ChatgptCardDetail;
      pageData.actions.create = pageData.actions.ChatgptCardCreate;
      pageData.actions.update = pageData.actions.ChatgptCardUpdate;
      // 合并表单数据及配置
      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>
 