<template>
  <div>
    <el-card class="el-card">
      <el-form :model="erpQuery" label-width="120px" v-if="showSearch">
        <el-row>
          <el-col :span="6">
              <el-form-item label="医保编码:">
                <el-input v-model="erpQuery.ybbm" style="width: 90%" clearable="true" placeholder="请输入医保编码"
                ></el-input>
              </el-form-item>
            </el-col>
          <el-col :span="6">
            <el-form-item label="物资编码:">
              <el-input v-model="erpQuery.code" style="width: 90%" placeholder="请输入药品编码" clearable="true"
                        @keyup.enter.native="keyupErp_submit($event)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="药品名称:">
              <el-input v-model="erpQuery.name" style="width: 90%" placeholder="请输入药品名称" clearable="true"
                        @keyup.enter.native="keyup_submit($event)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="规格/单位:">
              <el-input v-model="erpQuery.spec" style="width: 90%" placeholder="包装/制剂 规格/单位" clearable="true"
                        @keyup.enter.native="keyupErp_submit($event)"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="批准文号:">
              <el-input v-model="erpQuery.registerNo" style="width: 90%" clearable="true" placeholder="请输入批准文号"
                        @keyup.enter.native="keyup_submit($event)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="生产企业:">
              <el-input v-model="erpQuery.manufactory" style="width: 90%" clearable="true" placeholder="请输入生产企业"
                        @keyup.enter.native="keyup_submit($event)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="配送企业:">
              <el-input v-model="erpQuery.supName" style="width: 90%" clearable="true" placeholder="请输入配送企业"
                        @keyup.enter.native="keyup_submit($event)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="外部系统:">
              <el-select v-model="thirdSysFk" style="width: 90%" placeholder="请设置主系统" disabled="isImport">
                <el-option
                  v-for="item in thirdSys"
                  :key="item.value"
                  :label="item.thirdName"
                  :value="item.thirdId">
                  <span style="float: left">{{ item.thirdName }}</span>
                  <span
                    style="float: right; color: #8492a6; font-size: 13px">{{ item.thirdId }}</span>
                </el-option>
              </el-select>
            </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="searchErpList">查询</el-button>
          <el-button type="primary" icon="el-icon-bottom-left" @click="combine" :loading="combineLoading">选入</el-button>
          <el-button type="primary" icon="el-icon-bottom-left" @click="combineAll" v-if="isImport  &&  !supEnable">结果全部选入</el-button>
        </el-button-group>
      </div>

      <el-divider style="margin: 15px"></el-divider>

      <el-table :data="erpList" style="width: 100%" highlight-current-row="true" border ref="multipleTable" @row-click="handleRowClick"
                v-loading="erpLloading" @current-change="handleErpChange" @selection-change="handleSelectionChange">
        <el-table-column v-if="isImport" type="selection" width="55" :selectable="checkSelectable"></el-table-column>
        <el-table-column label width="45" v-if="!isImport">
          <template slot-scope="scope">
            <el-radio :label="scope.row.id" v-model="chenck" ><span></span></el-radio>
          </template>
        </el-table-column>
        <!--<el-table-column v-if="!isImport" type="selection" width="55"></el-table-column>-->
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="医保编码" prop="ybbm"></el-table-column>
        <el-table-column label="物资编码" prop="code"></el-table-column>
        <el-table-column label="物资名称" prop="name"></el-table-column>
        <el-table-column label="计量单位" prop="measname"></el-table-column>
        <el-table-column label="包装规格" prop="spec"></el-table-column>
        <el-table-column label="制剂规格" prop="prepnSpec"></el-table-column>
        <el-table-column label="制剂单位" prop="prepnUnit"></el-table-column>
        <el-table-column label="批准文号" prop="registerNo"></el-table-column>
        <el-table-column label="生产企业" prop="manufactory"></el-table-column>
        <el-table-column label="配送企业" prop="supName"></el-table-column>
        <el-table-column label="操作" width="60">
          <template slot-scope="scope">
            <el-button type="text" @click.native.stop="detailDialog(scope.row)">详情</el-button>
          </template>
        </el-table-column>

      </el-table>
      <pagination
        v-show="pageTotal>0"
        :total="pageTotal"
        :page.sync="erpQuery.page"
        :limit.sync="erpQuery.limit"
        @pagination="handleErpPageChange"
      ></pagination>
    </el-card>

    <el-card class="el-card" v-if="!isImport">
      <el-form label-width="120px">
        <el-row>
          <el-col :span="12" class="el-col">
            <el-form-item label="物资名称:">
              <el-input style="width: 90%" size="small" splaceholder="请输入内容" :disabled="true"
                        v-model="data.cpmctymc"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="el-col">
            <el-form-item label="产品标识:">
              <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入内容"
                        v-model="data.nameCode"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" class="el-col">
            <el-form-item label="包装规格:">
              <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入内容"
                        v-model="data.bzgg"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="el-col">
            <el-form-item label="批准文号:">
              <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入内容"
                        v-model="data.zczbhhzbapzbh"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <!--<el-col :span="12" class="el-col">-->
          <!--  <el-form-item label="注册人英文名称:">-->
          <!--    <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入内容"-->
          <!--              v-model="data.ylqxzcrbarywmc"></el-input>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->
          <!--<el-col :span="12" class="el-col">-->
          <!--  <el-form-item label="器械类别:">-->
          <!--    <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入内容"-->
          <!--              v-model="data.qxlb"></el-input>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->
        </el-row>

        <el-row>
          <!--<el-col :span="12" class="el-col">-->
          <!--  <el-form-item label="产品类别:">-->
          <!--    <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入内容"-->
          <!--              v-model="data.cplb"></el-input>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->
          <!--<el-col :span="12" class="el-col">-->
          <!--  <el-form-item label="分类编码:">-->
          <!--    <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入内容"-->
          <!--              v-model="data.flbm"></el-input>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->
        </el-row>

        <el-row>
          <el-col :span="12" class="el-col">
            <el-form-item label="商品条码:">
              <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入内容"
                        v-model="data.sptm"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="el-col">
            <el-form-item label="医保编码:">
              <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入内容"
                        v-model="data.ybbm"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" class="el-col">
            <el-form-item label="统一社会信用号:">
              <el-input style="width: 90%" :disabled="true" size="small" splaceholder="请输入内容"
                        v-model="data.tyshxydm"></el-input>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
    </el-card>

    <el-dialog
      title="药品信息详情"
      :visible.sync="thrProductsDetailVisible"
      width="60%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
      v-if="thrProductsDetailVisible"
    >
      <thrProductsDetail
        :data="thisData"
      ></thrProductsDetail>
    </el-dialog>

  </div>

</template>

<script>
import {modifyRel, addErp, addAllErp, combineUdi} from "@/api/basic/udiRelevance";
import {getInvbasdoc} from "@/api/basic/getErps";
import {getBasicThirdSys} from "@/api/basic/basicThirdSys";
import thrProductsDetail from "../../../thirdSys/drug/thrDrugDetail";

export default {
  name: "closeDialog",
  props: {
    uuid: {
      type: Object,
      required: true,
    },

    data: {
      type: Object,
      required: true,
    },

    relId: {
      type: Number,
      required: true,
    },

    isImport: {
      type: Boolean,
      required: true,
    },

    closeDialog: {
      type: Function,
      required: true,
    },
    defaultSys: {
      type: Object,
      required: true,
    },
    supEnable: {
      type: Object,
      required: true,
    },
    intentThirdId: {
      type: Object,
      required: true,
    },
    mainIds: {
      type: Array,
      required: true,
    }

  },

  data() {
    return {
      showSearch: true,
      erpQuery: {
        code: null,
        name: null,
        spec: null,
        registerNo: null,
        manufactory: null,
        thirdSys: null,
        supName: null,
        measname: null,
        page: 1,
        limit: 10,
        productType:2
      },
      thisData: null,
      thrProductsDetailVisible: false,
      combineLoading: false,
      combineQuery: {
        thirdId: "",
        relId: "",
        erpName: "",
        keys: [],
        thirdIds: [],
        thirdSys: null,
        query: null,
        isRlInv: null,
        manufactory: null,
        measname: null,
        ybbm: null,
        sptm: null,
      },

      udidlList: [],
      erpList: [],
      pageTotal: 1,
      total: 1,
      currentRow: null,
      loading: false,
      erpLloading: false,
      multipleUdiSelection: [],
      thirdSys: [],
      thirdSysFk: null,
      chenck: '',
    };
  },
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.erpQuery = {
        code: null,
        name: null,
        spec: null,
        registerNo: null,
        manufactory: null,
        thirdSys: null,
        supName: null,
        measname: null,
        page: 1,
        limit: 10,
        productType:2
      };
      this.getErpList();
    },
    tableRowClassName({row}) {
      if (row.checked) return "warning-row";
      return "";
    },

    detailDialog(row) {
      this.thisData = row;
      this.thrProductsDetailVisible = true;
    },

    // checkSelectable(row) {
    //   return !row.checked;
    // },
    handleCurrentChange(val) {
      this.unionQuery.page = val;
      this.getList();
    },
    handleErpChange(val,currentRow, oldCurrentRow) {
      if (!this.isImport){
        this.chenck = val.id
        this.currentRow = val;
      }
    },
    handleRowClick(row){
      row.isSelected = !row.isSelected;
      let isTrue = this.checkSelectable(row)
      if (isTrue){
        this.$refs.multipleTable.toggleRowSelection(row);
      }
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    handleErpPageChange(val) {
      this.erpQuery.page = val.page;
      this.getErpList();
    },

    searchErpList() {
      this.erpQuery.page = 1;
      this.getErpList();
    },

    keyup_submit(event) {

      this.erpQuery.page = 1;
      this.getErpList();
      event.target.select();
    },
    getErpList() {
      this.erplLoading = true;
      this.erpQuery.thirdSys = this.thirdSysFk;
      getInvbasdoc(this.erpQuery)
        .then((response) => {
          this.erplLoading = false;
          if (response.code == 20000) {
            this.erpList = response.data.list || [];
            this.pageTotal = response.data.total || 0;
          } else {
            this.$message.error(response.message);
            this.erpList = [];
            this.pageTotal = 0;
          }
        })
        .catch(() => {
          this.erplLoading = false;
          this.erpList = [];
          this.pageTotal = 0;
        });
    },
    intentBack() {
      this.closeDialog();
    },

    combineAll() {

      this.$confirm("此操作将在后台自动下载所有查询结果, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.combineQuery.thirdSys = this.thirdSysFk;
          this.combineQuery.query = this.erpQuery;
          addAllErp(this.combineQuery)
            .then((response) => {
              if (response.code == 20000) {
                this.$message.success(response.data);
                this.closeDialog()
              } else {
                this.$message.error(response.message);
                this.closeDialog()
              }
            })
            .catch(() => {
              this.closeDialog()
            });
        })
        .catch(() => {
        });


    },

    combine() {
      this.combineQuery.query = null;
      this.combineQuery.relId = this.relId;
      this.combineQuery.thirdSys = this.thirdSysFk;
      this.combineQuery.supEnable = this.supEnable;
      if (this.isImport) {
        this.combineQuery.thirdId = null;
        let selectData = this.multipleUdiSelection;
        let selected = '';
        selectData.forEach((row) => {

          if (row.checked) {
            selected = selected + row.code + ",";
          }
          this.combineQuery.thirdIds.push(row.code);
          this.combineQuery.manufactory = row.manufactory;
          this.combineQuery.measname = row.measname;

        });
        if (this.currentRow != null) {
          this.combineQuery.manufactory = this.currentRow.manufactory;
          this.combineQuery.measname = this.currentRow.measname;
        }


        if (selectData.length == 0) {
          this.$message.warning("未选择产品");
          return;
        }
        if (selected != '') {
          this.$confirm("物资编码：" + selected + "已被添加,是否继续添加", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              this.combineLoading = true;
              combineUdi(this.combineQuery)
                .then((response) => {
                  this.combineLoading = false;
                  if (response.code == 20000) {
                    this.closeDialog()
                    this.$message.success(response.data);
                  } else {
                    this.$message.error(response.message);
                  }
                })
                .catch(() => {
                  this.combineLoading = false;
                  this.combineQuery.thirdIds = [];
                  this.$emit("closeUdi", false);

                });
            })
            .catch(() => {
              this.combineQuery.thirdIds = [];
            });
        } else {
          combineUdi(this.combineQuery)
            .then((response) => {
              this.combineLoading = false;
              if (response.code == 20000) {
                this.closeDialog()
                this.$message.success(response.data);
              } else {
                this.closeDialog()
                this.$message.warning(response.message);
              }
            })
            .catch(() => {
              this.combineLoading = false;
              this.closeDialog()
            });
        }

      } else {
        this.combineQuery.thirdId = this.currentRow.code;
        this.combineQuery.erpName = this.currentRow.name;
        this.combineQuery.manufactory = this.currentRow.manufactory;
        this.combineQuery.measname = this.currentRow.measname;
        this.combineQuery.ybbm = this.currentRow.ybbm;
        this.combineQuery.sptm = this.currentRow.sptm;
        this.erpQuery.manufactory = this.currentRow.manufactory;
        this.erpQuery.measname = this.currentRow.measname;
        this.combineQuery.price = this.currentRow.price;


        if (this.currentRow.checked) {

          this.$confirm("物资编码：" + this.currentRow.code + "已被选入,是否继续选入", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              this.combineLoading = true;
              modifyRel(this.combineQuery)
                .then((response) => {
                  this.combineLoading = false;
                  if (response.code == 20000) {
                    this.$emit("closeUdi", true);
                  } else {
                    this.$emit("closeUdi", false);
                    this.$message.error(response.message);
                  }
                })
                .catch(() => {
                  this.combineLoading = false;
                  this.$emit("closeUdi", false);
                });
            })
        } else {
          this.combineLoading = true;
          modifyRel(this.combineQuery)
            .then((response) => {
              this.combineLoading = false;
              if (response.code == 20000) {
                this.$message.success("更新成功")
                this.$emit("closeUdi", false);
              } else {
                this.$emit("closeUdi", false);
                this.$message.error(response.message);
              }
            })
            .catch(() => {
              this.combineLoading = false;
              this.$emit("closeUdi", false);
            });
        }

      }
    },

    handleSelectionChange(val) {
      this.multipleUdiSelection = val;
    },
    getBasicThirdSys() {
      let query = {
        enabled: true,
        mainSys: false,
        filter: true,
      };
      if (this.isImport) {
        query.mainSys = true;
      }
      getBasicThirdSys(query)
        .then((response) => {
          this.thirdSys = response.data.list || [];
          this.thirdSysFk = this.thirdSys[0].thirdId;
          if (this.defaultSys != null) {
            this.thirdSysFk = this.defaultSys;
          }
          this.searchErpList();

        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });
    },
    checkSelectable(row) {
      if (this.mainIds == null) {
        return true
      } else {
        if (this.mainIds.length > 0) {
          return this.mainIds.indexOf(row.code) === -1
        } else {
          return true
        }
      }
    }
  },

  components: {
    thrProductsDetail
  },

  created() {
    this.getBasicThirdSys();
    this.erpQuery.code = this.intentThirdId;
  },
};
</script>

<style scoped>

div /deep/ .el-table .warning-row {
  background: #bebebe;
}

div /deep/ .el-table .success-row {
  background: #ffffff;
}
</style>
