<!--
 * @Description: 新增/编辑物料弹窗
 * @Author:zyy
 * @Date: 2021-03-19 13.:13:30
-->
<template>
  <div>
    <el-dialog
      v-dialogDrag
      :visible.sync="dialogData.consignContractVisible"
      :close-on-click-modal="false"
      :lock-scroll="false"
      :title="dialogData.handleType == 'edit' ? '新增物料' : '编辑物料' "
      width="986px"
    >
      <div class="p0-16 mb10">
        <el-form
          ref="formData"
          :model="formData"
          :rules="rules"
          :inline-message="true"
        >
          <table class="from_table">
            <tr>
              <td> <el-form-item label="物料编码" /></td>
              <td>
                <gx-input
                  v-model.trim="formData.purchaseCode"
                  gx-maxlength="varchar50"
                  placeholder=""
                  :disabled="true"
                />
              </td>
              <td>
                <el-form-item label="物料名称" prop="purchaseName" />
              </td>
              <td>
                <gx-input
                  v-model.trim="formData.purchaseName"
                  gx-maxlength="varchar50"
                  placeholder=""
                />
              </td>
            </tr>
            <tr>
              <td> <el-form-item label="物资规格/型号" prop="purchaseSpecification" /></td>
              <td colspan="3">
                <gx-input
                  v-model.trim="formData.purchaseSpecification"
                  gx-maxlength="varchar50"
                  placeholder=""
                  style="width: calc(100% - 30px );"
                />
              </td>
            </tr>
            <tr>
              <td>
                <el-form-item label="品牌" prop="purchaseBrand" />
              </td>
              <td colspan="3">
                <gx-input
                  v-model.trim="formData.purchaseBrand"
                  placeholder=""
                  gx-maxlength="varchar500"
                  style="width: calc(100% - 30px );"
                />
              </td>
            </tr>
            <tr>
              <td>
                <el-form-item label="计量单位" prop="purchaseUnit" />
              </td>
              <td>
                <el-select v-model="formData.purchaseUnit" filterable placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="item in purchaseUnitList"
                    :key="item.typecode"
                    :label="item.typename"
                    :value="item.typecode"
                  />
                </el-select>
              </td>
              <td> <el-form-item label="" /></td>
              <td>&nbsp;</td>
            </tr>
          </table>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer" style="margin-top: 10px;">
        <el-button type="primary" :loading="confirmLoading" @click="okdialog('formData')">确认</el-button>
        <el-button @click="closeOf">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
/* api */
import {
  getProtPlanMaterialStoreInfoPurchaseCode, // 获取物资编码
  saveOrUpdateProtPlanMaterialStoreInfo // 录入或是更新物料信息
} from '@/api/procurement/purchasplan'
/** 公共方法 */
import { filterDictText } from '@/assets/js/utils'// 常用方法
import { procurementPlanGrounpMixins } from '@/views/procurement/procurementPlan/mixins/procurementPlanGroupMixins' // 公共字典
import { procurementMixins } from '@/views/procurement/mixins/procurementMixins'
export default {
  name: 'EntryProjectInfoDialog',
  components: {
  },
  mixins: [procurementPlanGrounpMixins, procurementMixins],
  props: {
    dialogData: {
      type: Object,
      default: function() {
        return {
          consignContractVisible: false,
          smallCode: '', // 物资小类编码
          smallName: '', // 物资小类名称
          handleType: '', // 操作类型，add 新增，edit 编辑
          id: '' // 物资类ID
        }
      }
    },
    // 计划id
    planmainid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      confirmLoading: false, // btnLoading
      formData: {
        purchaseUnit: '', // 计量单位
        purchaseCode: '', // 物资编码
        purchaseName: '', // 物料名称
        purchaseSpecification: '', // 物资规格/规格型号
        purchaseBrand: '' // 品牌
      },
      // purchaseClassification: '', // 专业分类（同物资小类名称)
      // 校验规则
      rules: {
        purchaseName: [{ required: true, message: '不能为空', trigger: 'blur' }], // 物资名称
        purchaseCode: [{ required: true, message: '不能为空', trigger: 'blur' }], // 物料编码
        purchaseSpecification: [{ required: true, message: '不能为空', trigger: 'blur' }], // 物资规格/规格型号
        purchaseUnit: [{ required: true, message: '请选择', trigger: 'change' }] // 计量单位
        // purchaseBrand: [{ required: true, message: '不能为空', trigger: 'blur' }] // 品牌
      }
    }
  },
  created() {
    // 获取物料编码
    this.getPurchaseCode()
  },
  methods: {
    filterDictText, // 字典检索
    // 获取物料编码
    getPurchaseCode() {
      // edit 编辑
      if (this.dialogData.handleType === 'edit') {
        this.formData.purchaseUnit = this.dialogData.purchaseUnit // 计量单位
        this.formData.purchaseCode = this.dialogData.purchaseCode // 物资编码
        this.formData.purchaseName = this.dialogData.purchaseName // 物料名称
        this.formData.purchaseSpecification = this.dialogData.purchaseSpecification // 物资规格/规格型号
        this.formData.purchaseBrand = this.dialogData.purchaseBrand // 品牌
      } else {
        const data = {
          smallCode: this.dialogData.smallCode // 物资小类编码
        }
        getProtPlanMaterialStoreInfoPurchaseCode(data).then(res => {
          const data = res.data
          this.formData.purchaseCode = data
        })
      }
    },
    closeOf() {
      // this.$emit('saveconsignContractInfo', true)
      this.dialogData.consignContractVisible = false
    },
    // 确认
    okdialog(formName) {
      this.confirmLoading = true // btnLoding
      const data = {
        smallCode: this.dialogData.smallCode, // 物资小类编码
        smallName: this.dialogData.smallName, // 物资小类名称
        id: this.dialogData.handleType === 'edit' ? this.dialogData.id : '', // 物资类ID
        purchaseUnit: this.formData.purchaseUnit, // 计量单位
        purchaseCode: this.formData.purchaseCode, // 物资编码
        purchaseName: this.formData.purchaseName, // 物料名称
        purchaseSpecification: this.formData.purchaseSpecification, // 物资规格/规格型号
        purchaseBrand: this.formData.purchaseBrand // 品牌
      }
      this.$refs[formName].validate((valid, field) => {
        if (valid) { // 校验通过
          saveOrUpdateProtPlanMaterialStoreInfo(data).then(res => {
            this.$emit('onClose', true)
            this.dialogData.consignContractVisible = false
          }).catch(() => {
            this.confirmLoading = true // btnLoding
          })
        } else {
          this.confirmLoading = false // btnLoding
          this.$message.warning('请注意页面提示信息')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
