<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 :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item prop="prototypeRequestCode">
              <template slot="label"> 申请单编码 </template>
              <el-input
                disabled
                v-model="form.prototypeRequestCode"
                placeholder="自动生成"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="8" :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 :lg="6" :md="8" :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-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="简称" prop="abbreviation">
              <el-input
                v-model="form.abbreviation"
                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="enName">
              <el-input
                v-model="form.enName"
                placeholder="请输入样品英文名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="样品数量" prop="num">
              <el-input v-model="form.num" placeholder="请输入样品数量">
                <template slot="suffix">{{ form.unit }}</template>
              </el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="打样形式" prop="type">
              <el-input
                v-model="form.type"
                placeholder="请选择打样形式"
              ></el-input>
            </el-form-item>
          </el-col> -->

          <el-col :span="6">
            <el-form-item label="打样形式" prop="type">
              <el-select v-model="form.type" placeholder="请选择打样形式">
                <el-option
                  v-for="dict in dict.type.pro_proof_form"
                  :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="modality">
              <el-input
                v-model="form.modality"
                placeholder="请选择分配方式"
              ></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="6">
            <el-form-item label="分配方式" prop="modality">
              <el-select v-model="form.modality" placeholder="请选择分配方式">
                <el-option
                  v-for="dict in dict.type.pro_distribution_profing"
                  :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="surfaceTreatment">
              <el-input
                v-model="form.surfaceTreatment"
                placeholder="请输入表面处理"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="NG原因" prop="ngReason">
              <el-input
                v-model="form.ngReason"
                placeholder="请输入NG原因"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item prop="plannedFinishDate">
              <template slot="label">
                <TipLabel title="完成日期" tip="计划完成日期" />
              </template>
              <el-date-picker
                value-format="yyyy-MM-dd"
                v-model="form.plannedFinishDate"
                placeholder="请输入计划完成日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item prop="plannedLayoutDate">
              <template slot="label">
                <TipLabel title="版面时间" tip="版面需求时间" />
              </template>
              <el-date-picker
                value-format="yyyy-MM-dd"
                v-model="form.plannedLayoutDate"
                placeholder="请输入版面需求时间"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="测试标准" prop="testStandard">
              <el-input
                v-model="form.testStandard"
                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,
} from "@/api/mes/pro/prototypeRequest";

export default {
  components: { ProogingBom, ProogingProcess, ItemSelect, RoutesProcessSelect },
  dicts: ["pro_proof_form", "pro_distribution_profing"],
  provide() {
    return {
      mode: this.mode,
    };
  },
  data() {
    return {
      loading: false,
      bomList: [],
      // 表单参数
      form: {
        prototypeRequestCode: "",
        itemId: "",
        itemName: "",
        unit: "",
        routeId: "",
        routeName: "",
        abbreviation: "",
        enName: "",
        num: "",
        type: "",
        modality: "",
        surfaceTreatment: "",
        ngReason: "",
        plannedFinishDate: "",
        plannedLayoutDate: "",
        testStandard: "",
        remark: "",
      },
      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.prototypeRequestId;
        this.loading = true;
        getPrototypeRequest(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("提交成功！");

            this.$nextTick(() => {
              // 关闭页签
              tabPlugins.closeOpenPage();
              this.$router.replace({
                path: '/mes/proofing/query'
              })
            })
          })
          .catch(() => {
            this.loading = false;
          });
      } else {
        updatePrototypeRequest(params)
          .then(() => {
            this.loading = false;
            this.$modal.msgSuccess("提交成功！");
            this.getFormInfo();
          })
          .catch(() => {
            this.loading = false;
          });
      }
    },
    /**重置表单 */
    resetForm() {
      this.form = {
        prototypeRequestCode: "",
        itemId: "",
        itemName: "",
        routeId: "",
        abbreviation: "",
        enName: "",
        num: "",
        type: "",
        modality: "",
        surfaceTreatment: "",
        ngReason: "",
        plannedFinishDate: "",
        plannedLayoutDate: "",
        testStandard: "",
        remark: "",
      };
      this.$refs["ProogingBomRef"].resetState();
      this.$refs["ProogingProcessRef"].resetState();
      this.gCode();
    },
  },
};
</script>

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