<template>
  <div>
    <el-card>
      <el-form :model="filterQuery" label-width="120px" v-if="showSearch">
        <el-row>
          <el-col :span="8">
            <el-form-item label="DI/物资编码:">
              <el-input v-model="filterQuery.unionCode" style="width:90%" placeholder="请输入DI/物资编码"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物资名称:">
              <el-input v-model="filterQuery.cpmctymc" style="width:90%" placeholder="请输入物资名称"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产企业:">
              <el-input v-model="filterQuery.manufactory" style="width: 90%" placeholder="请输入生产企业"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="注册/备案人:">
              <el-input v-model="filterQuery.ylqxzcrbarmc" style="width: 90%" placeholder="请输入注册/备案人"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="统一社会信用号:">
              <el-input v-model="filterQuery.tyshxydm" style="width: 90%" placeholder="请输入统一社会信用号"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="注册/备案号:">
              <el-input v-model="filterQuery.zczbhhzbapzbh" style="width: 90%" placeholder="请输入注册/备案号"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="top-right-btn">
        <el-button-group style="display:flex;">
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="submitSearch">查询</el-button>
          <el-button type="primary" icon="el-icon-upload2" @click="selectExport">选中导入</el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>
      <el-table v-loading="loading" :data="list" style="width: 100%" @current-change="handleSelectionChange" border
                highlight-current-row>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="物资名称" prop="cpmctymc" show-overflow-tooltip></el-table-column>
        <el-table-column label="生产企业" prop="manufactory" show-overflow-tooltip></el-table-column>
        <el-table-column label="注册/备案号" prop="zczbhhzbapzbh" show-overflow-tooltip></el-table-column>
        <el-table-column label="注册/备案人" prop="ylqxzcrbarmc" show-overflow-tooltip></el-table-column>
        <el-table-column label="产品类别" prop="cplb" show-overflow-tooltip></el-table-column>
        <el-table-column label="器械类别" prop="qxlb" show-overflow-tooltip></el-table-column>
        <el-table-column label="分类编码" prop="classifyName" show-overflow-tooltip></el-table-column>
        <el-table-column width="60" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="handleModifyClick(scope.row)"
            >详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

<!--      <el-dialog title="请选择对应的注册/备案证" :visible.sync="dialogFormVisible" append-to-body width="40%">-->
<!--        <el-form :model="form">-->
<!--          <el-form-item label="" prop="isGive">-->
<!--            <el-radio-group v-model="currentCert">-->
<!--              <el-radio style="width: 100%;  margin-top: 10px;" :label="item" :key="item" v-for="item in certList">{{-->
<!--                  item-->
<!--                }}-->
<!--              </el-radio>-->
<!--            </el-radio-group>-->
<!--          </el-form-item>-->
<!--        </el-form>-->
<!--        <div slot="footer" class="dialog-footer">-->
<!--          <el-button type="primary" @click="selectCert">确 定</el-button>-->
<!--          <el-button @click="dialogFormVisible = false">取 消</el-button>-->
<!--        </div>-->
<!--      </el-dialog>-->
      <el-dialog
        title="产品信息详情"
        :visible.sync="editDialogVisible"
        width="70%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        append-to-body
        v-if="editDialogVisible"
      >

        <el-card>
          <el-form :model="editQuery" ref="editQuery" label-width="140px" style="margin-top: 20px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="物资名称" prop="cpmctymc">
                  <el-input style="width: 90%" size="small" splaceholder="请输入物资名称" :disabled="true"
                            v-model="editQuery.cpmctymc"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="el-col">
                <el-form-item label="器械类别" prop="qxlb">
                  <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入器械类别"
                            v-model="editQuery.qxlb"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12" class="el-col">
                <el-form-item label="产品类别" prop="cplb">
                  <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入产品类别"
                            v-model="editQuery.cplb"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="el-col">
                <el-form-item label="分类编码" prop="flbm">
                  <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入分类编码"
                            v-model="editQuery.flbm"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12" class="el-col">
                <el-form-item label="医疗器械注册人" prop="ylqxzcrbarmc">
                  <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入医疗器械注册人"
                            v-model="editQuery.ylqxzcrbarmc"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="el-col">
                <el-form-item label="注册人英文名称" prop="ylqxzcrbarywmc">
                  <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入注册人英文名称"
                            v-model="editQuery.ylqxzcrbarywmc"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12" class="el-col">
                <el-form-item label="统一社会信用号" prop="tyshxydm">
                  <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入统一社会信用号"
                            v-model="editQuery.tyshxydm"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="el-col">
                <el-form-item label="注册证/备案号" prop="zczbhhzbapzbh">
                  <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入注册证/备案号"
                            v-model="editQuery.zczbhhzbapzbh"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
        <el-card>

          <el-table :data="detailList" style="width: 100%" @current-change="handleSelectionChange" border
                    highlight-current-row>
            <el-table-column label="序号" type="index"></el-table-column>
            <el-table-column label="物资名称" prop="cpmctymc" show-overflow-tooltip></el-table-column>
            <el-table-column label="规格型号" prop="ggxh" show-overflow-tooltip></el-table-column>
            <el-table-column label="最小销售产品标识" prop="nameCode" width="140"></el-table-column>
            <el-table-column label="产品编码" prop="thirdId" show-overflow-tooltip></el-table-column>
          </el-table>


          <pagination
            v-show="detailTotal>0"
            :total="detailTotal"
            :limit.sync="detailQuery.limit"
            :page.sync="detailQuery.page"
            @pagination="handeleDetailChange"
          ></pagination>
        </el-card>
      </el-dialog>
<!--      <el-dialog-->
<!--        title="耗材字典详情"-->
<!--        :visible.sync="udiRlDetailVisible"-->
<!--        width="85%"-->
<!--        :close-on-click-modal="false"-->
<!--        :close-on-press-escape="false"-->
<!--        append-to-body-->
<!--        v-if="udiRlDetailVisible"-->
<!--      >-->
<!--        <udiRlDetailDialog :editQuery="currentRow"></udiRlDetailDialog>-->
<!--      </el-dialog>-->
      <pagination
        v-show="total>0"
        :total="total"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="handleCurrentChange"
      ></pagination>

    </el-card>
  </div>
</template>

<script>
import {
  getUdiInfos,
  updateErpProduct,
  thirdSysDetail,
  getSmUdiInfos, getCertUdis
} from "@/api/basic/udiRelevance";
import {udiUpload} from "@/api/thrsys/smp"
import {filterByUuid} from "@/api/basic/udiInfo";
import {
  insertCompanyProductRelevance,
  updateCompanyProductRelevance, insertFilter
} from "@/api/basic/udiRlSuptRelevance";
import store from "@/store";
import {isBlank} from "@/utils/strUtil";
import udiRlDetailDialog from "@/views/purchase/purApply/purUdiRlDetailDialog.vue";

export default {
  name: "UdIInfoSelect",
  props: {
    unitFk: {
      type: Object,
      required: true,
    },
    selectType: {
      type: Object,
      required: true,
    },
    closeSelDialog: {
      type: Function,
      required: true,
    },
  },
  data() {
    return {
      showSearch: true,
      filterQuery: {
        customerId: null,
        uuids: [],
        rlIds: [],
        ylqxzcrbarmc: "",
        supName: null,
        cpmctymc: "",
        nameCode: "",
        page: 1,
        limit: 20,
        addType: 1,
        isDisable: false,
      },
      //选择资质证书
      // dialogFormVisible: false,
      // currentCert: null,
      // currentRow: {}, // 设置初始值为空对象
      // udiRlDetailVisible: false,
      detailTotal: 0,
      detailQuery: {
        cpmctymc: null,
        manufactory: null,
        page: 1,
        limit: 20,
      },
      editQuery: null,
      relevanceEdit: {
        id: null,
        thirdId: "",
        isUseDy: false,
      },
      isImport: false,
      isImportUdi: false,
      isUploadSmp: false,
      isImportFile: false,
      uploadFileUrl: null,
      checked: false,
      list: [],
      detailList: [],
      sysList: [],
      total: 0,
      thirdNo: "",
      uuid: "111",
      thirdId: null,
      relId: "",
      thisData: {
        nameCode: null,
        cpmctymc: null,
        ggxh: null
      },
      multipleSelection: {},
      editDialogVisible: false,
      selectUdiDialogVisible: false,
      selectErpDialogVisible: false,
      selectLocalVisible: false,
    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        customerId: null,
        uuids: [],
        rlIds: [],
        ylqxzcrbarmc: "",
        supName: null,
        cpmctymc: "",
        nameCode: "",
        page: 1,
        limit: 20,
        unitFk: null,
        addType: 1,
      };
      this.getList();
    },

    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    submitSearch() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      this.filterQuery.customerId = store.getters.customerId;
      getCertUdis(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;
        });
    },

    getDetailList(detailQuery) {
      this.loading = true;
      getUdiInfos(detailQuery)
        .then((response) => {
          this.loading = false;
          this.detailList = response.data.list || [];
          this.detailTotal = response.data.total || 0;
        })
        .catch(() => {
          this.loading = false;
          this.detailList = [];
          this.detailTotal = 0;
        });
    },

    handleDetail(row) {
      this.editQuery = row;
    },

    combine() {
      this.combineQuery.productUuid = this.currentRow.uuid;
      this.combineQuery.customerId = store.getters.customerId;
      // this.combineQuery.zczbhhzbapzbh=this.currentRow.zczbhhzbapzbh;
      insertCompanyProductRelevance(this.combineQuery).then(response => {
        this.loading = false;
        if (response.code === 20000) {
          this.$message.success("添加成功");
          this.closeDialog(true);
        } else {
          this.$message.error(response.message);
        }
      });
    },
    // selectCert() {
    //   if (this.currentCert == null) {
    //     this.$message.error("请先选择对应的注册证！");
    //   } else {
    //     console.log("currentRow:", this.currentRow);
    //     if (this.currentRow && this.currentRow.hasOwnProperty("zczbhhzbapzbh")) {
    //       this.currentRow.zczbhhzbapzbh = this.currentCert;
    //       this.multipleSelection;
    //       console.log("执行选择对应注册证号")
    //       this.closeSelDialog(this.multipleSelection);
    //       // this.combine();
    //     } else {
    //       console.error("currentRow 或 zczbhhzbapzbh 未定义");
    //     }
    //   }
    // },



    selectExport() {
      var selectData = this.multipleSelection;
      console.log("日志："+selectData.id);
      if (selectData == null) {
        this.$message.warning("请先选入物资字典信息！");
        return;
      }
      // let str = this.multipleSelection.zczbhhzbapzbh;
      // console.log("日志zczbhhzbapzbh："+str);
      // if (!isBlank(str) && str.search(",") != -1) {
      //   this.currentCert = null;
      //   this.certList = str.split(',');
      //   this.dialogFormVisible = true;
      // } else {
      //   this.closeSelDialog(selectData);
      // }
      this.closeSelDialog(selectData.id);

    },
    allExport() {
      this.filterQuery.customerId = store.getters.customerId;
      insertFilter(this.filterQuery).then(response => {
        this.loading = false;
        if (response.code == 20000) {
          this.$message.success(response.data);
          this.$emit("closeSelDialog", true);
          this.closeSelDialog();
        } else {
          this.$message.error(response.message);
        }
      });
    },

    intentDetail() {
      this.isImportUdi = false;
      this.selectUdiDialogVisible = true;
    },

    intentImportErp(val) {
      this.uuid = val.uuid;
      this.relId = val.id;
      this.isImport = true;
      this.thisData = val;
      this.selectErpDialogVisible = true;
    },
    uploadSMP() {
      var selectData = this.multipleSelection;
      selectData.forEach((obj) => {
        this.filterQuery.uuids.push(obj.uuid);
      });
      this.allUploadSMP();
    },

    allUploadSMP() {
      udiUpload(this.filterQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.$message({
              type: "success",
              message: "上传成功！",
            });
          } else {
            this.$message({
              type: "error",
              message: response.message,
            });
          }
          this.$emit("cancelDialog", true);
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "上传失败",
          });
          this.$emit("cancelDialog", true);
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      // console.log(this.multipleSelection.zczbhhzbapzbh);
      // this.currentRow = val;
    },


    handeleDetailChange(val) {
      this.detailQuery.page = val.page;
      this.getDetailList();
    },

    handleCurrentChange(val) {
      this.filterQuery.page = val.page;
      this.getList();
    },
    handleModifyClick(row) {
      this.editDialogVisible = true;
      this.editQuery = row;
      this.relevanceEdit = {
        id: row.id,
        thirdId: row.thirdId,
        isUseDy: row.isUseDy,
      };
      this.checked = row.isUseDy == 1;
      this.detailQuery = {
        entireCpmctymc: row.cpmctymc,
        manufactory: row.manufactory,
        page: 1,
        limit: 10,
      };

      this.getDetailList(this.detailQuery);
      this.getThirdSysDetail();
    },
    closeUdi(val) {
      this.selectUdiDialogVisible = false;
      this.selectErpDialogVisible = false;
      this.selectLocalVisible = false;
      if (val) {
        this.getList();
      }
    },
    getThirdSysDetail() {
      let query = {
        id: this.relevanceEdit.id,
      };

      thirdSysDetail(query)
        .then((response) => {
          this.loading = false;
          this.sysList = response.data.list || [];
        })
        .catch(() => {
          this.loading = false;
          this.sysList = [];
        });
    },
    checkSelectable(row) {
      return !row.isSelect;
    },
    uploadErpProduct() {
      updateErpProduct()
        .then((response) => {
          if (response.code == 20000) {
            this.$message({
              type: "success",
              message: response.data,
            });
            this.$emit("closeDialog", true);
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(() => {
        });
    },

    closeDialog() {
      this.selectErpDialogVisible = false;
      this.selectLocalVisible = false;
    },

    selectSysParam() {
      let query = {
        paramKey: "smp_isUploadProuct",
      };
      selectSysParamByKey(query).then((response) => {
        if (response.code == 20000) {
          if (response.data.paramValue == "1") {
            this.isUploadSmp = true;
          }
        }
      });
      let query1 = {
        paramKey: "upload_pi_file_enabled",
      };
      selectSysParamByKey(query1).then((response) => {
        if (response.code == 20000) {
          if (response.data.paramValue == "1") {
            this.isImportFile = true;
          }
        }
      });
      let query2 = {
        paramKey: "upload_pi_file_url",
      };
      selectSysParamByKey(query2).then((response) => {
        if (response.code == 20000) {
          this.uploadFileUrl = response.data.paramValue;
        }
      });
    },
  },
  mounted() {
  },
  components: {},
  created() {

    this.getList();
    this.selectSysParam();
  },
};
</script>
<style scoped>
.el-form-item--mini.el-form-item {
  margin-bottom: 9px;
}
</style>
