<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" :sm="12" :xs="12">
            <el-form-item :label="$t('申请单编码')" prop="productionRequestCode">
              <el-input
                disabled
                v-model="form.productionRequestCode"
               
              ></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.itemName"
                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="6" :md="8" :sm="12" :xs="12">
            <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-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('制造工厂')" prop="factoryId">
              <el-input
                v-model="form.factoryName"
                readonly
               
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="$refs['FactorySelectRef'].showFlag = true"
                ></el-button>
              </el-input>
              <FactorySelect
                ref="FactorySelectRef"
                @onSelected="onFactorySelect"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="仓库" prop="warehouse">
              <el-input
                v-model="form.warehouse"
               
              ></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('尺码')" prop="sizeGroup">
              <el-input
                v-model="form.sizeGroup"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('是否要鞋图')" prop="isNeedShoeImg">
              <el-select v-model="form.isNeedShoeImg">
                <el-option :label="$t('是')" :value="1"></el-option>
                <el-option :label="$t('否')" :value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('是否附样品')" prop="isNeedPrototype">
              <el-select v-model="form.isNeedPrototype">
                <el-option :label="$t('是')" :value="1"></el-option>
                <el-option :label="$t('否')" :value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('唯一码产品')" prop="isUniqueCode">
              <el-select v-model="form.isUniqueCode">
                <el-option :label="$t('是')" :value="1"></el-option>
                <el-option :label="$t('否')" :value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('不考虑型体配色')" prop="isConsiderColor">
              <el-select v-model="form.isConsiderColor">
                <el-option :label="$t('是')" :value="1"></el-option>
                <el-option :label="$t('否')" :value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('是否禁用')" prop="isDisabled">
              <el-select v-model="form.isDisabled">
                <el-option :label="$t('是')" :value="1"></el-option>
                <el-option :label="$t('否')" :value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <!-- <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="样品" prop="prototype">
              <el-input
                v-model="form.prototype"
               
              ></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="$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('宽度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('高度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('厚度mm')" prop="thick">
              <el-input v-model="form.thick" type="number" :precision="2" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('成品克重g')" prop="weight">
              <el-input v-model="form.weight" type="number" :precision="2" />
            </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="fillet">
              <el-input v-model="form.fillet" 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="fillet">
              <el-radio-group v-model="form.isAllotype" >
                <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-row>
        <el-row>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :lg="6" :md="8" :sm="12" :xs="12" :label="$t('是否直角')" prop="isRightAngle">
              <el-radio-group v-model="form.isRightAngle">
                <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('角度(mm)')" prop="angle">
              <el-input v-model="form.angle" type="number" :precision="2" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('孔径(mm)')" prop="aperture">
              <el-input v-model="form.aperture" 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-row>
        <el-row>
          <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('是否防霉')" prop="isMould">
              <el-radio-group v-model="form.isMould" >
                <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="isCoding">
              <el-radio-group v-model="form.isCoding" >
                <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="certification">
              <el-input v-model="form.certification" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('样品形状')" prop="shape">
              <el-select v-model="form.shape">
                <el-option
                  v-for="dict in dict.type.product_shape"
                  :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="$t('样品形状大小')" prop="shapeSize">
              <el-input v-model="form.shapeSize" type="number" :precision="4" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :lg="6" :md="8" :sm="12" :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 :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('鞋图')" prop="shoeImg">
              <ImageUpload
                :limit="1"
                :value="form.shoeImg"
                :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 ImageUpload from "@/components/ImageUpload/index.vue";
import ProogingProcess from "./components/ProogingProcess.vue";
import ItemSelect from "@/components/itemSelect/single.vue";
import FactorySelect from "@/components/FactorySelect/index.vue";
import RoutesProcessSelect from "@/components/routesProcessSelect/index.vue";
import { listRouteprocess } from "@/api/mes/pro/routeprocess";
import tabPlugins from "@/plugins/tab";
import {
  addProductionRequest,
  getProductionRequest,
  updateProductionRequest,
} from "@/api/mes/pro/productionRequest";

export default {
  components: {
    ProogingBom,
    ProogingProcess,
    ItemSelect,
    RoutesProcessSelect,
    FactorySelect,
    ImageUpload,
  },
  dicts: ['product_shape'],
  provide() {
    return {
      mode: this.mode,
    };
  },
  data() {
    return {
      loading: false,
      bomList: [],
      // 表单参数
      form: {
        productionRequestCode: "",
        itemId: "",
        itemName: "",
        unit: "",
        routeId: "",
        routeName: "",
        factoryId: "",
        factoryName: "",
        type: "",
        modality: "",
        isNeedPrototype: "",
        isNeedShoeImg: "",
        isUniqueCode: "",
        isDisabled: "",
        shoeImg: "",
        sizeGroup: "",
        remark: "",
        prototype: "",
        warehouse: "",
        length: '',
        width: '',
        height: '',
        thick: '',
        weight: '',
        angleType: '',
        isAllotype:'',
        isRightAngle:'',
        fillet:'',
        angle: '',
        aperture: '',
        singleLayerThick: '',
        singleWeight: '',
        layerNum: '',
        isMould: '',
        isCoding: '',
        certification: '',
        shape: '',
        shapeSize: '',
      },
      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();
  },
  methods: {
    /** 根据 id 获取页面数据 */
    getFormInfo() {
      if (this.mode != "apply") {
        const id = this.$route.query.productionRequestId;
        this.loading = true;
        getProductionRequest(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.productionRequestCode = 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;
      }
    },
    onFactorySelect(row) {
      if (row != undefined && row != null) {
        this.form.factoryId = row.factoryId;
        this.form.factoryName = row.factoryName;
      }
    },
    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") {
        addProductionRequest(params)
          .then((res) => {
            this.loading = false;
            this.$modal.msgSuccess(this.$t('提交成功！'));

            // 关闭页签
            tabPlugins.closeOpenPage();
          })
          .catch(() => {
            this.loading = false;
          });
      } else {
        updateProductionRequest(params)
          .then(() => {
            this.loading = false;
            this.$modal.msgSuccess(this.$t('提交成功！'));
            this.getFormInfo();
          })
          .catch(() => {
            this.loading = false;
          });
      }
    },
    /**重置表单 */
    resetForm() {
      this.form = {
        productionRequestCode: "",
        itemId: "",
        itemName: "",
        unit: "",
        routeId: "",
        routeName: "",
        factoryId: "",
        factoryName: "",
        type: "",
        modality: "",
        isNeedPrototype: "",
        isNeedShoeImg: "",
        isUniqueCode: "",
        isDisabled: "",
        shoeImg: "",
        sizeGroup: "",
        remark: "",
        prototype: "",
        warehouse: "",
        length: '',
        width: '',
        height: '',
        thick: '',
        weight: '',
        angleType: '',
        isAllotype:'',
        isRightAngle:'',
        fillet:'',
        angle: '',
        aperture: '',
        singleLayerThick: '',
        singleWeight: '',
        layerNum: '',
        isMould: '',
        isCoding: '',
        certification: '',
        shape: '',
        shapeSize: '',
      };
      this.$refs["ProogingBomRef"].resetState();
      this.$refs["ProogingProcessRef"].resetState();
    },
    //图片上传成功
    handleImgUploaded(imgUrl) {
      this.form.shoeImg = imgUrl;
    },
    //图片移除
    handleImgRemoved(imgUrl) {
      this.form.shoeImg = null;
    },
  },
};
</script>

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