<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>
    </vxe-form>


    <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/base/api.js';

export default {

  props: {
    dataId: {
    }

  },

  data() {

    // 页面数据变量
    var pageData = {

      actions: {
        create: `${BASE_URL.BASE_URL}/MesEnum/v1/mes/enum/create`,
        update: `${BASE_URL.BASE_URL}/MesEnum/v1/mes/enum/update`,
        get: `${BASE_URL.BASE_URL}/MesEnum/v1/mes/enum/detail`,
        list: `${BASE_URL.BASE_URL}/MesEnum/v1/mes/enum/list`,
      },


      formOptions: {
        data: {
          "name": "",
          "code": "",
          enum_id:"0"
        },

        titleWidth: 150,
        titleAlign: 'right',

        rules: {
          name: [
            { required: true, message: '请输入名称' }
          ]
        },

        items: [
          { field: 'name', title: '名称', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入名称' } } },
          { field: 'code', title: '编号', span: 24, itemRender: { name: '$input', props: { placeholder: '请输入编号' } } },
          { field: 'enum_id', title: '上级分类', span: 24, itemRender: { name: '$select', props: { placeholder: '请输入上级分类', options: [] } } }
        ]
      }

    };

    pageData.formOptions = Object.assign({}, this.$mk.config.defaults.formOptions, pageData.formOptions);

    return pageData;
  },
  created() {
    if (this.dataId) {
      this.$mk.post({
        url: this.actions.get,
        loading: "加载中...",
        data: {
          id: this.dataId
        },
        useBigInt: true
      }).then(a => {
        if(a.data.mes_enum.enum_id){
          a.data.mes_enum.enum_id =  a.data.mes_enum.enum_id.toString();
        }else{
          a.data.mes_enum.enum_id =  "0"
        }
        this.formOptions.data = a.data.mes_enum;
      }).catch((a) => {
        this.$mk.error(a.data.msg);
      });
    }
    this.loadEmuns();
  },
  // 函数
  methods: {

    loadEmuns() {
      this.$mk.post({
        url: this.actions.list,
        loading: "加载中...",
        data: {
          "page": 1,
          "limit": 10000,
          "end_time": 0,
          "start_time": 0,
          "order_bys": [
          ],
          "search_rules": [
            {
              column: "enum_id", // 字段名
              mode: "=", // 搜索模式
              value: "0"
            }
          ]
        },
        useBigInt: true
      }).then(a => { 

        let options_emuns = [{ value: "0", label: "选择上级分类" }];
        console.log(a.data.MesEnum)
        if (a.data.MesEnum) {
          a.data.MesEnum.forEach(item => {
             options_emuns.push({ value: item.id.toString(), id:item.id,  label: item.name });
          });
        } 
        this.formOptions.items[2].itemRender.props.options = options_emuns;

      });
    },

    loadData() {
    },

    ok() {

      let save = () => {
        let action = !this.dataId ? this.actions.create : this.actions.update;
        let postdata = Object.assign({ id: this.dataId }, this.formOptions.data);

        let options = this.formOptions.items[2].itemRender.props.options;
        options.forEach(item=>{
          if(item.value == postdata.enum_id){
            postdata.enum_id = item.id;
          }
        }); 
        this.$mk.post({
          url: action,
          loading: "保存中...",
          data: postdata,
          useBigInt: true
        }).then(() => {
          this.$mk.success("保存成功");
          this.$emit("callback", { success: true });
        }).catch((a) => {
          this.$mk.error(a.data.msg);
        });
      };

      this.$refs.xForm.validate((a) => {

        if (a) {
          let count = 0;
          for (let name in a) {
            a[name];
            count++;
          }
          this.$mk.error(`存在${count}项错误，请检查`);
        } else {
          save();
        }
      });


    },

    cancel() {
      this.$emit("callback", {});
    }
  },
  // 监听属性
  watch: {

  }
};

</script>
<style></style>
