<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" :rules="form.rules.propertyTypeRules" :items="typeData" item-text="name" item-value="nId" label="物料属性类型"></v-select>

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

    <!-- <v-text-field v-model="form.model.propertyType" label="属性类型" :rules="form.rules.propertyTypeRules"></v-text-field>

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

    <!-- <v-checkbox v-model="form.model.isLocked" label="是否锁定" :rules="form.rules.isLockedRules"> </v-checkbox>

    <v-checkbox v-model="form.model.isFrozen" label="是否冻结" :rules="form.rules.isFrozenRules"> </v-checkbox> -->
  </nm-form-vuetify-drawer>
</template>
<script>
import { mixins } from 'xmanager-ui'
const { add, edit, update } = $api.materialApp.billOfMaterialTemplateProperty

export default {
  mixins: [mixins.formSave],
  props: {
    billOfMaterialTemplate: {
      type: String,
      required: false
    }
  },
  data() {
    return {
      title: '物料清单模板扩展属性',
      actions: { add, edit, update },
      parent: this,
      typeData: [
        { name: 'string', nId: 'string' },
        { name: 'bool', nId: 'bool' },
        { name: 'int', nId: 'int' }
      ],
      form: {
        model: {
          /** 编码属性名 */
          nId: '',
          /** 属性类型 */
          propertyType: '',
          /** 属性值 */
          propertyValue: '',
          /** 是否锁定 */
          isLocked: false,
          /** 是否冻结 */
          isFrozen: false,
          /** 物料清单模板 */
          billOfMaterialTemplate: this.billOfMaterialTemplate
        },
        rules: {
          nIdRules: [v => !!v || '编码属性名 is required'],
          propertyTypeRules: [],
          propertyValueRules: [],
          isLockedRules: [],
          isFrozenRules: []
        }
      }
    }
  },
  methods: {
    customResetFunction() {
      this.form.model.description = ''
      this.form.model.nId = ''
      this.form.model.propertyType = ''
      this.form.model.propertyValue = ''
      this.form.model.billOfMaterialTemplate = this.billOfMaterialTemplate
    }
  },
  watch: {
    '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 {
        this.form.rules.propertyValueRules = [v => !!v || '值不能为空']
      }
    }
  }
}
</script>
