<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="130px"
      >
        <el-form-item label="模具编码" prop="mouldCode">
          <el-input v-model="formData.mouldCode" placeholder="请输入模具编码" />
        </el-form-item>
        <el-form-item label="模具名称" prop="mouldName">
          <el-input v-model="formData.mouldName" placeholder="请输入模具名称" />
        </el-form-item>
        <el-form-item label="物料编码" prop="materialCode">
          <el-select
            v-model="formData.materialCode"
            placeholder="请选择供应商/客户编码"
            :loading="loading"
            filterable
            clearable
            remote
            :remote-method="getMaterialCodeList"
            @change="changeMaterialCodePage"
          >
            <el-option
              v-for="item in materialCodeList"
              :key="item.id"
              :label="item.code"
              :value="item.code"
            >
              <span style="float: left">{{ item.code }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{
                item.name
              }}</span></el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-select
            placeholder="请选择供应商/客户名称"
            v-model="formData.materialName"
            :loading="loading"
            filterable
            clearable
            remote
            :remote-method="getMaterialNameList"
            @change="changeMaterialNamePage"
          >
            <el-option
              v-for="item in materialNameList"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            >
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{
                item.code
              }}</span></el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="供应商/客户编码" prop="supplierOrCustomerCode">
          <el-select
            v-model="formData.supplierOrCustomerCode"
            placeholder="请选择供应商/客户编码"
            :loading="loading"
            filterable
            clearable
            remote
            :remote-method="getSupplierCodeList"
            @change="changeSupplierCode"
          >
            <el-option
              v-for="item in supplierOrCustomerCodeList"
              :key="item.id"
              :label="item.code"
              :value="item.code"
            >
              <span style="float: left">{{ item.code }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{
                item.name
              }}</span></el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="供应商/客户名称" prop="supplierOrCustomerName">
          <el-select
            v-model="formData.supplierOrCustomerName"
            placeholder="请选择供应商/客户名称"
            :loading="loading"
            filterable
            clearable
            remote
            :remote-method="getSupplierNameList"
            @change="changeSupplierName"
          >
            <el-option
              v-for="item in supplierOrCustomerNameList"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            >
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{
                item.code
              }}</span></el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input
            v-model="formData.note"
            type="textarea"
            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 SupplierMouldsApi from "@/api/wms/base/supplierMoules.js";
import { getSupplierPage } from "@/api/wms/base/supplier";
import { getMaterialPage } from "@/api/wms/base/material";

export default {
  name: "SupplierMouldsForm",
  components: {},
  data() {
    return {
      loading: false,
      // 应商/客户列表
      supplierOrCustomerCodeList: [],
      supplierOrCustomerNameList: [],
      // 物料列表
      materialCodeList: [],
      materialNameList: [],
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        mouldCode: undefined,
        mouldName: undefined,
        materialCode: undefined,
        materialName: undefined,
        supplierOrCustomerCode: undefined,
        supplierOrCustomerName: undefined,
        note: undefined,
      },
      // 表单校验
      formRules: {
        mouldCode: [
          { required: true, message: "模具编码不能为空", trigger: "blur" },
        ],
        mouldName: [
          { required: true, message: "模具名称不能为空", trigger: "blur" },
        ],
        materialCode: [
          { required: true, message: "物料编码不能为空", trigger: "blur" },
        ],
        materialName: [
          { required: true, message: "物料名称不能为空", trigger: "blur" },
        ],
        supplierOrCustomerCode: [
          {
            required: true,
            message: "供应商/客户编码不能为空",
            trigger: "change",
          },
        ],
        supplierOrCustomerName: [
          {
            required: true,
            message: "供应商/客户名称不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },

  created() {},
  methods: {
    // 获得应商/客户列表*******************************************
    getSupplierNameList(name) {
      this.loading = true;
      getSupplierPage({
        pageNo: 1,
        pageSize: 10,
        name,
      })
        .then((response) => {
          this.supplierOrCustomerNameList = response.data.list;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 修改应商/客户名称
    changeSupplierName(e) {
      if (e === "") {
      } else {
        const { code } = this.supplierOrCustomerNameList.find(
          (item) => item.name === e
        );
        this.formData.supplierOrCustomerCode = code;
      }
    },
    getSupplierCodeList(code) {
      this.loading = true;
      getSupplierPage({
        pageNo: 1,
        pageSize: 10,
        code,
      })
        .then((response) => {
          this.supplierOrCustomerCodeList = response.data.list;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 修改应商/客户编码
    changeSupplierCode(e) {
      if (e === "") {
      } else {
        const { name } = this.supplierOrCustomerCodeList.find(
          (item) => item.code === e
        );
        this.formData.supplierOrCustomerName = name;
      }
    },
    // 获得物料列表*******************************************
    getMaterialNameList(name) {
      this.loading = true;
      getMaterialPage({
        pageNo: 1,
        pageSize: 10,
        name,
      })
        .then((response) => {
          this.materialNameList = response.data.list;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 修改物料名称
    changeMaterialNamePage(e) {
      if (e === "") {
      } else {
        const { code } = this.materialNameList.find((item) => item.name === e);
        this.formData.materialCode = code;
      }
    },
    getMaterialCodeList(code) {
      this.loading = true;
      getMaterialPage({
        pageNo: 1,
        pageSize: 10,
        code,
      })
        .then((response) => {
          this.materialCodeList = response.data.list;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 修改物料编码
    changeMaterialCodePage(e) {
      if (e === "") {
      } else {
        const { name } = this.materialCodeList.find((item) => item.code === e);
        this.formData.materialName = name;
      }
    },

    
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await SupplierMouldsApi.getSupplierMoulds(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 SupplierMouldsApi.updateSupplierMoulds(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit("success");
          return;
        }
        // 添加的提交
        await SupplierMouldsApi.createSupplierMoulds(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit("success");
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        mouldCode: undefined,
        mouldName: undefined,
        materialCode: undefined,
        materialName: undefined,
        supplierOrCustomerCode: undefined,
        supplierOrCustomerName: undefined,
        note: undefined,
      };
      this.resetForm("formRef");
    },
  },
};
</script>
