<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="条形码" prop="barCode">
          <el-input v-model="formData.barCode" placeholder="请输入条形码"/>
        </el-form-item>
        <el-form-item label="商品编码" prop="code">
          <el-input v-model="formData.code" placeholder="请输入商品编码"/>
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="规格型号" prop="model">
          <el-input v-model="formData.model" placeholder="请输入规格型号"/>
        </el-form-item>
        <el-form-item label="销售价格" prop="price">
          <el-input v-model="formData.price" placeholder="请输入销售价格"/>
        </el-form-item>
        <el-form-item label="所属分类" prop="type">
          <el-select v-model="formData.type" placeholder="请选择所属分类">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="计量单位" prop="unit">
          <el-input v-model="formData.unit" placeholder="请输入计量单位"/>
        </el-form-item>
        <el-form-item label="默认仓库" prop="warehouse">
          <el-input v-model="formData.warehouse" placeholder="请输入默认仓库"/>
        </el-form-item>
        <el-form-item label="最低库存" prop="miniStock">
          <el-input v-model="formData.miniStock" placeholder="请输入最低库存"/>
        </el-form-item>
        <el-form-item label="最高库存" prop="maxStock">
          <el-input v-model="formData.maxStock" placeholder="请输入最高库存"/>
        </el-form-item>
        <el-form-item label="当前库存" prop="stock">
          <el-input v-model="formData.stock" placeholder="请输入当前库存"/>
        </el-form-item>
        <el-form-item label="产品图片" prop="img">
          <el-input v-model="formData.img" placeholder="请输入产品图片"/>
        </el-form-item>
        <el-form-item label="配件属性" prop="attribute">
          <el-input v-model="formData.attribute" placeholder="请输入配件属性"/>
        </el-form-item>
        <el-form-item label="适用子公司" prop="corpId">
          <el-input v-model="formData.corpId" placeholder="请输入适用子公司"/>
        </el-form-item>
        <el-form-item label="封面图片" prop="coverImg">
          <el-input v-model="formData.coverImg" placeholder="请输入封面图片"/>
        </el-form-item>
        <el-form-item label="适用车型" prop="carModel">
          <el-input v-model="formData.carModel" placeholder="请输入适用车型"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as WaresApi from '@/api/repair/wares';

export default {
  name: "WaresForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        barCode: undefined,
        code: undefined,
        name: undefined,
        model: undefined,
        price: undefined,
        type: undefined,
        unit: undefined,
        warehouse: undefined,
        miniStock: undefined,
        maxStock: undefined,
        stock: undefined,
        img: undefined,
        attribute: undefined,
        corpId: undefined,
        coverImg: undefined,
        carModel: undefined,
        remark: undefined,
      },
      // 表单校验
      formRules: {},
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await WaresApi.getWares(id);
          this.formData = res.data;
          this.title = "修改配件库";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增配件库";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await WaresApi.updateWares(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await WaresApi.createWares(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        barCode: undefined,
        code: undefined,
        name: undefined,
        model: undefined,
        price: undefined,
        type: undefined,
        unit: undefined,
        warehouse: undefined,
        miniStock: undefined,
        maxStock: undefined,
        stock: undefined,
        img: undefined,
        attribute: undefined,
        corpId: undefined,
        coverImg: undefined,
        carModel: undefined,
        remark: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
