<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="8em"
      >
        <el-row>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="生产版本编码" prop="productionSolutionCode">
              <el-input
                v-model="form.productionSolutionCode"
                disabled
                placeholder
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="生产版本名称" prop="productionSolutionName">
              <el-input
                v-model="form.productionSolutionName"
                disabled
                placeholder
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="生产版本类型" prop="productionSolutionType">
              <el-select
                v-model="form.productionSolutionType"
                clearable
                placeholder="请选择项目类别"
              >
                <el-option
                  v-for="dict in dict.type.production_solution_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="产品名称" prop="itemName">
              <el-input v-model="form.itemName" disabled placeholder></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="工艺路线" prop="routeName">
              <el-input
                v-model="form.routeName"
                disabled
                placeholder
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="生产用途" prop="usageId">
              <el-select v-model="form.usageId" placeholder="请选择用途">
                <el-option
                  v-for="dict in dict.type.production_solution_usage"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="有效起始日期" prop="startDate">
              <el-date-picker
                clearable
                v-model="form.startDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择有效起始日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="有效期截止日期" prop="expiryDate">
              <el-date-picker
                clearable
                v-model="form.expiryDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择有效期截止日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="最小批量" prop="minLot">
              <el-input-number
                :min="1"
                v-model="form.minLot"
                placeholder="请输入最小批量"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="最大批量" prop="maxLot">
              <el-input-number
                :min="1"
                v-model="form.maxLot"
                placeholder="请输入最大批量"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="单位" prop="unitOfMeasure">
              <el-input
                v-model="form.unitOfMeasure"
                disabled
                placeholder
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :lg="6" :md="8" :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,
  getProductionSolution,
  submitProductionSolution,
} from "@/api/mes/pro/productionSolution";

export default {
  components: { ProogingBom, ProogingProcess, ItemSelect, RoutesProcessSelect },
  dicts: ["production_solution_type", "production_solution_usage"],
  provide() {
    return {
      mode: this.mode,
    };
  },
  data() {
    return {
      loading: false,
      bomList: [],
      // 表单参数
      form: {
        productionSolutionId: "",
        productionSolutionCode: "",
        productionSolutionName: "",
        productionSolutionType: "",
        itemId: "",
        itemName: "",
        routeId: "",
        routeName: "",
        usageId: "",
        usageName: "",
        startDate: "",
        expiryDate: "",
        maxLot: "",
        minLot: "",
        unitOfMeasure: "",
        materialsGroupBill: "",
        alternativeBill: "",
        groupCounter: "",
        groupKey: "",
        status: "",
        sapItemCode: "",
        factoryCode: "",
        remark: "",
        createBy: "",
        createTime: "",
        updateBy: "",
        updateTime: "",
      },
      rules: {},
    };
  },
  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: {
    /** 根据 id 获取页面数据 */
    getFormInfo() {
      if (this.mode != "apply") {
        const id = this.$route.query.productionSolutionId;
        this.loading = true;
        getProductionSolution(id)
          .then(({ data }) => {
            Object.assign(this.form, data);
            this.bomList = data.bomHead.bomItemList;
            // 设置组件数据
            this.$refs["ProogingBomRef"].form2 = data.bomHead;
            this.$refs["ProogingBomRef"].tableData = data.bomHead.bomItemList;
            this.$refs["ProogingProcessRef"].tableData = data.processList;
          })
          .finally(() => {
            this.loading = false;
          });
      }
    },
    /** 生成编码 */
    gCode() {
      genCode("PROTOTYPE_REQUEST_CODE").then((response) => {
        this.form.prototypeRequestCode = 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.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 bomHead = this.$refs.ProogingBomRef.getComData();
      const processList = this.$refs.ProogingProcessRef.getComData();
      const params = {
        ...this.form,
        bomHead,
        processList,
      };
      this.loading = true;
      if (this.mode == "apply") {
        addPrototypeRequest(params)
          .then((res) => {
            this.loading = false;
            this.$modal.msgSuccess("提交成功！");

            // 关闭页签
            tabPlugins.closeOpenPage();
          })
          .catch(() => {
            this.loading = false;
          });
      } else {
        submitProductionSolution(params)
          .then(() => {
            this.loading = false;
            this.$modal.msgSuccess("提交成功！");
          })
          .catch(() => {
            this.loading = false;
          });
      }
    },
    /**重置表单 */
    resetForm() {
      this.form = {
        productionSolutionId: "",
        productionSolutionCode: "",
        productionSolutionName: "",
        productionSolutionType: "",
        itemId: "",
        itemName: "",
        routeId: "",
        routeName: "",
        usageId: "",
        usageName: "",
        startDate: "",
        expiryDate: "",
        maxLot: "",
        minLot: "",
        unitOfMeasure: "",
        materialsGroupBill: "",
        alternativeBill: "",
        groupCounter: "",
        groupKey: "",
        status: "",
        sapItemCode: "",
        factoryCode: "",
        remark: "",
        createBy: "",
        createTime: "",
        updateBy: "",
        updateTime: "",
      };
      this.$refs["ProogingBomRef"].resetState();
      this.$refs["ProogingProcessRef"].resetState();
      this.gCode();
    },
  },
};
</script>

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