<template>
  <nm-form-vuetify-drawer ref="form" v-bind="form" v-on="on" :parent="parent" :title="title" placement="left" :visible.sync="visible_">
    <!-- 物料select -->
    <!-- <nm-material-select v-model="form.model.propertyType"></nm-material-select> -->
    <!-- 物料entitypicker -->
    <!-- <nm-material-entitypicker v-model="form.model.templateNId" @materialinfo="materialinfos"></nm-material-entitypicker> -->
    <!-- 设备combobox -->
    <!-- <nm-equipment-combobox v-model="form.model.propertyType"></nm-equipment-combobox> -->
    <!-- 设备组combobox -->
    <!-- <nm-equipmentgroup-combobox v-model="form.model.propertyType"></nm-equipmentgroup-combobox> -->
    <!-- 物料清单entitypicker -->
    <!-- <nm-bom-entitypicker v-model="form.model.templateNId"></nm-bom-entitypicker> -->

    <v-text-field v-model="form.model.nId" label="属性名" :rules="form.rules.nIdRules"></v-text-field>

    <v-select v-model="form.model.propertyType" :rules="form.rules.propertyTypeRules" :items="typeData" item-text="name" item-value="nId" label="物料属性类型"></v-select>

    <v-text-field v-model="form.model.propertyValue" v-bind="propertyValue" label="物料值" :rules="form.rules.propertyValueRules"></v-text-field>

    <nm-unit-combobox v-model="form.model.propertyUoMNId" :disabled="disabled"></nm-unit-combobox>

    <!-- <v-text-field v-model="form.model.propertyUoMNId" label="物料属性单位" :rules="form.rules.propertyUoMNIdRules"></v-text-field> -->
  </nm-form-vuetify-drawer>
</template>
<script>
import { mixins } from 'xmanager-ui'
import cols from '../../../materialTemplate/index/cols'
const { add, edit, update } = $api.materialApp.materialTemplateProperty
const api = $api.materialApp.materialTemplateProperty
const materialapi = $api.materialApp.material
const materialTemplateapi = $api.materialApp.materialTemplate

export default {
  mixins: [mixins.formSave],
  props: {
    materialTemplateId: {
      type: String,
      required: false
    }
  },
  data() {
    return {
      disabled: true,
      entitycols: cols,
      entityaction: materialTemplateapi.query,
      propertyValue: null,
      panel: null,
      templateData: [],
      typeData: [
        { name: 'string', nId: 'string' },
        { name: 'bool', nId: 'bool' },
        { name: 'int', nId: 'int' },
        { name: 'decimal', nId: 'decimal' },
        { name: 'quantity', nId: 'quantity' }
      ],
      title: '物料模板属性',
      actions: { add, edit, update },
      parent: this,
      unitData: [],
      form: {
        model: {
          /** 物料模板 */
          materialTemplate: this.materialTemplateId,
          /** 主键id */
          nId: '',
          /** 物料值 */
          propertyValue: '',
          /** 物料属性类型 */
          propertyType: '',
          /** 物料属性单位id */
          propertyUoMNId: '',
          name: ''
        },
        rules: {
          materialTemplateRules: [v => !!v || '物料模板 is required'],
          nameRules: [v => !!v || '物料模板属性名 is required'],
          nIdRules: [v => !!v || '物料模板属性名 is required'],
          propertyValueRules: [],
          propertyTypeRules: [v => !!v || '物料属性类型 is required'],
          propertyUoMNIdRules: [],
          adnIdRules: [v => !!v || '主键id is required']
        }
      }
    }
  },
  methods: {
    customResetFunction() {
      this.form.model.materialTemplate = this.materialTemplateId
      this.form.model.propertyValue = ''
      this.form.model.propertyType = ''
      this.form.model.propertyUoMNId = null
      this.form.model.name = ''
      this.form.model.nId = ''
    },

    onclick() {
      // console.log(this.materialTemplateId)
    },
    gettemplatedata() {
      //模板
      api.gettemplate().then(res => {
        this.templateData = res.rows
      })
    },
    //获取单位
    getuomdata() {
      //单位
      materialapi.getUnit().then(res => {
        this.unitData = res.rows
      })
    }
  },
  mounted() {
    // console.log(this.materialTemplateId)
  },
  watch: {
    // materialTemplateId: function(val) {
    //   // console.log(val)
    // },
    'form.model.propertyValue': function() {
      if (this.form.model.propertyType == 'bool') {
        this.form.rules.propertyValueRules = [v => /^(TRUE)|^(FALSE)|^(true)|^(false)$/.test(v) || '值只能是true或false']
      } else if (this.form.model.propertyType == 'int') {
        this.form.rules.propertyValueRules = [v => !!v || '值不能为空', v => /^[0-9]+$/.test(v) || '只能是整数']
      } else if (this.form.model.propertyType == 'decimal') {
        this.form.rules.propertyValueRules = [v => !!v || '值不能为空', v => /^[1-9]\d{0,13}(\.\d{0,4})?$/.test(v) || '只能是数字']
      } else if (this.form.model.propertyType == 'quantity') {
        this.form.rules.propertyValueRules = [v => !!v || '值不能为空', v => /^[1-9]\d{0,13}(\.\d{0,4})?$/.test(v) || '只能是数字']
      } else {
        this.form.rules.propertyValueRules = [v => !!v || '值不能为空']
      }
    },
    'form.model.propertyType': function() {
      if (this.form.model.propertyType == 'quantity') {
        this.disabled = false
      } else {
        this.form.model.propertyUoMNId = null
        this.disabled = true
      }
    }
  }
}
</script>
