<template>
  <div class="form-page" v-loading="loading">
    <PageTitle>
      {{ pageTitle }}
      <!-- 申请按钮显示 -->
      <template slot="buttons" v-if="mode == 'apply'">
        <el-button size="mini" @click="resetForm">{{ $t('common.reset') }}</el-button>
        <el-button size="mini" type="primary" @click="saveForm">{{ $t('common.submit') }}</el-button>
      </template>

      <!-- 修改 按钮显示 -->
      <template slot="buttons" v-if="mode == 'edit'">
        <el-button size="mini" type="primary" @click="saveForm">{{ $t('common.submit') }}</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="$t('量产制作单编码')" prop="productionMakeCode">
              <el-input
                disabled
                v-model="form.productionMakeCode"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('产品编码')" prop="itemId">
              <el-input
                v-model="form.itemCode"
                readonly
               
              >
                <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="$t('产品名称')" 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="$t('产品类别')" prop="productTypeName">
              <el-input v-model="form.productTypeName" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('品牌')" prop="brandName">
              <el-input v-model="form.brandName" />
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('英文名称')" 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="$t('样品单号')" prop="prototypeMakeCode">
              <el-input v-model="form.prototypeMakeCode" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('采购单位')" prop="purchaseUnit">
              <el-input v-model="form.purchaseUnit" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('单位')" prop="unit">
              <el-input v-model="form.unit" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('换算值')" prop="conversionValue">
              <el-input v-model="form.conversionValue" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('长度cm')" prop="length">
              <el-input v-model="form.length" type="number" :precision="2" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('单层厚(丝)')" prop="singleLayerThick">
              <el-input v-model="form.singleLayerThick" type="number" :precision="2" :placeholder="$t('请输入单层厚(单位丝)')" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('单重(KG)')" prop="singleWeight">
              <el-input v-model="form.singleWeight" type="number" :precision="2" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('层数')" prop="layerNum">
              <el-input v-model="form.layerNum" type="number" :precision="2" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('高度cm')" prop="height">
              <el-input v-model="form.height" type="number" :precision="2" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('宽度cm')" prop="width">
              <el-input v-model="form.width" type="number" :precision="2" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('印张长度')" prop="printingPaperlenth">
              <el-input
                v-model="form.printingPaperlenth"
               
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('印张宽度')" prop="printPaperwidth">
              <el-input
                v-model="form.printPaperwidth"
               
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('仓库')" prop="storePlace">
              <el-input
                v-model="form.storePlace"
               
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('尺码组')" prop="sizeGroupName">
              <el-input
                v-model="form.sizeGroupName"
               
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('派工倍数')" prop="sendworkMultiple">
              <el-input
                v-model="form.sendworkMultiple"
               
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('行')" prop="rowNum">
              <el-input v-model="form.rowNum" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('是否禁用')" 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="$t('是否创建新规格书')" 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="$t('是否要鞋图')" 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="$t('是否附样品')" 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="$t('是否唯一码产品')" 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="$t('是否不考虑型体配色')" 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="$t('配方')" prop="formula">
              <el-input v-model="form.formula" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('产品材质')" prop="productMaterial">
              <el-input v-model="form.productMaterial" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :md="8" :xs="12">
            <el-form-item :label="$t('印刷正面颜色')" prop="printingFrontcolor">
              <el-input v-model="form.printingFrontcolorName"
                        readonly
                       >
                <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="$t('印刷背面颜色')" prop="printingBackcolor">
              <el-input
                readonly
                v-model="form.printingBackcolorName"
               
              >
                <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="$t('工艺路线')" prop="routeId">
              <el-input
                v-model="form.routeName"
                readonly
               
              >
                <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="$t('common.remark')" prop="remark">
              <el-input
                type="textarea"
                :rows="3"
                v-model="form.remark"
               
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :md="8" :xs="12">
            <el-form-item :label="$t('产品正面')" 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="$t('产品背面')" 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="$t('工艺')">
          <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: this.$t('量产制作单修改'),
        info: this.$t('量产制作单查看'),
        apply: this.$t('量产制作单申请'),
      };
      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(this.$t('提交成功！'));

            // 关闭页签
            closeOpenPage();
          })
          .catch(() => {
            this.loading = false;
          });
      } else {
        updateProductionMake(params)
          .then(() => {
            this.loading = false;
            this.$modal.msgSuccess(this.$t('提交成功！'));
          })
          .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>
