<template>
  <nm-form-vuetify-drawer ref="form" v-bind="form" v-on="on" :parent="parent" :title="title" placement="left" :visible.sync="visible_">
    <v-text-field v-model="form.model.nId" label="属性名" :rules="form.rules.nIdRules"></v-text-field>

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

    <v-text-field v-model="form.model.propertyValue" label="属性值" :rules="form.rules.propertyValueRules"></v-text-field>

    <nm-unit-combobox v-model="form.model.propertyUoMNId" :disabled="disabled"></nm-unit-combobox>
  </nm-form-vuetify-drawer>
</template>
<script>
import { mixins } from 'xmanager-ui'

const { add, edit, update } = $api.materialApp.materialProperty
const api = $api.materialApp.materialProperty

export default {
  mixins: [mixins.formSave],
  props: {
    materialId: {
      type: String,
      required: false
    }
  },
  data() {
    return {
      disabled: false,
      title: '物料属性',
      actions: { add, edit, update },
      parent: this,
      form: {
        model: {
          /** 物料 */
          material: this.materialId,
          /** 主键 */
          nId: '',
          /** 属性值 */
          propertyValue: '',
          /** 属性类型 */
          propertyType: '',
          name: ''
          /** 属性关联id */
          // propertyUoMNId: ''
        },
        rules: {
          materialRules: [v => !!v || '物料 is required'],
          nIdRules: [v => !!v || '属性名称 is required'],
          propertyValueRules: [v => !!v || '属性值 is required'],
          propertyTypeRules: [v => !!v || '属性类型 is required'],
          nameRules: [v => !!v || '属性名称 is required']
          // propertyUoMNIdRules: [v => !!v || '属性关联id is required']
        }
      },
      typeData: [
        { name: 'string', nId: 'string' },
        { name: 'bool', nId: 'bool' },
        { name: 'int', nId: 'int' },
        { name: 'decimal', nId: 'decimal' },
        { name: 'quantity', nId: 'quantity' }
      ],
      materialData: [],
      isMultiple: false,
      returnObject: false
    }
  },
  methods: {
    getmaterialata() {
      api.getMaterial().then(res => {
        this.materialData = res.rows
      })
    },
    customResetFunction() {
      this.form.model.material = this.materialId
      this.form.model.nId = ''
      this.form.model.propertyValue = ''
      this.form.model.propertyType = ''
    }
  },
  watch: {
    // materialId: 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>
