<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
        :model="form"
        ref="form"
        size="small"
        :inline="true"
        label-width="8em"
      >
      <el-row>
        <el-col :md="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 :md="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 :md="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-row>
      <el-row>    
          <el-col :md="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 :md="12" :xs="12">
            <el-form-item label="生产用途" prop="usageName">
              <el-input
                v-model="form.usageName"
                disabled
                placeholder
              ></el-input>
            </el-form-item>
          </el-col>

        
         
          
 <el-col :md="24" :xs="12">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                :rows="3"
                v-model="form.remark"
                disabled
                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
} from "@/api/mes/pro/productionSolution";

export default {
  components: { ProogingBom, ProogingProcess, ItemSelect, RoutesProcessSelect },
  dicts: [],
  provide() {
    return {
      mode: this.mode,
    };
  },
  data() {
    return {
      loading: false,
      bomList: [],
      // 表单参数
      form: {
      productionSolutionId: "", 
        productionSolutionCode: "",
        productionSolutionName: "",
         itemId: "",
         itemName: "",
        routeId: "",
        routeName: "",
        usageId: "",
        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.bomList;
            // 设置组件数据
            this.$refs["ProogingBomRef"].tableData = data.bomList;
            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 bomList = this.$refs.ProogingBomRef.getComData();
      const processList = this.$refs.ProogingProcessRef.getComData();
      const params = {
        ...this.form,
        bomList,
        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 {
        updatePrototypeRequest(params)
          .then(() => {
            this.loading = false;
            this.$modal.msgSuccess("提交成功！");
          })
          .catch(() => {
            this.loading = false;
          });
      }
    },
    /**重置表单 */
    resetForm() {
      this.form = {
       productionSolutionId: "", 
        productionSolutionCode: "",
        productionSolutionName: "",
         itemId: "",
        routeId: "",
        usageId: "",
        remark: "",
        createBy: "",
        createTime: "",
        updateBy: "",
        updateTime: "",
      };
      this.$refs["ProogingBomRef"].resetState();
      this.$refs["ProogingProcessRef"].resetState();
      this.gCode();
    },
  },
};
</script>

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