
<template>
  <div>
    <el-card class="el-card">
      <div slot="header" style="font-weight: bold">
        <span>产品标识信息</span>
      </div>

      <el-form :model="productDetail" ref="productDetail">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="发码机构编码名称:" prop="issuingAgency">
              <el-select
                v-model="productDetail.issuingAgency"
                @change="intenStandagency"
                style="width: 70%"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in agencyList"
                  :key="item.value"
                  :label="item.issuingAgency"
                  :value="item.UDI_StandAgencyOID"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="厂商识别码:" prop="IndentCode">
              <el-select
                v-model="productDetail.IndentCode"
                style="width: 70%"
                @change="intenCodeChanged"
                placeholder="请选择厂商识别码"
              >
                <el-option
                  v-for="item in intenCodeValue"
                  :key="item.value"
                  :label="item.IndentCode"
                  :value="item.UDI_FacIdentCodeOID"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="标识添加方式:" prop="randomType">
              <el-select
                size="small"
                v-model="randomType"
                @change="intenRandom"
                style="width: 75%"
                placeholder="请选择"
              >
                <el-option label="根据商品条码匹配" value="1"></el-option>

                <el-option label="随机选择未使用的码" value="3"></el-option>
                <el-option label="手动选择" value="4"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="最小销售单元产品标识:" prop="primaryDeviceId">
              <el-input
                size="small"
                style="width: 44%"
                placeholder="请输入最小销售单元产品标识"
                v-model="productDetail.primaryDeviceId"
                @change="primarychange"
              ></el-input>

              <el-button
                type="primary"
                size="mini"
                @click="selectCode(4)"
                style="margin-left: 10px"
                v-bind:disabled="randomType != 4"
                >选择产品项目码</el-button
              >
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="产品标识发布日期:" prop="publishDate">
              <el-date-picker
                v-model="productDetail.publishDate"
                type="date"
                style="width: 62.5%"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item
              label="最小销售单元内使用单元数量:"
              prop="pkgQuantity"
            >
              <el-input
                size="small"
                style="width: 58.5%"
                placeholder="请输入最小销售单元内使用单元数量"
                v-model="productDetail.pkgQuantity"
                @change="pkgQuantityChange"
                type="number"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="使用单元产品标识:" prop="useUnitDeviceId">
              <el-input
                size="small"
                style="width: 41%"
                placeholder="请输入使用单元产品标识"
                v-model="productDetail.useUnitDeviceId"
                @change="primarychange"
                v-bind:disabled="productDetail.pkgQuantity < 2"
              ></el-input>
              <el-button
                type="primary"
                size="mini"
                @click="selectCode(1)"
                style="margin-left: 10px"
                v-bind:disabled="productDetail.pkgQuantity < 2"
                >选择产品项目码</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否有本体直接标识:" prop="isDirectMark">
              <el-select
                size="small"
                v-model="productDetail.isDirectMark"
                placeholder="请选择"
                v-bind:disabled="
                  productDetail.pkgQuantity != 1 &&
                  productDetail.pkgQuantity != ''
                "
                style="width: 68%"
                @change="isDirectChange"
              >
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item
              label="本体标识与最小销售单元标识是否一致:"
              prop="isSameDeviceId"
            >
              <el-select
                size="small"
                v-model="productDetail.isSameDeviceId"
                v-bind:disabled="productDetail.isDirectMark != 1"
                placeholder="请选择"
                style="width: 41%"
                @change="intentDirectMark"
              >
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="本体产品标识:" prop="directMarkDI">
              <el-input
                size="small"
                style="width: 53.5%"
                placeholder="请输入本体产品标识"
                v-model="productDetail.directMarkDI"
                v-bind:disabled="
                  productDetail.isSameDeviceId != 2 ||
                  productDetail.isDirectMark != 1
                "
                @change="primarychange"
              ></el-input>
              <el-button
                type="primary"
                size="mini"
                @click="selectCode(2)"
                style="margin-left: 10px"
                v-bind:disabled="
                  productDetail.isSameDeviceId != 2 ||
                  productDetail.isDirectMark != 1
                "
                >选择产品项目码</el-button
              >
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="标识载体:" prop="carrierValue">
              <el-select
                v-model="carrierValue"
                size="small"
                multiple
                placeholder="请选择"
                style="width: 72%"
                @change="carrierChanged"
              >
                <el-option
                  v-for="item in carrierOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item
              label="是否与注册/备案产品标识一致:"
              prop="isSameRegDI"
            >
              <el-select
                size="small"
                v-model="productDetail.isSameRegDI"
                style="width: 57.5%"
                placeholder="请选择"
              >
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="注册/备案产品标识:" prop="registrationDI">
              <el-input
                size="small"
                style="width: 62%"
                placeholder="请输入注册/备案产品标识"
                v-model="productDetail.registrationDI"
                v-bind:disabled="productDetail.isSameRegDI == 1"
                @input="change()"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="生产标识是否包含批号:" prop="isLotBatch">
              <el-select
                size="small"
                style="width: 65%"
                v-model="productDetail.isLotBatch"
                placeholder="请选择"
              >
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item
              label="生产标识是否包含生产日期:"
              prop="isManufacturingDate"
            >
              <el-select
                size="small"
                v-model="productDetail.isManufacturingDate"
                style="width: 53%"
                placeholder="请选择"
              >
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="生产标识是否包含序列号:" prop="isSerialNumber">
              <el-select
                size="small"
                v-model="productDetail.isSerialNumber"
                style="width: 63%"
                placeholder="请选择"
              >
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item
              label="生产标识是否包含失效日期:"
              prop="isExpirationDate"
            >
              <el-select
                size="small"
                style="width: 53%"
                v-model="productDetail.isExpirationDate"
                placeholder="请选择"
              >
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <el-card class="el-card">
      <div
        slot="header"
        style="font-weight: bold; justify-content: space-between; display: flex"
      >
        <span>包装标识信息</span>
        <el-button
          type="primary"
          size="small"
          @click="handleAddpackageClick(null, null)"
          >添加</el-button
        >
      </div>
      <el-table :data="packageList" style="width: 100%">
        <el-table-column label="序号" type="index" width="80"></el-table-column>

        <el-table-column
          prop="pkgType"
          label="产品包装级别"
          width="160"
        ></el-table-column>
        <el-table-column
          prop="pkgDeviceId"
          label="包装产品标识"
          width="160"
        ></el-table-column>
        <el-table-column
          prop="pkgQuantity"
          label="包装内含小一级产品标识数量 "
          width="220"
        ></el-table-column>
        <el-table-column
          prop="containsDIPackage"
          label="包装内含小一级包装产品标识 "
          width="220"
        ></el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              @click="handleAddpackageClick(scope.$index, scope.row)"
              type="text"
              size="small"
              >修改</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="deletePkgDialog(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      :title="strgTitle[formName]"
      :visible.sync="packageVisible"
      width="65%"
      :append-to-body="true"
      top="5vh"
    >
      <el-form :model="pkgQuery" ref="dataForm">
        <el-row :gutter="20" class="el-row" type="flex">
          <el-col :span="4">
            <div class="edit-text">
              <span>产品包装级别:</span>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="pkgType">
              <el-input
                v-model="pkgQuery.pkgType"
                size="small"
                style="width: 80%"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <div class="edit-text">
              <span>包装产品标识:</span>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="pkgDeviceId">
              <el-input
                v-model="pkgQuery.pkgDeviceId"
                size="small"
                style="width: 50%"
              ></el-input>
              <el-button
                type="primary"
                size="mini"
                @click="selectCode(5)"
                style="margin-left: 10px"
                >选择产品项目码</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20" class="el-row" type="flex">
          <el-col :span="4">
            <div class="edit-text">
              <span>包装内含小一级产品标识数量:</span>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="pkgQuantity">
              <el-input
                v-model="pkgQuery.pkgQuantity"
                style="width: 80%"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <div class="edit-text">
              <span>包装内含小一级包装产品标识:</span>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="containsDIPackage">
              <el-input
                v-model="pkgQuery.containsDIPackage"
                style="width: 80%"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click.native="onSrgSubmit()"
          >保存</el-button
        >
        <el-button @click.native="cancelDialog" size="small">取消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="selectDialogVisible"
      width="65%"
      :append-to-body="true"
      v-if="selectDialogVisible"
    >
      <selectIdentify
        @identifyInfo="getIdentifyInfo"
        :facidentcode="intentFacident"
        :closeDialog="closeDialog"
      ></selectIdentify>
    </el-dialog>
    <div style="text-align: center; margin-top: 20px">
      <el-button
        type="primary"
        size="small"
        icon="search"
        @click.native="postData()"
        >{{ btSaveStr }}</el-button
      >
      <el-button type="primary" size="small" icon="search" @click="cancelModify"
        >取消</el-button
      >
    </div>
  </div>
</template>
<script>
import {
  listCli,
  listSrg,
  listClassify,
} from "../../api/publish/udiSubProduct";
import { findAll } from "../../api/basic/facidentcode";
import { randomIdentifier } from "../../api/basic/identifier";
import {
  listDeviceId,
  addDeviceId,
  modifyDeviceId,
  deleteDeviceId,
  listPkg,
} from "../../api/publish/udiSubProductID";
import { listStandagency } from "../../api/basic/agency";
import { listFacidentcode } from "../../api/basic/facidentcode";
import selectIdentify from "../basic/selectIdentify";
export default {
  name: "productDetail",
  props: {
    productDetail: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      carrierOptions: [
        {
          value: "1",
          label: "一维码",
        },
        {
          value: "2",
          label: "二维码",
        },
        {
          value: "3",
          label: "RFID",
        },
        {
          value: "4",
          label: "其他",
        },
      ],

      carrierValue: [],
      intenCodeValue: [],
      strgTitle: {
        add: "新增包装标识信息",
        edit: "编辑包装标识信息",
      },

      strgQuery: {
        UDI_DeviceSrgOID: "", //
        storageType: "", //储存或操作条件
        lowValue: "", //最低值
        highValue: "", //最高值
        unit: "", //计量单位
        UDI_SubProduct_FK: "", //子类信息外键
      },

      agencyQuery: {
        agency: "",
        page: 1,
        limit: 200,
        // customerId: store.getters.customerId,
      },
      cliList: [],
      srgList: [],
      packageList: [],
      deletePackageList: [],
      deleteCliList: [],
      deleteSrgList: [],
      deletepKGList: [],
      btSaveStr: "保存",
      curSrgIndex: 0,
      filterQuery: {
        deviceId: "",
      },
      classifyOptions: [],
      value: [],
      isDeviceKit: {
        0: "否",
        1: "是",
      },
      isEdit: false,
      selectIdentifyType: 0,
      currentIdentifyInfo: "",
      selectCodeType: 0,
      randomType: "", //生产随机码类型
      randomCode: "", //服务器拉下来的随机码
      pkgQuery: {
        UDI_DevicePkgOID: "", //
        pkgType: "", //产品包装级别
        pkgDeviceId: "", //包装产品标识
        pkgQuantity: "", //包装内含小一级产品标识数量
        containsDIPackage: "", //包装内含小一级包装产品标识
        UDI_DeviceId_FK: this.productDetail.UDI_DeviceIdOID, //产品标识信息外键
      },

      packageVisible: false,
      selectDialogVisible: false,
      formName: "",
      intentFacident: "",
      total: 0,
      loading: true,
      index: null,
      agencyList: [],
      rules: {
        IndentCode: [
          {
            required: true,
            message: "请选择厂商识别码",
            trigger: "change",
          },
        ],
        primaryDeviceId: [
          {
            required: true,
            message: "请输入最小销售单元产品标识",
            trigger: "change",
          },
        ],
        publishDate: [
          {
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],

        pkgQuantity: [
          {
            required: true,
            message: "请输入最小销售单元内使用单元数量",
            trigger: "blur",
          },
        ],

        isDirectMark: [
          {
            required: true,
            message: "是否有本体直接标识",
            trigger: "change",
          },
        ],
        carrierValue: [
          {
            required: true,
            message: "请选择标识载体",
            trigger: "change",
          },
        ],
        isSameRegDI: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        isLotBatch: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        isManufacturingDate: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        isSerialNumber: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        isExpirationDate: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
      },
    };
  },

  methods: {
    //添加包装表示
    handleAddpackageClick(index, row) {
      this.curSrgIndex = index;

      this.formName = "add";
      if (row !== null) {
        this.packageVisible = true;
        this.formName = "edit";
        this.pkgQuery = row;
      } else {
        this.pkgQuery = {
          UDI_DevicePkgOID: "", //
          pkgType: "", //产品包装级别
          pkgDeviceId: "", //包装产品标识
          pkgQuantity: "", //包装内含小一级产品标识数量
          containsDIPackage: "", //包装内含小一级包装产品标识
          UDI_DeviceId_FK: this.productDetail.UDI_DeviceIdOID, //产品标识信息外键
        };
        console.log(
          "packageList = " + this.packageList + this.packageList.length
        );
        var start = 1;
        if (this.packageList.length == 0) {
          start = 1;
          if (
            this.productDetail.useUnitDeviceId != "" ||
            this.productDetail.directMarkDI != ""
          ) {
            start++;
          }

          var id =
            start +
            this.productDetail.primaryDeviceId.substring(
              1,
              this.productDetail.primaryDeviceId.length - 1
            );
          console.log(this.productDetail.primaryDeviceId + "****" + id);
          this.pkgQuery.pkgDeviceId = this.getGsi(id);
          this.pkgQuery.containsDIPackage = this.productDetail.primaryDeviceId;
          this.pkgQuery.pkgQuantity = 1;

          // if (
          //   this.productDetail.useUnitDeviceId != "" &&
          //   this.productDetail.useUnitDeviceId != null
          // ) {
          //   console.log(1);
          //   this.pkgQuery.pkgDeviceId = this.productDetail.primaryDeviceId;
          //   this.pkgQuery.pkgQuantity = this.productDetail.pkgQuantity;
          //   this.pkgQuery.containsDIPackage = this.productDetail.useUnitDeviceId;
          // } else if (
          //   this.productDetail.directMarkDI != "" &&
          //   this.productDetail.directMarkDI != null
          // ) {
          //   console.log(2);
          //   this.pkgQuery.pkgDeviceId = this.productDetail.primaryDeviceId;
          //   this.pkgQuery.pkgQuantity = 1;
          //   this.pkgQuery.containsDIPackage = this.productDetail.directMarkDI;
          // } else {
          //   console.log(3);
          //   var start = Number(
          //     this.productDetail.primaryDeviceId.substring(0, 1)
          //   );
          //   start++;
          //   var id =
          //     start +
          //     this.productDetail.primaryDeviceId.substring(
          //       1,
          //       this.productDetail.primaryDeviceId.length
          //     );
          //   console.log(this.productDetail.primaryDeviceId + "****" + id);
          //   this.pkgQuery.pkgDeviceId = id;
          //   this.pkgQuery.containsDIPackage = this.productDetail.primaryDeviceId;
          // }
        } else {
          let temp = this.packageList[this.packageList.length - 1];

          start = Number(temp.pkgDeviceId.substring(0, 1));
          start++;
          var id =
            start + temp.pkgDeviceId.substring(1, temp.pkgDeviceId.length - 1);
          this.pkgQuery.pkgDeviceId = this.getGsi(id);
          this.pkgQuery.containsDIPackage = temp.pkgDeviceId;
          this.pkgQuery.pkgQuantity = 1;
        }
        if (start < 9) this.onSrgSubmit();
      }
    },
    handleChange(value) {
      console.log(value);
    },
    //弹框选择产品项目码
    selectCode(value) {
      console.log(value);
      if (this.intentFacident == "") {
        this.$message.warning("请选择厂商识别码与发码机构！");
      } else {
        this.selectCodeType = value;
        this.selectDialogVisible = true;
      }
    },
    deleteCliDialog(index, row) {
      console.log(index + "---" + row);
      if (row.UDI_DeviceCliOID != null) {
        row.isDelete = 1;
        this.deleteCliList.push(row);
        this.cliList.splice(index, 1);
        console.log("真" + index + "---" + row);
      } else {
        this.cliList.splice(index, 1);
        console.log("假" + index + "---" + row);
      }
    },
    deleteSrgDialog(index, row) {
      console.log(index + "---" + row);
      if (row.UDI_DeviceSrgOID != "") {
        row.isDelete = 1;
        this.deleteSrgList.push(row);
        this.srgList.splice(index, 1);
        console.log("真" + index + "---" + row);
      } else {
        this.srgList.splice(index, 1);
        console.log("假" + index + "---" + row);
      }
    },

    onSrgSubmit() {
      if (this.formName == "add") {
        this.packageList.push(this.pkgQuery);
        console.log("---" + this.srgList.length);
        this.cancelDialog();
      } else {
        this.packageList[this.curSrgIndex] = this.pkgQuery;
        this.cancelDialog();
      }
      this.strgQuery = {
        UDI_DeviceSrgOID: "", //
        storageType: "", //储存或操作条件
        lowValue: "", //最低值
        highValue: "", //最高值
        unit: "", //计量单位
        UDI_SubProduct_FK: "", //子类信息外键
      };
    },

    closeDialog() {
      this.selectDialogVisible = false;
    },

    cancelDialog() {
      this.packageVisible = false;

      this.selectDialogVisible = false;
    },
    //删除包装产品标识
    deletePkgDialog(index, row) {
      console.log(index + "---" + row);
      if (row.UDI_DevicePkgOID != null) {
        row.isDelete = 1;
        this.deletepKGList.push(row);
        this.packageList.splice(index, 1);
        console.log("真" + index + "---" + row);
      } else {
        this.packageList.splice(index, 1);
        console.log("假" + index + "---" + row);
      }
    },

    //获取包装列表
    getPkList() {
      listPkg(this.filterQuery)
        .then((response) => {
          this.loading = false;
          this.packageList = response.data || [];
        })
        .catch(() => {
          this.loading = false;
          this.packageList = [];
        });
    },

    //获取产品分类
    getCPList() {
      listClassify()
        .then((response) => {
          this.loading = false;
          this.classifyOptions = response.data || [];
          console.log("response.data=" + response.data.length);
        })
        .catch(() => {
          this.loading = false;
          this.classifyOptions = [];
        });
    },

    //选择产品项目码
    getIdentifyInfo(row) {
      this.currentIdentifyInfo = row;
      if (this.selectCodeType == 1) {
        //选择使用单元标识
        console.log("---1");
        var str = this.getGsi("0" + this.currentIdentifyInfo.IdentifierCode);
        console.log(str);
        this.productDetail.useUnitDeviceId = str;
      } else if (this.selectCodeType == 2) {
        //选择本体标识
        var str = this.getGsi("0" + this.currentIdentifyInfo.IdentifierCode);
        this.productDetail.directMarkDI = str;
        console.log("---" + str);
      } else if (this.selectCodeType == 4) {
        //选择最小销售单元标识
        console.log(
          this.currentIdentifyInfo.IdentifierCode +
            "--" +
            this.currentIdentifyInfo.IdentifierCode.length
        );
        var str = this.getGsi("0" + this.currentIdentifyInfo.IdentifierCode);
        this.productDetail.primaryDeviceId = str;
        console.log("---" + str);
      } else if (this.selectCodeType == 5) {
        var str = this.getGsi("0" + this.currentIdentifyInfo.IdentifierCode);

        this.pkgQuery.pkgDeviceId = str;
        console.log("---" + str);
      }
    },

    //选择标识载体
    carrierChanged(value) {
      console.log("value=" + value);
      var str = "";
      for (var i = 0; i < value.length; i++) {
        if (str != "") {
          str = str + ";" + value[i];
        } else {
          str = str + value[i];
        }
      }
      console.log("str=" + str);
      this.productDetail.deviceCarrier = str;
    },

    pkgQuantityChange(value) {
      if (value <= 1 || value == "") {
        this.productDetail.useUnitDeviceId = "";
        this.productDetail.isDirectMark = "2";
      } else {
        var str2 = this.getGsi(
          "1" +
            this.productDetail.primaryDeviceId.substring(
              1,
              this.productDetail.primaryDeviceId.length - 1
            )
        );
        console.log(
          "  this.productDetail.useUnitDeviceId=" +
            this.productDetail.useUnitDeviceId
        );
        this.productDetail.useUnitDeviceId = str2;
        this.productDetail.isDirectMark = "2";
        this.productDetail.isSameDeviceId = "2";
        this.productDetail.directMarkDI = "";
      }
    },

    isDirectChange(value) {
      console.log(
        this.productDetail.isSameDeviceId +
          this.productDetail.isDirectMark +
          value
      );
      if (
        this.productDetail.isSameDeviceId != 2 ||
        this.productDetail.isDirectMark != 1
      ) {
        this.productDetail.directMarkDI = "";
      } else if (this.productDetail.isSameDeviceId == 2) {
        console.log(this.productDetail.primaryDeviceId.length);
        if (this.productDetail.primaryDeviceId.length > 12) {
          var str = this.productDetail.primaryDeviceId.substring(1, 13);
          var result = this.getGsi("1" + str);
          this.productDetail.directMarkDI = result;
        }
      }
    },
    //选择发码机构
    intenStandagency(value) {
      this.productDetail.UDI_StandAgency_FK = value;
      this.productDetail.issuingAgency = value
        ? this.agencyList.find((ele) => ele.UDI_StandAgencyOID === value)
            .issuingAgency
        : "";

      this.agencyQuery.agency = this.productDetail.issuingAgency;
      this.getIndentCodeList();
    },

    //选择厂商识别码
    intenCodeChanged(value) {
      console.log("选择厂商识别码=" + value);
      this.productDetail.IndentCode = value;
      this.productDetail.UDI_FacIdentCode_FK = value;
      this.productDetail.IndentCode = value
        ? this.intenCodeValue.find((ele) => ele.UDI_FacIdentCodeOID === value)
            .IndentCode
        : "";
      this.intentFacident = this.productDetail.UDI_FacIdentCode_FK;
      console.log("选择厂商识别码=" + this.productDetail.IndentCode);
    },

    //获取厂商标识数据
    getIndentCodeList() {
      this.loading = true;
      listFacidentcode(this.agencyQuery)
        .then((response) => {
          this.loading = false;
          this.intenCodeValue = response.data.list || [];
          // this.productDetail.IndentCode = this.intenCodeValue[0].UDI_FacIdentCodeOID;
          // this.intenCodeChanged(this.productDetail.IndentCode);
        })
        .catch(() => {
          this.loading = false;
          this.intenCodeValue = [];
        });
    },

    primarychange(value) {
      console.log(value);
      if (value.length == 13) {
        var head = value.substring(0, 1);
        var str = this.getGsi(head + value.substring(1, value.length));
        this.productDetail.primaryDeviceId = str;
      }
    },

    //生成随机标识
    intenRandom(value) {
      this.pkgQuantity = 0;
      this.productDetail.useUnitDeviceId = "";
      this.productDetail.directMarkDI = "";
      this.packageList = [];
      let tQuery = {
        IdentifierCode: this.productDetail.IndentCode,
        IsUDI: "0",
        isSPTM: "",
        IsReg: "0",
      };

      if (this.randomType == 1) {
        console.log(this.productDetail.SPTM);
        if (this.productDetail.SPTM != "" && this.productDetail.SPTM != null) {
          var str = this.getGsi(
            "0" +
              this.productDetail.SPTM.substring(
                0,
                this.productDetail.SPTM.length - 1
              )
          );
          this.productDetail.primaryDeviceId = str;
        } else {
          this.productDetail.primaryDeviceId = "";
          this.$alert("该产品没有商品条码！", "提示", {
            confirmButtonText: "确定",
            callback: (action) => {
              this.randomType = "";
            },
          });
        }
      } else if (this.randomType == 2 || this.randomType == 3) {
        console.log(
          this.productDetail.IndentCode + "---" + this.intentFacident
        );
        if (
          this.productDetail.IndentCode == "" ||
          this.productDetail.IndentCode == null
        ) {
          this.randomType = "";
          this.$message.warning("请选择厂商识别码与发码机构！");
          return;
        }
        if (this.randomType == 3) {
          tQuery.isSPTM = "0";
        }
        randomIdentifier(tQuery)
          .then((response) => {
            if (response.code == 20000) {
              if (response.data == null) {
                this.$message.error("该产商识别码下无产品项目码!");
              } else {
                var str = this.getGsi("0" + response.data);
                this.productDetail.primaryDeviceId = str;
                var str2 = this.getGsi(
                  "1" +
                    this.productDetail.primaryDeviceId.substring(
                      1,
                      this.productDetail.primaryDeviceId.length - 1
                    )
                );
              }
            } else {
              this.$message.error("获取产品项目码失败!");
            }
          })
          .catch(() => {
            this.productDetail.primaryDeviceId = "";
          });
      } else if (value == 4) {
        if (
          this.productDetail.IndentCode == "" ||
          this.productDetail.IndentCode == null
        ) {
          this.randomType = "";
          this.$message.warning("请选择厂商识别码与发码机构！");
          return;
        }
        this.productDetail.primaryDeviceId = "";
      }
      this.productDetail.useUnitDeviceId = "";
    },

    //生产校验位
    getGsi(str) {
      var arrs = [];
      for (var i = 0; i < str.length; i++) {
        arrs[str.length - 1 - i] = Number(str.substr(i, 1));
      }

      var osi = 0;
      for (var i = 0; i < arrs.length; i = i + 2) {
        osi = osi + arrs[i];
      }
      osi = osi * 3;

      var jsi = 0;
      for (var i = 1; i < arrs.length; i = i + 2) {
        jsi = jsi + arrs[i];
      }

      var jyw = 10 - ((osi + jsi) % 10);
      if (jyw == 10) {
        jyw = 0;
      }
      return str + jyw;
    },

    //生成本体标识
    intentDirectMark(value) {
      this.isDirectChange(value);
      if (value == 2) {
        if (this.productDetail.primaryDeviceId.length > 12) {
          var str = this.productDetail.primaryDeviceId.substring(1, 13);
          var result = this.getGsi("1" + str);
          this.productDetail.directMarkDI = result;
        }
      }
    },

    //提交保存
    postData() {
      if (this.productDetail.primaryDeviceId == "") {
        this.$message.warning("最小销售单元产品标识不能为空");
        return;
      }
      if (this.productDetail.publishDate == "") {
        this.$message.warning("产品标识发布日期不能为空");
        return;
      }

      this.postQuery = {
        udiDeviceIdEntity: this.productDetail,
        udiDevicepkEntities: this.packageList,
      };

      if (this.isEdit) {
        for (var i = 0; i < this.deletepKGList.length; i++) {
          this.postQuery.udiDevicepkEntities.push(this.deletepKGList[i]);
        }

        modifyDeviceId(this.postQuery)
          .then((response) => {
            if (response.code == 20000) {
              this.$emit("closeModifyDi", true);
            } else {
              this.$message.error(response.message);
              // this.$emit("closeModifyDi", false);
            }
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
            this.$message.error("服务器出错！");
            // this.$emit("closeModifyDi", false);
          });
      } else {
        addDeviceId(this.postQuery)
          .then((response) => {
            if (response.code == 20000) {
              this.$emit("closeModifyDi", true);
            } else {
              this.$message.error(response.message);
            }
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
            this.$message.error("服务器出错！");
          });
      }
    },
    //取消保存
    cancelModify() {
      this.$emit("closeModifyDi", null);
    },
    //获取发码机构数据
    getAgencyList() {
      this.loading = true;
      listStandagency()
        .then((response) => {
          this.loading = false;
          this.agencyList = response.data.list || [];
          this.productDetail.issuingAgency = this.agencyList[0].UDI_StandAgencyOID;
          this.intenStandagency(this.productDetail.issuingAgency);
        })
        .catch(() => {
          this.loading = false;
          this.agencyList = [];
        });
    },
  },

  filters: {},
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  components: {
    selectIdentify,
  },
  created() {
    console.log(this.productDetail.UDI_DeviceIdOID);
    this.filterQuery.deviceId = this.productDetail.UDI_DeviceIdOID;
    console.log(this.filterQuery.deviceId);
    this.getPkList();
    this.getCPList();
    this.getAgencyList();

    //初始化载体标识
    if (
      this.productDetail.deviceCarrier == null ||
      this.productDetail.deviceCarrier == ""
    ) {
      this.carrierValue.push(this.carrierOptions[0].value);
      this.carrierValue.push(this.carrierOptions[1].value);
      this.carrierValue.push(this.carrierOptions[2].value);
      this.carrierChanged(this.carrierValue);
    } else {
      this.carrierValue = this.productDetail.deviceCarrier.split(";");
    }

    //区分编辑或添加
    if (this.productDetail.UDI_DeviceIdOID != "") {
      this.isEdit = true;
      this.agencyQuery.agency = this.productDetail.issuingAgency;
      this.getIndentCodeList();
      this.intentFacident = this.productDetail.UDI_FacIdentCode_FK;
      this.btSaveStr = "修改保存";
    } else {
      this.isEdit = false;
      this.btSaveStr = "保存";
    }
    this.productDetail.IndentCode = "";
    console.log(
      "str=" +
        this.productDetail.UDI_DeviceIdOID +
        "---" +
        this.productDetail.UDI_Product_FK
    );
  },
};
</script>

