<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.alternative" label="物料清单代替类型" :rules="form.rules.alternativeRules"></v-text-field> -->
    <v-text-field v-model="form.model.nId" label="编码" :rules="form.rules.nIdRules"></v-text-field>

    <v-text-field v-model="form.model.name" label="名称" :rules="form.rules.nameRules"></v-text-field>

    <v-text-field v-model="form.model.description" label="详情" :rules="form.rules.descriptionRules"></v-text-field>

    <v-text-field v-model="form.model.sequence" label="排序" :rules="form.rules.sequenceRules"></v-text-field>

    <nm-materialgroup-combobox v-model="form.model.materialGroup" :model="form.model.materialGroup" :disabled="gp_disabled"></nm-materialgroup-combobox>

    <nm-material-combobox v-model="form.model.materialNId" :model="form.model.materialNId" :disabled="mn_disabled" :returnObject="returnObject"></nm-material-combobox>

    <v-checkbox v-model="form.model.getMaterialRevision" label="选择当前物料版本" @click="materialclick" :disabled="ckmt_disabled"> </v-checkbox>

    <v-text-field v-model="form.model.materialRevision" label="物料版本" :rules="form.rules.materialRevisionRules" :disabled="mt_disabled"></v-text-field>

    <nm-Operation v-model="form.model.operationNId"></nm-Operation>

    <nm-OperationRevision v-model="form.model.operationRevision" :OperationNid="form.model.operationNId"></nm-OperationRevision>

    <!-- <nm-unit-combobox v-model="form.model.uoMNId" :model="form.model.uoMNId" :unitDataRules="form.rules.uoMNIdRules"></nm-unit-combobox> -->

    <v-text-field v-model="form.model.quantity" label="数量" :rules="form.rules.quantityRules"></v-text-field>

    <v-select
      v-model="form.model.useBillOfMaterials"
      :rules="form.rules.useBillOfMaterialsRules"
      :items="typeData"
      item-text="name"
      item-value="nId"
      label="是否使用物料"
      :disabled="us_disabled"
    ></v-select>

    <!-- <v-text-field v-model="form.model.materialNId" label="物料编码" :rules="form.rules.materialNIdRules"></v-text-field>
    <v-text-field v-model="form.model.materialGroup" label="物料组" :rules="form.rules.materialGroupRules"></v-text-field> -->

    <!-- 默认 -->
    <!-- <v-text-field v-model="form.model.billOfMaterials" label="物料清单" :rules="form.rules.billOfMaterialsRules"></v-text-field> -->

    <!-- <v-text-field v-model="form.model.useBillOfMaterials" label="是否按原物料使用" :rules="form.rules.useBillOfMaterialsRules"></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.billOfMaterialsItem
// const api = $api.materialApp.billOfMaterialsItem

export default {
  mixins: [mixins.formSave],
  props: {
    billOfMaterialId: {
      type: String,
      required: false
    }
  },
  data() {
    return {
      //物料版本
      mt_disabled: false,
      //工序版本
      op_disabled: false,
      //物料编码
      mn_disabled: false,
      //物料组
      gp_disabled: false,
      //物料版本checkbox
      ckmt_disabled: false,
      //工序版本checkbox
      ckop_disabled: false,
      //是否使用物料
      us_disabled: false,
      isMultiple: false,
      returnObject: true,
      //固定下拉
      typeData: [
        { name: 'Never', nId: '0' },
        { name: 'IfAvailable', nId: '1' },
        { name: 'Always', nId: '2' }
      ],
      title: '物料清单项',
      actions: { add, edit, update },
      parent: this,
      form: {
        model: {
          /** 编码 */
          nId: '',
          /** 名称 */
          name: '',
          /** 详情 */
          description: '',
          /** 物料清单代替类型 */
          alternative: '',
          /** 物料编码 */
          materialNId: '',
          /** 物料版本 */
          materialRevision: '',
          /** 数量 */
          quantity: '',
          /** 排序 */
          sequence: '',
          /** 工序编码 */
          operationNId: '',
          /** 工序版本 */
          operationRevision: '',
          /** 物料组 */
          materialGroup: '',
          /** 物料清单 */
          billOfMaterials: this.billOfMaterialId,
          /** 是否按原物料使用 */
          useBillOfMaterials: '',
          /** 是否锁定 */
          isLocked: false,
          /** 是否冻结 */
          isFrozen: false
        },
        rules: {
          nIdRules: [v => !!v || '编码 is required'],
          nameRules: [],
          descriptionRules: [],
          alternativeRules: [v => !!v || '物料清单代替类型 is required'],
          materialNIdRules: [],
          materialRevisionRules: [],
          quantityRules: [v => /^[\d\s]*$/.test(v) || '只能是数字'],
          sequenceRules: [v => /^[\d\s]*$/.test(v) || '只能是数字'],
          operationNIdRules: [],
          operationRevisionRules: [],
          materialGroupRules: [],
          billOfMaterialsRules: [v => !!v || '物料清单 is required'],
          useBillOfMaterialsRules: [],
          isLockedRules: [],
          isFrozenRules: []
        }
      }
    }
  },
  methods: {
    customResetFunction() {
      this.form.model.nId = ''
      this.form.model.name = ''
      this.form.model.description = ''
      this.form.model.alternative = ''
      this.form.model.materialNId = null
      this.form.model.materialRevision = null
      this.form.model.quantity = ''
      this.form.model.sequence = ''
      this.form.model.operationNId = null
      this.form.model.operationRevision = null
      this.form.model.materialGroup = null
      this.form.model.billOfMaterials = this.billOfMaterialId
      this.form.model.useBillOfMaterials = ''
    },
    materialclick() {
      if (this.form.model.getMaterialRevision == true) {
        this.form.model.materialRevision = null
        this.mt_disabled = true
      } else {
        this.mt_disabled = false
      }
    }
  },
  mounted() {},
  watch: {
    //checkbox工序版本
    'form.model.getOperationRevision': function(val) {
      if (val == true) {
        this.op_disabled = true
      } else {
        this.op_disabled = false
      }
    },
    //物料下拉
    'form.model.materialNId': function(val) {
      if (val != null) {
        this.gp_disabled = true
        this.form.model.alternative = 1
      } else {
        this.gp_disabled = false
      }
    },
    //物料组下拉
    'form.model.materialGroup': function(val) {
      if (val != null) {
        this.form.model.getMaterialRevision = false

        this.form.model.alternative = 0
        this.mt_disabled = true
        this.form.model.materialRevision = null
        this.mn_disabled = true
        this.ckmt_disabled = true
        this.form.model.materialNId = null
        this.form.model.useBillOfMaterials = false
        this.us_disabled = true
        this.mt_disabled = true
      } else {
        this.mt_disabled = false
        this.mn_disabled = false
        this.ckmt_disabled = false
        this.us_disabled = false
        this.mt_disabled = false
      }
    }
  }
}
</script>
