<template>
  <div>
    <el-card class="el-card">
      <div>
        <el-form
          :inline="true"
          :model="unionQuery"
          class="query-form"
          size="mini"
        >
          <el-row>
            <el-form-item class="query-form-item">
              <el-input
                v-model="unionQuery.coName"
                placeholder="药品名称"
              ></el-input>
            </el-form-item>
            <el-form-item class="query-form-item">
              <el-input
                v-model="unionQuery.unit"
                placeholder="生产企业"
              ></el-input>
            </el-form-item>
            <el-form-item class="query-form-item">
              <el-input
                v-model="unionQuery.authCode"
                placeholder="批准文号"
              ></el-input>
            </el-form-item>
            <el-form-item class="query-form-item">
              <el-input
                v-model="unionQuery.nameCode"
                placeholder="产品编码"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button-group>
                <el-button
                  type="primary"
                  icon="el-icon-refresh"
                  size="mini"
                  @click="onReset"
                ></el-button>
                <el-button
                  type="primary"
                  icon="search"
                  @click="getList"
                  size="mini"
                  >查询</el-button
                >
                <el-button
                  style="margin-right: 20px"
                  type="primary"
                  size="mini"
                  icon="search"
                  @click="combine"
                  >选入</el-button
                >
              </el-button-group>
            </el-form-item>
          </el-row>
        </el-form>

        <el-table
          :data="udidlList"
          style="width: 100%"
          @row-click="intentDetail"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionUdiChange"
        >
          <el-table-column
            type="selection"
            width="55"
            :selectable="checkSelectable"
          ></el-table-column>

          <el-table-column label="药品类型" prop="type" fixed></el-table-column>
          <el-table-column label="生产企业" prop="unit" fixed></el-table-column>
          <el-table-column
            label="药品通用名"
            prop="coname"
            fixed
          ></el-table-column>
          <el-table-column label="剂型" prop="form" fixed></el-table-column>
          <el-table-column
            label="制剂规格"
            prop="formspec"
            fixed
          ></el-table-column>
          <el-table-column
            label="包装规格"
            prop="packspec"
            fixed
          ></el-table-column>
          <el-table-column
            label="批准文号"
            prop="authcode"
            fixed
          ></el-table-column>
          <el-table-column label="商品名" prop="name" fixed></el-table-column>
          <!-- <el-table-column
            label="产品编码"
            prop="namecode"
            fixed
          ></el-table-column> -->
          <el-table-column
            label="包装比例"
            prop="packratio"
            fixed
          ></el-table-column>
        </el-table>
        <el-pagination
          :page-size="unionQuery.limit"
          @current-change="handlePageChange"
          layout="prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

 <script>
import { combineFilter } from "../../api/warehouse/drug";
import { getErpProduct, combineDrugProduct } from "../../api/erp/udiRelevance";
export default {
  name: "closeDialog",
  props: {
    closeDialog: {
      type: Function,
      required: true,
    },
  },

  data() {
    return {
      erpQuery: {
        key: "",
        page: 1,
        limit: 10,
      },
      unionQuery: {
        coName: "",
        unit: "",
        authCode: "",
        nameCode: "",
        page: 1,
        limit: 20,
      },
      combineQuery: {
        thirdId: "",
        keys: [],
      },

      checked: false,
      udidlList: [],
      erpList: [],
      currentRow: null,
      multipleUdiSelection: [],
    };
  },
  methods: {
    getList() {
      this.loading = true;
      combineFilter(this.unionQuery)
        .then((response) => {
          this.loading = false;
          this.udidlList = response.data.list || [];
          this.total = response.data.total || 0;
        })
        .catch(() => {
          this.loading = false;
          this.udidlList = [];
          this.total = 0;
        });
    },

    tableRowClassName({ row, rowIndex }) {
      if (row.check) return "warning-row";
      return "";
    },

    checkSelectable(row, index) {
      // if ((row.check = "True")) return true;
      return !row.check;
    },

    getErpList() {
      this.loading = true;
      getErpProduct(this.erpQuery)
        .then((response) => {
          this.loading = false;
          this.erpList = response.data || [];
        })
        .catch(() => {
          this.loading = false;
          this.erpList = [];
        });
    },
    intentBack() {
      this.$router.go(-1);
    },
    combine() {
      let selectData = this.multipleUdiSelection;
      var ids = [];
      selectData.forEach((obj, index) => {
        console.log(obj + "  " + obj.packratio);
        let drugEntity = {
          packRatio: obj.packratio,
          coName: obj.coname,
          packSpec: obj.packspec,
        };
        console.log(drugEntity.uuid);
        ids.push(obj.uuid);
      });

      this.combineQuery.keys = ids;
      if (this.checked) {
        this.combineQuery.thirdId = this.currentRow.id;
      } else {
        this.combineQuery.thirdId = "";
      }

      combineDrugProduct(this.combineQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.$emit("closeMain", true);
          } else {
            this.$emit("closeMain", false);
          }
        })
        .catch(() => {
          this.$emit("closeMain", false);
        });
    },
    handlePageChange(val) {
      this.unionQuery.page = val;
      this.getList();
    },
    intentDetail(row) {
      this.$emit("productInfo", row);
      this.closeDialog();
    },
    handleCurrentChange(val) {
      console.log(val);
      this.currentRow = val;
    },
    handleSelectionUdiChange(val) {
      console.log("----" + val);
      this.multipleUdiSelection = val;
    },
  },
};
</script>

<style type="text/scss" lang="scss">
.el-table .warning-row {
  background: #f2f2f2;
}
</style>
