<template>
  <div class="form-page" v-loading="loading">
    <PageTitle>
      {{ pageTitle }}
      <!-- 申请按钮显示 -->
      <template slot="buttons" v-if="mode == 'apply'">
        <el-button size="mini" @click="resetForm">重置</el-button>
        <el-button size="mini" type="primary" @click="saveForm">提交</el-button>
      </template>

      <!-- 修改 按钮显示 -->
      <template slot="buttons" v-if="mode == 'edit'">
        <el-button size="mini" type="primary" @click="saveForm">提交</el-button>
      </template>
    </PageTitle>

    <PageWrapper>
      <el-form
        class="page-form"
        :model="form"
        ref="form"
        size="small"
        :inline="true"
        label-width="100px"
      >
        <el-row>
          <el-col :md="6" :sm="12" :xs="12">
            <el-form-item prop="brandQuotationCode">
              <template slot="label"> 编码 </template>
              <el-input
                disabled
                v-model="form.brandQuotationCode"
                placeholder="自动生成"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :md="6" :sm="12" :xs="12">
            <el-form-item label="名称" prop="brandQuotationName">
              <el-input
                v-model="form.brandQuotationName"
                placeholder="请输入品牌报价单名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="12" :xs="12">
            <el-form-item label="总金额" prop="amount">
              <el-input-money 
              v-model="form.amount" 
              v-bind:disabled="form.brandQuotationId==0"
              placeholder="新增时不可输入，请修改时操作"/>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="6">
            <el-form-item label="品牌" prop="brandCode">
              <el-input v-model="form.brandName" readonly="readonly">
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="handleBrandSelect"
                  placeholder="请选择品牌"
                ></el-button>
              </el-input>
            </el-form-item>
            <BrandSelect
              ref="brSelect"
              @onSelected="onBrandSelected"
            ></BrandSelect>
          </el-col> -->
          <!-- <el-col :span="6">
            <el-form-item label="品牌名称" prop="brandName">
              <el-input v-model="form.brandName" readonly="readonly" />
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="8">
            <el-form-item label="品牌编码" prop="brandCode">
              <el-input v-model="form.brandCode" readonly="readonly">
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="handleBrandSelect"
                  :disabled="form.brandContactId"
                ></el-button>
              </el-input>
            </el-form-item>
            <BrandSelect
              ref="brSelect"
              @onSelected="onBrandSelected"
            ></BrandSelect>
          </el-col> -->

          <el-col :md="6" :sm="12" :xs="12">
            <el-form-item label="产品" prop="itemId">
              <el-input
                v-model="form.itemName"
                readonly
                placeholder="请选择产品"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="$refs['ItemSelectRef'].showFlag = true"
                ></el-button>
              </el-input>
              <ItemSelect ref="ItemSelectRef" @onSelected="onItemSelect" />
            </el-form-item>
          </el-col>

            <el-col :span="8">
                <el-form-item label="品牌名称" prop="brandName">
                    <el-input v-model="form.brandName" placeholder="请先将所选产品绑定品牌" disabled/>
                </el-form-item>
            </el-col>    

          <el-col :md="6" :sm="12" :xs="12">
            <el-form-item label="工艺路线" prop="routeId">
              <el-input
                v-model="form.routeName"
                readonly
                placeholder="请选择工艺路线"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="$refs['RoutesProcessSelectRef'].showFlag = true"
                ></el-button>
              </el-input>
              <RoutesProcessSelect
                ref="RoutesProcessSelectRef"
                @onSelected="onRoutesProcessSelect"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :md="24" :sm="12" :xs="12">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                :rows="3"
                v-model="form.remark"
                placeholder="备注"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-tabs type="border-card" @tab-click="tabClick">
        <el-tab-pane label="BOM">
          <ProogingBom ref="ProogingBomRef" />
        </el-tab-pane>
        <el-tab-pane label="工艺">
          <ProogingProcess ref="ProogingProcessRef" :bomList="bomList" />
        </el-tab-pane>
      </el-tabs>
    </PageWrapper>
  </div>
</template>

<script>
import { genCode } from "@/api/system/autocode/rule";
import ProogingBom from "./components/ProogingBom.vue";
import ProogingProcess from "./components/ProogingProcess.vue";
import ItemSelect from "@/components/itemSelect/single.vue";
import RoutesProcessSelect from "@/components/routesProcessSelect/index.vue";
import { listRouteprocess } from "@/api/mes/pro/routeprocess";
import tabPlugins from "@/plugins/tab";
import {
  addPrototypeRequest,
  getPrototypeRequest,
  updatePrototypeRequest,
} from "@/api/mes/pro/prototypeRequest";
// 导入品牌选择框
import BrandSelect from "@/components/brandSelect/single.vue";
//导入品牌报价js
import { listBrandQuotation, getBrandQuotation, delBrandQuotation, addBrandQuotation, updateBrandQuotation } from "@/api/mes/dv/brandquotation";

export default {
  components: { ProogingBom, ProogingProcess, ItemSelect, RoutesProcessSelect, BrandSelect },
  dicts: [],
  provide() {
    return {
      mode: this.mode,
    };
  },
  data() {
    return {
      loading: false,
      bomList: [],
      // 表单参数
      form: {
        brandQuotationId: "",
        brandQuotationCode: "",
        brandQuotationName: "",
        brandId: "",
        brandName: "",
        itemId: "",
        itemName: "",
        routeId: "",
        routeName: "",
        remark: "",
        amount: "",
      },
      // 表单校验
      rules: {
        brandId: [
          { required: true, message: "品牌不能为空,请先查验所选产品是否已绑定品牌", trigger: "blur" }
        ],             
        }
    };
  },
  computed: {
    scrollHeight() {
      return "calc(100vh - 73px - 83px)";
    },
    mode() {
      return this.$route.path.split("/").at(-1);
    },
    pageTitle() {
      const titles = {
        edit: "品牌报价单修改",
        info: "品牌报价单查看",
        apply: "品牌报价单申请",
      };
      return titles[this.mode];
    },
  },
  created() {
    this.getFormInfo();
    if (this.mode == "apply") {
    this.gCode();
    }
  },
  methods: {
            //选择品牌
    handleBrandSelect() {
      this.$refs.brSelect.showFlag = true;
    },
    onBrandSelected(row) {
      if (row != undefined && row != null) {
        this.form.brandId = row.brandId;
        this.form.brandCode = row.brandCode;
        this.form.brandName = row.brandName;
        this.form.groupName = row.groupName;
        this.form.address = row.address;
        this.form.salesContact = row.salesContact;
        this.form.devContact = row.devContact;
        this.form.background = row.background;
        this.form.scope = row.scope;
        this.form.potentialProject = row.potentialProject;
        this.form.integrityRegulations = row.integrityRegulations;
      }
    },
    /** 根据 id 获取页面数据 */
    getFormInfo() {
      if (this.mode != "apply") {
        const id = this.$route.query.brandQuotationId;
        this.loading = true;
        getBrandQuotation(id)
          .then(({ data }) => {
            Object.assign(this.form, data);
            this.bomList = data.bomList;
            // 设置组件数据
            this.$refs["ProogingBomRef"].tableData = data.bomList;
            this.$refs["ProogingProcessRef"].tableData = data.processList;
          })
          .finally(() => {
            this.loading = false;
          });
      }
    },
    /** 生成编码 */
    gCode() {
      genCode("BRAND_QUOTATION_CODE").then((response) => {
        this.form.brandQuotationCode = response;
      });
    },
    /** 点击tab 获取bom的数据,将参数传递给后面的组件 */
    tabClick(val) {
      if (val.index != 0) {
        this.bomList = this.$refs["ProogingBomRef"].getComData();
      }
    },
    onItemSelect(row) {
      if (row != undefined && row != null) {
        this.form.itemId = row.itemId;
        this.form.itemName = row.itemName;
        this.form.brandId = row.brandId;
        this.form.brandName = row.brandName;
        this.form.unit = row.unitOfMeasure;
      }
    },
    onRoutesProcessSelect(row) {
      if (row != undefined && row != null) {
        this.form.routeId = row.routeId;
        this.form.routeName = row.routeName;

        const params = {
          pageNum: 1,
          pageSize: 1000,
          routeId: row.routeId,
        };
        listRouteprocess(params).then(({ rows }) => {
          this.$refs["ProogingProcessRef"].setList(rows);
        });
      }
    },
    /**提交 */
    saveForm() {
      const bomList = this.$refs.ProogingBomRef.getComData();
      const processList = this.$refs.ProogingProcessRef.getComData();
      const params = {
        ...this.form,
        bomList,
        processList,
      };
      this.loading = true;
      if (this.mode == "apply") {
        addBrandQuotation(params)
          .then((res) => {
            this.loading = false;
            this.$modal.msgSuccess("提交成功！");

            // 关闭页签
            tabPlugins.closeOpenPage();
          })
          .catch(() => {
            this.loading = false;
          });
      } else {
        updateBrandQuotation(params)
          .then(() => {
            this.loading = false;
            this.$modal.msgSuccess("提交成功！");
          })
          .catch(() => {
            this.loading = false;
          });
      }
    },
    /**重置表单 */
    resetForm() {
      this.form = {
        brandQuotationId: "",
        brandQuotationCode: "",
        brandQuotationName: "",
        brandId: "",
        brandName: "",
        itemId: "",
        itemName: "",
        routeId: "",
        routeName: "",
        remark: "",
        amount: "",
      };
      this.$refs["ProogingBomRef"].resetState();
      this.$refs["ProogingProcessRef"].resetState();
      this.gCode();
    },
  },
};
</script>

<style lang="scss"></style>
