<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" :xs="12">
            <el-form-item label="量产制作单编码" prop="productionMakeCode">
              <el-input
                disabled
                v-model="form.productionMakeCode"
                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.itemCode"
                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="12" :md="8" :sm="12" :xs="12">
            <el-form-item label="产品名称" prop="itemName">
              <el-input v-model="form.itemName" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="产品类别" prop="productTypeName">
              <el-input v-model="form.productTypeName" placeholder="请输入产品类别" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="品牌" prop="brandName">
              <el-input v-model="form.brandName" placeholder="请输入品牌" />
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="8" :sm="12" :xs="12">
            <el-form-item label="英文名称" prop="itemNameEn">
              <el-input v-model="form.itemNameEn" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="样品单号" prop="prototypeMakeCode">
              <el-input v-model="form.prototypeMakeCode" placeholder="请输入样品单号" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="采购单位" prop="purchaseUnit">
              <el-input v-model="form.purchaseUnit" placeholder="请输入采购单位" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="换算值" prop="conversionValue">
              <el-input v-model="form.conversionValue" placeholder="请输入换算值" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="长度cm" prop="length">
              <el-input v-model="form.length" type="number" :precision="2" placeholder="请输入长度" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="单层厚(丝)" prop="singleLayerThick">
              <el-input v-model="form.singleLayerThick" type="number" :precision="2" placeholder="请输入单层厚(单位丝)" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="单重(KG)" prop="singleWeight">
              <el-input v-model="form.singleWeight" type="number" :precision="2" placeholder="请输入单重" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="层数" prop="layerNum">
              <el-input v-model="form.layerNum" type="number" :precision="2" placeholder="请输入层数" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="高度cm" prop="height">
              <el-input v-model="form.height" type="number" :precision="2" placeholder="请输入高度" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="宽度cm" prop="width">
              <el-input v-model="form.width" type="number" :precision="2" placeholder="请输入宽度" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="印张长度" prop="printingPaperlenth">
              <el-input
                v-model="form.printingPaperlenth"
                placeholder="请输入印张长度"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="印张宽度" prop="printPaperwidth">
              <el-input
                v-model="form.printPaperwidth"
                placeholder="请输入印张宽度"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="仓库" prop="storePlace">
              <el-input
                v-model="form.storePlace"
                placeholder="请输入仓库"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="尺码组" prop="sizeGroupName">
              <el-input
                v-model="form.sizeGroupName"
                placeholder="请输入尺码组"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="派工倍数" prop="sendworkMultiple">
              <el-input
                v-model="form.sendworkMultiple"
                placeholder="请输入派工倍数"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="行" prop="rowNum">
              <el-input v-model="form.rowNum" placeholder="请输入行" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="是否禁用" prop="fillet">
              <el-radio-group v-model="form.isDisable" >
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="是否创建新规格书" prop="fillet" label-width="130px">
              <el-radio-group v-model="form.isCreateSpec" >
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="是否要鞋图" prop="fillet">
              <el-radio-group v-model="form.isNeedShoeImg" >
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="是否附样品" prop="fillet">
              <el-radio-group v-model="form.isAddSimple" >
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="是否唯一码产品" prop="fillet">
              <el-radio-group v-model="form.isUniqueCode" >
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="是否不考虑型体配色" prop="fillet" label-width="140px">
              <el-radio-group v-model="form.isConsiderColor" >
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="配方" prop="formula">
              <el-input v-model="form.formula" placeholder="请输入配方" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="产品材质" prop="productMaterial">
              <el-input v-model="form.productMaterial" placeholder="请输入产品材质" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :md="8" :xs="12">
            <el-form-item label="印刷正面颜色" prop="printingFrontcolor">
              <el-input v-model="form.printingFrontcolorName"
                        readonly
                        placeholder="请输入印刷正面颜色">
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="$refs['printingFrontcolorSelect'].showFlag = true"
                ></el-button>
              </el-input>
              <ColorSelect
                ref="printingFrontcolorSelect"
                @onSelected="onPrintingFrontcolorSelected"
              >
              </ColorSelect>
            </el-form-item>
          </el-col>
          <el-col :md="8" :xs="12">
            <el-form-item label="印刷背面颜色" prop="printingBackcolor">
              <el-input
                readonly
                v-model="form.printingBackcolorName"
                placeholder="请输入印刷背面颜色"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="$refs['printingBackcolorSelect'].showFlag = true"
                ></el-button>
              </el-input>
              <ColorSelect
                ref="printingBackcolorSelect"
                @onSelected="onPrintingBackcolorSelected"
              >
              </ColorSelect>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :lg="8" :md="8" :sm="12" :xs="12" style="height: 50.5px">
            <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="8" :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-row>
          <el-col :md="8" :xs="12">
            <el-form-item label="产品正面" prop="frontImg">
              <ImageUpload :limit="1" :value="form.frontImg" fieldName="frontImg" :fileSize="5"
                           @onUploaded="handleImgUploaded" @onRemoved="handleImgRemoved"></ImageUpload>
            </el-form-item>
          </el-col>
          <el-col :md="8" :xs="12">
            <el-form-item label="产品背面" prop="backImg">
              <ImageUpload :limit="1" :value="form.backImg" fieldName="backImg" :fileSize="5" @onUploaded="handleImgUploaded" @onRemoved="handleImgRemoved"></ImageUpload>
            </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 { closeOpenPage } from "@/plugins/tab";
import ColorSelect from "@/components/colorSelect/index.vue";

//新增方法的js
import { listProductionMake, getProductionMake, delProductionMake, addProductionMake, updateProductionMake } from "@/api/mes/pro/productionMake";
import { getProductionRequest } from "@/api/mes/pro/productionRequest";

export default {
  name: "/mes/pro/productionMake/query",
  components: { ProogingBom, ProogingProcess, ItemSelect, RoutesProcessSelect, ColorSelect },
  dicts: ["sys_yes_no"],
  provide() {
    return {
      mode: this.mode,
    };
  },
  data() {
    return {
      loading: false,
      bomList: [],
      // 表单参数
      form: {
        prototypeRequestCode: "",
        prototypeMakeCode:"",
        itemId: "",
        itemCode: "",
        itemName: "",
        itemNameEn: "",
        productTypeName:"",
        purchaseUnit:"",
        brandId:"",
        brandName:"",
        unit: "",
        conversionValue:"",
        length:"",
        singleLayerThick:"",
        singleWeight:"",
        layerNum:"",
        height:"",
        width:"",
        printingPaperlenth:"",
        printPaperwidth:"",
        storePlace:"",
        sizeGroupName:"",
        sendworkMultiple:"",
        rowNum:"",
        isDisable:"",
        isCreateSpec:"",
        isNeedShoeImg:"",
        isAddSimple:"",
        isUniqueCode:"",
        isConsiderColor:"",
        productMaterial:"",
        sheet_length:"",
        sheet_width:"",
        dispatch_multiple:"",
        row_num:"",
        routeId: "",
        routeName: "",
        abbreviation: "",
        enName: "",
        num: "",
        type: "",
        modality: "",
        surfaceTreatment: "",
        ngReason: "",
        plannedFinishDate: "",
        plannedLayoutDate: "",
        testStandard: "",
        remark: "",
        //量产制作单参数（注意修改---------------------------------------------------------）
        productionMakeId: null,
        productionMakeCode: null,
        productionRequestId: null,
        identifyingPeople: null,
        identifyingDate: null,
        printingFrontcolor: null,
        printingBackcolor: null,
        printingFrontcolorName: null,
        printingBackcolorName: null,
        frontImg: null,
        backImg: null,
        productionMakeStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      },
      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.form.productionMakeCode == null || this.form.productionMakeCode == ""){
      this.gCode();
    }
  },
  methods: {
    /** 根据 id 获取页面数据 */
    getFormInfo() {
      if (this.mode != "apply") {
        const id = this.$route.query.productionMakeId;
        this.loading = true;
        getProductionMake(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;
          });
      }else if(this.$route.query.productionRequestId){
        this.loading = true;
        getProductionRequest(this.$route.query.productionRequestId)
          .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("PRODUCTION_MAKE_CODE").then((response) => {
        this.form.productionMakeCode = 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.itemCode = row.itemCode;
        this.form.itemName = row.itemName;
        this.form.brandId = row.brandId;
        this.form.brandName = row.brandName;
        this.form.itemNameEn = row.productEnglishName;
        this.form.sizeGroupName = row.sizeGroupName;
        this.form.sizeGroupId = row.sizeGroupId;
        this.form.productTypeName = row.itemTypeName;
        this.form.unit = row.unitOfMeasure;
        this.form.length = row.length;
        this.form.width = row.width;
        this.form.height = row.height;
        this.form.thick = row.thick;
        this.form.weight = row.weight;
        this.form.angleType = row.angleType;
        this.form.isAllotype = row.isAllotype;
        this.form.isRightAngle = row.isRightAngle;
        this.form.fillet = row.fillet;
        this.form.fillet = row.angle;
        this.form.aperture = row.aperture;
        this.form.singleLayerThick = row.singleLayerThick;
        this.form.singleWeight = row.singleWeight;
        this.form.layerNum = row.layerNum;
        this.form.isMould = row.isMould;
        this.form.isCoding = row.isCoding;
        this.form.certification = row.certification;
        this.form.shape = row.shape;
        this.form.shapeSize = row.shapeSize;
        this.form.printingPaperlenth = row.sheetLength;
        this.form.printPaperwidth = row.sheetWidth;
        this.form.sendworkMultiple = row.dispatchMultiple;
        this.form.rowNum = row.rowNum;
      }
    },
    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,
        'productionRequestId':this.$route.query.productionRequestId,
        bomList,
        processList,
      };
      this.loading = true;
      if (this.mode == "apply") {
        addProductionMake(params)
          .then((res) => {
            this.loading = false;
            this.$modal.msgSuccess("提交成功！");

            // 关闭页签
            closeOpenPage();
          })
          .catch(() => {
            this.loading = false;
          });
      } else {
        updateProductionMake(params)
          .then(() => {
            this.loading = false;
            this.$modal.msgSuccess("提交成功！");
          })
          .catch(() => {
            this.loading = false;
          });
      }
    },
    /**重置表单 */
    resetForm() {
      this.form = {
        prototypeMakeCode:"",
        prototypeRequestCode: "",
        itemId: "",
        itemCode: "",
        itemName: "",
        itemNameEn: "",
        productTypeName:"",
        purchaseUnit:"",
        brandId:"",
        brandName:"",
        unit: "",
        conversionValue:"",
        length:"",
        singleLayerThick:"",
        singleWeight:"",
        layerNum:"",
        height:"",
        width:"",
        printingPaperlenth:"",
        printPaperwidth:"",
        storePlace:"",
        sizeGroupName:"",
        sendworkMultiple:"",
        rowNum:"",
        isDisable:"",
        isCreateSpec:"",
        isNeedShoeImg:"",
        isAddSimple:"",
        isUniqueCode:"",
        isConsiderColor:"",
        productMaterial:"",
        sheet_length:"",
        sheet_width:"",
        dispatch_multiple:"",
        row_num:"",
        routeId: "",
        abbreviation: "",
        enName: "",
        num: "",
        type: "",
        modality: "",
        surfaceTreatment: "",
        ngReason: "",
        plannedFinishDate: "",
        plannedLayoutDate: "",
        testStandard: "",
        remark: "",
        //量产制作单参数（注意修改---------------------------------------------------------）
        productionMakeId: null,
        productionMakeCode: null,
        productionRequestId: null,
        identifyingPeople: null,
        identifyingDate: null,
        printingFrontcolor: null,
        printingBackcolor: null,
        printingFrontcolorName: null,
        printingBackcolorName: null,
        frontImg: null,
        backImg: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.$refs["ProogingBomRef"].resetState();
      this.$refs["ProogingProcessRef"].resetState();
      this.gCode();
    },
    onPrintingBackcolorSelected(obj) {
      if (obj != undefined && obj != null) {
        obj.forEach((element) => {
          this.form.printingBackcolor = element.colorId;
          this.form.printingBackcolorName = element.colorName;
        });
      }
    },
    onPrintingFrontcolorSelected(obj) {
      if (obj != undefined && obj != null) {
        obj.forEach((element) => {
          this.form.printingFrontcolor = element.colorId;
          this.form.printingFrontcolorName = element.colorName;
        });
      }
    },
    //图片上传成功
    handleImgUploaded(imgUrl, obj){
      this.form[obj.$attrs.fieldName] = imgUrl;
    },
    //图片移除
    handleImgRemoved(imgUrl, obj){
      this.form[obj.$attrs.fieldName] = null;
    },

  },
};
</script>

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