<template>
  <div>
    <el-card class="el-card">
      <div slot="header" style="font-weight: bold">
        <span>产商识别码</span>
      </div>
      <el-form
        :inline="true"
        :model="filterQuery"
        style="display: flex"
        size="mini"
      >
        <el-form-item class="query-form-item">
          <el-input
            v-model="filterQuery.agency"
            placeholder="发码机构"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button-group>
            <el-button
              type="primary"
              icon="el-icon-refresh"
              @click="onReset"
            ></el-button>
            <el-button type="primary" icon="search" @click="getList"
              >查询</el-button
            >
            <el-button
              type="primary"
              icon="search"
              @click.native="handleForm(null)"
              >添加</el-button
            >
          </el-button-group>
        </el-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        :data="list"
        @row-click="intentIdentify"
        style="width: 100%"
      >
        <el-table-column label="序号" type="index" width="80"></el-table-column>
        <el-table-column
          label="发码机构编码名称"
          prop="issuingAgency"
          width="140"
        ></el-table-column>
        <el-table-column
          label="厂商识别码 "
          prop="IndentCode"
          width="140"
        ></el-table-column>
        <el-table-column
          label="序号位数 "
          prop="NoDigit"
          width="140"
        ></el-table-column>
        <el-table-column
          label="备注  "
          prop="Text1"
          width="140"
        ></el-table-column>
        <el-table-column label="操作" align="center" width="120">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              v-bind:disabled="scope.row.isGenerate == 1"
              @click.native="generateCode(scope.row)"
              >生产产品项目码</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              v-bind:disabled="scope.row.isGenerate == 1"
              @click.native="handleForm(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              @click.native.stop="deleteDialog(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        :title="formMap[formName]"
        :visible.sync="formVisible"
        width="35%"
        top="5vh"
      >
        <el-form :model="editAgencyQuery" :rules="rules" 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="issuingAgency">
                <el-select
                  v-model="editAgencyQuery.issuingAgency"
                  style="width: 100%"
                  @change="intentAgency"
                  placeholder="请选择发码机构"
                >
                  <el-option
                    v-for="item in agencyList"
                    :key="item.UDI_StandAgencyOID"
                    :label="item.issuingAgency"
                    :value="item.UDI_StandAgencyOID"
                  >
                  </el-option>
                </el-select>
              </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="productName">
                <el-input
                  v-model="editAgencyQuery.IndentCode"
                  maxLength="11"
                  @input="indentCodeChange"
                  :disabled="editAgencyQuery.issuingAgency == ''"
                  size="small"
                ></el-input>
              </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="NoDigit">
                <el-input
                  v-model="editAgencyQuery.NoDigit"
                  :disabled="true"
                  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="Text1">
                <el-input
                  v-model="editAgencyQuery.Text1"
                  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="onAddSubmit()"
            >保存</el-button
          >
          <el-button @click.native="cancelAdd" size="small">取消</el-button>
        </div>
      </el-dialog>

      <el-dialog title="选择发码机构" :visible.sync="agencyVisible" width="35%">
        <el-table
          v-loading="loading"
          @row-click="intentAgency"
          :data="agencyList"
          style="width: 100%"
        >
          <el-table-column
            label="序号"
            type="index"
            width="80"
          ></el-table-column>
          <el-table-column
            label="发码机构编码名称"
            prop="issuingAgency"
            width="140"
          ></el-table-column>
          <el-table-column
            label="发码机构名称   "
            prop="AgencyName"
            width="140"
          ></el-table-column>
        </el-table>
      </el-dialog>
      <el-pagination
        :page-size="filterQuery.limit"
        @current-change="handleCurrentChange"
        layout="prev, pager, next"
        :total="total"
      ></el-pagination>
    </el-card>
    <el-card class="el-card">
      <div slot="header" style="font-weight: bold">
        <span>产品项目码</span>
      </div>
      <el-form
        :inline="true"
        :model="subFilterQuery"
        style="display: flex"
        size="mini"
      >
        <el-form-item class="query-form-item">
          <el-input
            v-model="subFilterQuery.IdentifierCode"
            placeholder="产品项目码"
          ></el-input>
        </el-form-item>
        <el-form-item class="query-form-item">
          <el-select v-model="subFilterQuery.IsUDI" placeholder="是否已使用UDI">
            <el-option label="否" value="0"></el-option>
            <el-option label="是" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="query-form-item">
          <el-select
            v-model="subFilterQuery.isSPTM"
            placeholder="是否已使用商品条码"
          >
            <el-option label="否" value="0"></el-option>
            <el-option label="是" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="query-form-item">
          <el-select
            v-model="subFilterQuery.IsReg"
            placeholder="是否已使用注册证"
          >
            <el-option label="否" value="0"></el-option>
            <el-option label="是" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button-group>
            <el-button
              type="primary"
              icon="el-icon-refresh"
              @click="onSubReset"
            ></el-button>
            <el-button type="primary" icon="search" @click="getSubList"
              >查询</el-button
            >
          </el-button-group>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="subList" border style="width: 100%">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column prop="IdentifierCode" label="产品项目码 " width="150">
        </el-table-column>
        <el-table-column label="厂商识别码" width="150">
          <template slot-scope="scope">
            <span>{{ editAgencyQuery.IndentCode }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="SeqNumber" label="顺序号" width="150">
        </el-table-column>
        <el-table-column prop="IsUDI" label="是否使用UDI" width="120">
          <template slot-scope="scope">
            <span>{{ isTip[scope.row.IsUDI] }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="isSPTM" label="是否使用商品条码" width="120">
          <template slot-scope="scope">
            <span>{{ isTip[scope.row.isSPTM] }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="IsReg" label="是否使用注册证" width="120">
          <template slot-scope="scope">
            <span>{{ isTip[scope.row.IsReg] }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" width="120">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="deleteSubDialog(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        :visible.sync="addDialogVisible"
        width="95%"
        v-if="addDialogVisible"
      >
        <modifySp :productDetail="productDetail"></modifySp>
      </el-dialog>

      <el-pagination
        :page-size="subFilterQuery.limit"
        @current-change="handleSubCurrentChange"
        layout="prev, pager, next"
        :total="subTotal"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import {
  listStandagency,
  addStandagency,
  modifyStandagency,
  deleteStandagency,
} from "../../api/basic/agency";

import {
  listFacidentcode,
  addFacidentcode,
  modifyFacidentcode,
  deleteFacidentcode,
} from "../../api/basic/facidentcode";

import {
  listIdentifier,
  addIdentifier,
  modifyIdentifier,
  deleteIdentifier,
  generateIdentifier,
} from "../../api/basic/identifier";
import store from "../../store/index";
import modifySp from "../publish//modifySubProduct";

export default {
  data() {
    return {
      filterQuery: {
        agency: "",
        page: 1,
        limit: 5,
        // customerId: store.getters.customerId,
      },

      subFilterQuery: {
        facidentcode: "",
        IdentifierCode: "",
        IsUDI: "",
        isSPTM: "",
        IsReg: "",
        page: 1,
        limit: 10,
        // customerId: store.getters.customerId,
      },
      editQuery: {
        productName: "",
        Text1: "",
        UDI_ProductOID: "",
      },

      editAgencyQuery: {
        issuingAgency: "",
        IndentCode: "",
        Text1: "",
        NoDigit: "",
        UDI_FacIdentCodeOID: "",
        UDI_StandAgency_FK: "",
      },

      formVisible: false,
      list: [],
      total: 0,
      subList: [],
      subTotal: 0,

      agencyVisible: false,
      agencyList: [],

      formName: null,
      formMap: {
        add: "新增产商识别码",
        edit: "编辑产商识别码",
      },

      isTip: {
        0: "否",
        1: "是",
      },

      rules: {
        // productName: [
        //   { required: true, message: "请输用户名称", trigger: "blur" },
        // ],
      },
      productId: "",
      addDialogVisible: false,
      productDetail: {
        UDI_SubProductOID: "",
        brandName: "",
        productOldCode: "",
        productCode: "",
        companyName: "",
        companyGBName: "",
        regcardCodes: "",
        businessLicense: "",
        deviceType: "",
        productType: "",
        deviceKit: "",
        specification: "",
        deviceDescription: "",
        catalogNumber: "",
        isMRISafety: "",
        MRISafetyInfo: "",
        singleUse: "",
        reuseTimes: "",
        isDeviceSterile: "",
        isSterilizationUse: "",
        sterilizationMethod: "",
        otherInfoLink: "",
        YBBM: "",
        deviceEndDate: "",
        specialText: "",
        clinicalSizeText: "",
        SPTM: "",
        Quality: "",
        QualityUnit: "",
        productOldName: "",
        productName: "",
        deviceclinicals: [],
        devicestorages: [],
      },
    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        agency: "",
        page: 1,
        limit: 5,
        // customerId: store.getters.customerId,
      };
      this.getList();
    },

    selectAgenct() {
      this.agencyVisible = true;
    },

    intentAgency(value) {
      // this.agencyVisible = false;
      // this.editAgencyQuery.issuingAgency = row.issuingAgency;
      this.editAgencyQuery.UDI_StandAgency_FK = value;
    },

    intentIdentify(row) {
      (this.subFilterQuery = {
        facidentcode: row.UDI_FacIdentCodeOID,
        page: 1,
        limit: 10,
      }),
        (this.editAgencyQuery = row);
      console.log(this.editAgencyQuery.IndentCode + "-----");
      this.getSubList(this.subFilterQuery);
    },

    onSubReset() {
      this.subFilterQuery = {
        facidentcode: this.subFilterQuery.facidentcode,
        IdentifierCode: "",
        IsUDI: "",
        isSPTM: "",
        IsReg: "",
        page: 1,
        limit: 10,
        // customerId: store.getters.customerId,
      };
      this.getSubList();
    },

    getList() {
      this.loading = true;
      listFacidentcode(this.filterQuery)
        .then((response) => {
          this.loading = false;
          this.list = response.data.list || [];
          this.total = response.data.total || 0;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    getSubList() {
      this.loading = true;
      if (
        this.subFilterQuery.facidentcode == null ||
        this.subFilterQuery.facidentcode == ""
      ) {
        this.$message.warning("请选择产商识别码");
        return;
      }
      listIdentifier(this.subFilterQuery)
        .then((response) => {
          this.loading = false;
          this.subList = response.data.list || [];
          this.subTotal = response.data.total || 0;
        })
        .catch(() => {
          this.loading = false;
          this.subList = [];
          this.subTotal = 0;
        });
    },
    //获取发码机构数据
    getAgencyList() {
      this.loading = true;
      listStandagency()
        .then((response) => {
          this.loading = false;
          this.agencyList = response.data.list || [];
        })
        .catch(() => {
          this.loading = false;
          this.agencyList = [];
        });
    },

    //添加产商识别码
    handleForm(row) {
      this.formVisible = true;
      this.formName = "add";

      if (row !== null) {
        this.formName = "edit";
        this.editAgencyQuery = row;
      } else {
        this.editAgencyQuery = {
          issuingAgency: "",
          IndentCode: "",
          Text1: "",
          NoDigit: "",
          UDI_FacIdentCodeOID: "",
          UDI_StandAgency_FK: "",
        };
      }
    },

    deleteOrders(data) {
      this.loading = true;
      let tquery = {
        id: data.UDI_FacIdentCodeOID + "",
      };

      deleteFacidentcode(tquery)
        .then((response) => {
          if (response.code == 20000) {
            this.$message.success("删除成功！");
            this.getList();
            this.getSubList();
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(() => {});
    },

    cancelAdd() {
      this.formVisible = false;
      this.addDialogVisible = false;
    },
    onAddSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.cancelAdd();
          if (this.formName == "edit") {
            addFacidentcode(this.editAgencyQuery)
              .then((response) => {
                if (response.code == 20000) {
                  this.loading = false;
                  this.getList();
                } else {
                  this.$message.error(response.message);
                }
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            addFacidentcode(this.editAgencyQuery)
              .then((response) => {
                if (response.code == 20000) {
                  this.loading = false;
                  this.getList();
                } else {
                  this.$message.error(response.message);
                }
              })
              .catch(() => {
                this.loading = false;
              });
          }
        }
      });
    },

    deleteDialog(rowId) {
      this.$confirm(
        "此操作将删除厂商识别码以及关联的产品项目码, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.deleteOrders(rowId);
        })
        .catch(() => {});
    },

    deleteSubDialog(row) {
      this.$confirm("此操作将永久删除该产品项目码, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let tquery = {
            id: row.UDI_IdentifierOID,
          };
          deleteIdentifier(tquery)
            .then((response) => {
              if (response.code == 20000) {
                this.$message.success("删除成功！");
                this.getSubList();
              } else {
                this.$message.error(response.message);
              }
            })
            .catch(() => {});
        })
        .catch(() => {});
    },

    handleCurrentChange(val) {
      this.filterQuery.page = val;
      this.getList();
    },

    //子类操作
    handleAddClick(row) {
      this.addDialogVisible = true;
      if (row == null) {
        this.productDetail = {
          UDI_SubProductOID: "",
          brandName: "",
          productOldCode: "",
          productCode: "",
          companyName: "",
          companyGBName: "",
          regcardCodes: "",
          businessLicense: "",
          deviceType: "",
          productType: "",
          deviceKit: "",
          specification: "",
          deviceDescription: "",
          catalogNumber: "",
          isMRISafety: "",
          MRISafetyInfo: "",
          singleUse: "",
          reuseTimes: "",
          isDeviceSterile: "",
          isSterilizationUse: "",
          sterilizationMethod: "",
          otherInfoLink: "",
          YBBM: "",
          deviceEndDate: "",
          specialText: "",
          clinicalSizeText: "",
          SPTM: "",
          Quality: "",
          QualityUnit: "",
          productOldName: "",
          productName: "",
        };
      } else this.productDetail = row;
    },
    handleSubCurrentChange(val) {
      this.subFilterQuery.page = val;
      this.getSubList();
    },

    //生成项目码
    generateCode(row) {
      generateIdentifier(row)
        .then((response) => {
          if (response.code == 20000) {
            this.loading = false;
            this.getList();
            this.getSubList();
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
    //生产序号位数
    indentCodeChange(value) {
      console.log("value =" + value);
      if (value.length > 11) {
        this.$message.error("不能超过11位");
      } else {
        this.editAgencyQuery.NoDigit = 12 - value.length;
      }
    },
  },
  mounted() {},

  created() {
    this.getList();
    this.getAgencyList();
  },
};
</script>
<style>
.edit-text {
  width: 100%;
  font-size: 12px;
  margin-top: 10px;
  font-family: "Microsoft YaHei";
  color: #000000;
}
</style>