<template>
  <div>
    <el-card>
      <el-form :model="filterQuery" label-width="120px" v-show="showSearch">
        <el-row>
          <el-col :span="6">
            <el-form-item label="注册企业:">
              <el-select
                v-model="filterQuery.companyIdFk"
                style="width: 90%"
                filterable
                remote
                clearable="true"
                reserve-keyword
                placeholder="请选择注册企业"
                :remote-method="findMethod"
                :loading="loading"
              >
                <el-option
                  v-for="item in fromOptions"
                  :key="item.companyName"
                  :label="item.companyName"
                  :value="item.id"
                >
                  <span style="float: left">{{ item.companyName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="产品编码:">
              <el-input v-model="filterQuery.unionCode" style="width: 90%" placeholder="请输入DI/医保编码/商品条码"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <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="6">
            <el-form-item label="规格型号:">
              <el-input v-model="filterQuery.ggxh" style="width: 90%" placeholder="请输入规格型号" clearable="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item label="注册备案号:">
              <el-input v-model="filterQuery.zczbhhzbapzbh" style="width: 90%" placeholder="请输入注册备案号"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="生产企业:">
              <el-input v-model="filterQuery.ylqxzcrbarmc" 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="search">查询</el-button>
          <el-button type="primary" icon="el-icon-bottom-right" @click="selectProduct()">选入产品</el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" key="1" style="width: 100%"
                border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="注册企业" prop="companyName" show-overflow-tooltip></el-table-column>
        <el-table-column label="企业类型" prop="companyName" show-overflow-tooltip>

          <template slot-scope="scope">
            <span>{{ companyTypeMap[scope.row.companyType] }}</span>
          </template>

        </el-table-column>
        <el-table-column label="最小销售产品标识" prop="nameCode" show-overflow-tooltip></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="ylqxzcrbarmc" show-overflow-tooltip></el-table-column>
        <el-table-column label="注册/备案号" prop="zczbhhzbapzbh" show-overflow-tooltip></el-table-column>
        <el-table-column width="120" label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click.native.stop="handleModifyClick(scope.row)">详情</el-button>
            <el-button type="text" @click.native.stop="diEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click.native.stop="deleteDialog(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        title="产品信息详情"
        :visible.sync="editDialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="60%"
        v-if="editDialogVisible"
      >
        <selectDetail
          :detailQuery="detailQuery"
          :checked="checked"
          @closeUdi="closeUdi"
        >
        </selectDetail>
      </el-dialog>

      <el-dialog
        title="选入医疗器械信息"
        :visible.sync="selectProductVisible"
        width="85%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        v-if="selectProductVisible"
      >
        <selectLocalUdi
          :closeDialog="closeDialog"
          :companyId="filterQuery.companyIdFk"
        ></selectLocalUdi>
      </el-dialog>

      <el-dialog
        title="医疗器械信息-编辑"
        :visible.sync="editDiDialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="70%"
        v-if="editDiDialogVisible"
      >
        <ProductEdit
          :detailQuery="editFormat"
          :uuid="uuid"
          @closeDialog="closeUdi"
        ></ProductEdit>

        <div style="text-align: center; margin-top: 12px">
          <el-button
            type="primary"
            size="small"
            icon="search"
            @click="onAddSubmit(false)"
          >提交
          </el-button>
          <el-button
            type="primary"
            size="small"
            icon="search"
            @click="cancelDialog"
          >取消
          </el-button>
        </div>
      </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 selectLocalUdi from "./UdiInfoSelectLocalUdi";
import selectDetail from "./udilnfoManageSpDetail";
import {filterRlCompany, seachCompany, deleteRel, updateRel} from "../../../../api/basic/udiRlCompany";
import ProductEdit from "@/views/basic/product/rel/productEdit";

export default {
  data() {
    return {
      showSearch: true,
      filterQuery: {
        unionCode: null,
        udiCode: null,
        ylqxzcrbarmc: "",
        cpmctymc: "",
        nameCode: "",
        ggxh: null,
        page: 1,
        limit: 20,
        addType: 1,
        thrPiId: null,
        filterType: null,
        companyName: null,
        companyIdFk: null,
      },
      fromOptions: [],
      editQuery: null,
      detailQuery: null,
      defaultSys: null,
      isImport: false,
      isImportUdi: false,
      isUploadSmp: false,
      isSpCombine: false,
      isImportFile: false,
      uploadFileUrl: null,
      checked: false,
      list: [],
      detailList: [],
      sysList: [],
      filterList: [],
      total: 0,
      thirdNo: "",
      uuid: "111",
      originUuid: null,
      thirdId: null,
      relId: "",
      thisData: {
        nameCode: null,
        cpmctymc: null,
        ggxh: null
      },
      selectProductVisible: false,
      multipleSelection: [],
      editDialogVisible: false,       //详情
      selectUdiDialogVisible: false,  //关联选入
      selectErpDialogVisible: false,  //选入ERP产品
      selectLocalVisible: false,      //关联UDI
      selectVersionVisible: false,
      isUseDyCheck: false,
      loading: false,
      activeNames: ['1'],
      editSingleDiDialogVisible: false,
      companyTypeMap: {
        1: "生产企业",
        2: "代理商",
        3: "购买方",
      },
      editDiDialogVisible: false,
      editFormat: {},

    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        lockStatus: null,
        unionCode: null,
        udiCode: null,
        ylqxzcrbarmc: "",
        cpmctymc: "",
        thrPiId: null,
        nameCode: "",
        ggxh: null,
        page: 1,
        limit: 20,
        addType: 1,
      };
      this.total = 0;
      this.list = [];
      this.getList();
    },
    search() {
      this.filterQuery.page = 1;
      this.getList();
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    getList() {
      this.loading = true;
      filterRlCompany(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;
        });
    },
    keyup_submit(event) {
      this.filterQuery.page = 1;
      this.getList();
      event.target.select();
    },
    handleDetail(row) {
      this.editQuery = row;
    },
    deleteOrders(data) {
      this.loading = true;
      let tquery = {
        id: data.id + "",
      };

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

    deleteDialog(rowId) {
      this.$confirm("此操作将永久删除该产品信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteOrders(rowId);
        })
        .catch(() => {
        });
    },
    selectProduct() {
      if (this.$isBlank(this.filterQuery.companyIdFk)) {
        this.$message.error("请先选择注册企业！");
        return;
      }
      this.selectProductVisible = true;
    },

    cancelDialog() {
      this.editDiDialogVisible = false;
      this.editDialogVisible = false;
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val.page;
      this.getList();
    },
    handleModifyClick(row) {
      this.uuid = row.uuid;
      this.isImport = false;
      this.thisData = row;
      this.editDialogVisible = true;
      this.detailQuery = row;
      this.checked = row.isUseDy == 1;
    },

    closeUdi(val) {
      this.selectUdiDialogVisible = false;
      this.selectErpDialogVisible = false;
      this.selectLocalVisible = false;
      this.editDiDialogVisible = false;
      this.getList();
    },

    selectVersion() {
      this.selectVersionVisible = true;
      this.uuid = this.editQuery.uuid;
      this.originUuid = this.editQuery.originUuid;
    },
    closeDialog() {
      this.selectErpDialogVisible = false;
      this.selectLocalVisible = false;
      this.selectVersionVisible = false;
      this.selectProductVisible = false;
      this.getList();
    },


    findMethod(query) {
      this.fromOptions = [];
      let cQuery = {
        searchKey: query,
        page: 1,
        limit: 10,
      };
      seachCompany(cQuery)
        .then((response) => {
          this.loading = false;
          this.fromOptions = response.data || [];
        })
        .catch(() => {
          this.loading = false;
          this.fromOptions = [];
        });
    },

    diEdit(row) {
      this.uuid = row.uuid;
      this.editFormat = JSON.parse(JSON.stringify(row));
      this.editDiDialogVisible = true;
    },

    onAddSubmit(relSubmit) {
      this.editFormat.relSubmit = relSubmit;
      updateRel(this.editFormat)
        .then((response) => {
          if (response.code == 20000) {
            this.getList();
            this.cancelDialog();
            this.editDiDialogVisible = false;
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(() => {
          this.cancelDialog();
        });
    },
  }
  ,
  mounted() {
  }
  ,
  components: {
    selectLocalUdi,
    selectDetail, ProductEdit

  }
  ,
  created() {
    this.getList();
    this.findMethod();
  }
  ,
}
;
</script>
<style scoped>


.el-table .cell.el-tooltip {
  white-space: pre-wrap;
}


.itemTag {
  float: left;
  text-align: left;
  margin-top: 10px;
  width: 25%;
}

.text {
  font-size: 13px;
  font-family: "Microsoft YaHei";
}

.query-form-item {
  display: block !important;
  margin-right: 10px;
  margin-bottom: 5px;
}

.el-row {
  display: flex;
  flex-wrap: wrap;
}

.el-col {
  border-radius: 4px;
  flex-wrap: wrap;
}

.form-title {
  font-size: 16px;
  font-family: Noto Sans SC;
  font-weight: bold;
  color: #303133;
  padding-bottom: 17px;
  padding-top: 17px;
}
</style>
