<template>
  <div>
    <el-form
      :model="tempInfo"
      ref="planForm"
      label-width="237px"
      class="grid-form-class"
    >
      <div class="common-card">
        <h3>计划参数</h3>

        <div class="form-body">
          <div class="flex-css">
            <el-form-item
              label="计划类型"
              prop="planType"
              :rules="rules.selectRequired"
            >
              <el-select
                v-model="tempInfo.planType"
                placeholder="请选择计划类型"
                clearable
                @change="handleChange"
              >
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="计划周期"
              prop="planPrd"
              :rules="
                tempInfo.planType == '年度'
                  ? rules.dateRequired
                  : tempInfo.planType == '月度' || tempInfo.planType == '季度'
                  ? rules.selectRequired
                  : rules.noRequire
              "
            >
              <el-select
                v-model="tempInfo.planPrd"
                placeholder="请选择计划周期"
                v-if="tempInfo.planType == '月度'"
                clearable
              >
                <el-option
                  v-for="item in cycleList.mouth"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
              <el-select
                v-model="tempInfo.planPrd"
                placeholder="请选择计划周期"
                v-else-if="tempInfo.planType == '季度'"
                clearable
              >
                <el-option
                  v-for="item in cycleList.quarter"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
              <el-date-picker
                v-else-if="tempInfo.planType == '年度'"
                v-model="tempInfo.planPrd"
                type="year"
                placeholder="请选择计划周期"
                value-format="yyyy"
              >
              </el-date-picker>
              <el-input
                v-else
                disabled
                v-model="tempInfo.planPrd"
                placeholder="请选择计划周期"
              ></el-input>
            </el-form-item>
          </div>
          <div class="flex-css">
            <el-form-item
              label="订单日期"
              prop="docmkDate"
              :rules="rules.dateRequired"
            >
              <el-date-picker
                type="date"
                v-model="tempInfo.docmkDate"
                placeholder="请选择订单日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item
              label="到货时长（天）"
              prop="delvDays"
              :rules="rules.InterNum"
            >
              <el-input
                v-model="tempInfo.delvDays"
                placeholder="请输入到货时长（天）"
              />
            </el-form-item>
          </div>
        </div>
      </div>

      <!--列表-->
      <div class="common-card">
        <h3>药品明细</h3>
        <div class="right-type">
          <el-button
            type="primary"
            icon="el-icon-plus"
            :loading="loadGoodsLoading"
            @click="loadGoodsPlan()"
            >加载自动补货计划
          </el-button>
          <el-button type="warning" icon="el-icon-plus" @click="handleAddTemp()"
            >加载模板
          </el-button>
          <el-button
            class="lake-Blue"
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd()"
            >添加药品
          </el-button>
        </div>
        <el-table
          v-loading="listLoading"
          element-loading-text="加载中"
          :data="tempInfo.purcPlanDetlDReqDTOList"
          fit
          stripe
          border
        >
          <template slot="empty">
            <img src="@/assets/nopage.png" alt />
            <p>暂无数据</p>
          </template>
          <el-table-column
            fixed
            type="index"
            label="序号"
            width="60"
          ></el-table-column>
          <el-table-column label="药品编码" prop="drugListId" width="150" />
          <el-table-column label="药品名称" min-width="300">
            <template slot-scope="scope">
              <div class="drug-name">
                <el-tag v-if="scope.row.drugName">
                  {{ scope.row.drugName }}
                </el-tag>
                {{ "【" + scope.row.dosformName + "】" }}
                <el-tag v-if="scope.row.essdrugType">
                  {{
                    scope.row.essdrugType === "0"
                      ? "非基药"
                      : scope.row.essdrugType === "1"
                      ? "基药"
                      : scope.row.essdrugType
                  }}
                </el-tag>
                <br />
                {{ "生产厂家 【" + scope.row.prodentpName + "】" }}
                <br />
                {{
                  "药品规格 【" +
                  scope.row.specName +
                  "*" +
                  scope.row.convrat +
                  scope.row.prepunt +
                  "/" +
                  scope.row.pacUnt +
                  "】"
                }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="订货数量" width="150">
            <template #header>
              <span style="color: red; margin-right: 4px">*</span>
              <span>订货数量</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                label-width="0"
                :prop="'purcPlanDetlDReqDTOList.' + scope.$index + '.ordCnt'"
                :rules="rules.InterNum"
              >
                <el-input
                  v-model="scope.row.ordCnt"
                  placeholder="请输入"
                  @blur="numChange(scope.$index, scope.row)"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="供应商" width="200">
            <template #header>
              <span style="color: red; margin-right: 4px">*</span>
              <span>供应商</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                label-width="0"
                :prop="'purcPlanDetlDReqDTOList.' + scope.$index + '.splerName'"
                :rules="rules.selectRequired"
              >
                <el-button
                  type="text"
                  @click="handleClick(scope.$index, scope.row)"
                  >{{
                    scope.row.splerName ? scope.row.splerName : "请选择供应商"
                  }}</el-button
                >
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="采购价" prop="purcPric" width="150" />
          <el-table-column label="采购金额" prop="purcAmt" width="150" />
          <el-table-column label="当前库存" prop="invCnt" width="150" />
          <el-table-column label="最低库存" prop="lowInv" width="150" />
          <el-table-column label="最高库存" prop="highInv" width="150" />
          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button
                class="red"
                type="text"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <p class="price-css">
          合计数量：{{ amount.num }} <span></span>合计金额： {{ amount.price }}
        </p>
      </div>

      <div class="foot-button">
        <el-button type="info" @click="goback">返回</el-button>
        <el-button type="success" @click="save()" :loading="saveLoading"
          >保存
        </el-button>
        <el-button type="primary" @click="sunmit()" :loading="sunmitLoading"
          >提交
        </el-button>
      </div>
    </el-form>
    <!--添加药品-->
    <el-dialog
      title="选择药品"
      :visible.sync="dialogDrugFormVisible"
      width="1000px"
    >
      <!--条件-->
      <el-form :model="drugListQuery" ref="queryForm" :inline="true">
        <el-form-item label="药品信息">
          <el-input
            v-model="drugListQuery.drugInfo"
            placeholder="请输入药品名称/编码/生产企业"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getDrugData('search')"
            >查询
          </el-button>
          <el-button @click="drugReset()">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        ref="drugTable"
        v-loading="drugListLoading"
        row-key="drugListId"
        element-loading-text="加载中"
        :data="drugList"
        fit
        stripe
        border
        @selection-change="handleSelectionChange"
      >
        <template slot="empty">
          <img src="@/assets/nopage.png" alt />
          <p>暂无数据</p>
        </template>
        <el-table-column
          type="selection"
          :selectable="selected"
          :reserve-selection="true"
          width="55"
        >
        </el-table-column>
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="60"
        ></el-table-column>
        <el-table-column label="药品编码" prop="drugListId" width="200" />
        <el-table-column label="药品名称" prop="drugName" min-width="300">
          <template slot-scope="scope">
            <div class="drug-name">
              <el-tag v-if="scope.row.drugName">
                {{ scope.row.drugName }}
              </el-tag>
              {{ "【" + scope.row.dosformName + "】" }}
              <el-tag v-if="scope.row.essdrugType">
                {{
                  scope.row.essdrugType === "0"
                    ? "非基药"
                    : scope.row.essdrugType === "1"
                    ? "基药"
                    : scope.row.essdrugType
                }}
              </el-tag>
              <br />
              {{ "生产厂家 【" + scope.row.prodentpName + "】" }}
              <br />
              {{
                "药品规格 【" +
                scope.row.specName +
                "*" +
                scope.row.convrat +
                scope.row.prepunt +
                "/" +
                scope.row.pacUnt +
                "】"
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="当前库存" width="150" prop="invCnt" />
        <el-table-column label="采购价" width="150" prop="purcPric" />
      </el-table>

      <!--分页-->
      <yl-pagination
        v-show="drugTotal > 0"
        :total="drugTotal"
        :page.sync="drugListQuery.current"
        :limit.sync="drugListQuery.size"
        @pagination="getDrugData"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDrugFormVisible = false">取消</el-button>
        <el-button type="primary" @click="commitData()"> 确认</el-button>
      </div>
    </el-dialog>

    <!--加载模板-->
    <el-dialog
      title="选择模板"
      :visible.sync="dialogTempFormVisible"
      width="1000px"
    >
      <!--条件-->
      <el-form :model="tempListQuery" ref="queryForm" :inline="true">
        <el-form-item label="模板名称">
          <el-input
            v-model="tempListQuery.tmplName"
            placeholder="请输入模板名称"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="getTempData('search')"
            >查询
          </el-button>
          <el-button @click="tempReset()">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="tempListLoading"
        element-loading-text="加载中"
        :data="tempList"
        fit
        stripe
        border
      >
        <template slot="empty">
          <img src="@/assets/nopage.png" alt />
          <p>暂无数据</p>
        </template>
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="60"
        ></el-table-column>
        <el-table-column label="模板名称" prop="tmplName" />
        <el-table-column label="制作人" prop="crteUsrName" width="150" />
        <el-table-column label="制作时间" width="150" prop="crteTime" />
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="primary" plain @click="commitTempData(scope.row)"
              >选择</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <yl-pagination
        v-show="tempTotal > 0"
        :total="tempTotal"
        :page.sync="tempListQuery.current"
        :limit.sync="tempListQuery.size"
        @pagination="getTempData"
      />
    </el-dialog>

    <!--选择供应商-->
    <el-dialog
      title="供应商管理"
      :visible.sync="dialogComFormVisible"
      width="700px"
    >
      <!--条件-->
      <el-form :model="comListQuery" ref="queryForm" :inline="true">
        <el-form-item label="机构名称">
          <el-input
            v-model="comListQuery.splerName"
            placeholder="请输入机构名称"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="splerData('search')"
            >查询
          </el-button>
          <el-button @click="comReset()">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="comListLoading"
        element-loading-text="加载中"
        :data="comList"
        fit
        stripe
        border
      >
        <template slot="empty">
          <img src="@/assets/nopage.png" alt />
          <p>暂无数据</p>
        </template>
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="60"
        ></el-table-column>
        <el-table-column label="机构名称" prop="splerName" />
        <el-table-column label="价格" prop="pric" width="100" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="primary" plain @click="handleRow(scope.row)"
              >选择</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <yl-pagination
        v-show="comTotal > 0"
        :total="comTotal"
        :page.sync="comListQuery.current"
        :limit.sync="comListQuery.size"
        @pagination="splerData"
      />
    </el-dialog>
  </div>
</template>

<script>
import moment from "moment";
import ylPagination from "@/components/yl-pagination";
import rules from "@/utils/rules";
import { getGrpDrugListPassed } from "@/api/drugCataManage/index";
import { countNumPrice, getTotalAmount } from "@/utils/utils";
import {
  getDetlList,
  savePlan,
  submitPlan,
  getSplmDeltList,
  getSetEdDrugSplerRlts
} from "@/api/purchasing-management/procurement-plan-management";
import {
  getTempList,
  getTempDetlList
} from "@/api/purchasing-management/plan-template-maintenance";
export default {
  components: { ylPagination },
  data() {
    return {
      loadGoodsLoading: false,
      saveLoading: false,
      sunmitLoading: false,
      amount: {
        num: 0,
        price: 0
      },
      // 选择供应商的index
      chooseComIndex: 0,
      // 药品多选
      selectData: [],
      rules,
      listLoading: false,
      // 加载模板
      tempListLoading: false,
      tempListQuery: {
        //条件查询
        current: 1,
        size: 10
      },
      tempTotal: 0, //分页
      tempList: [],
      dialogTempFormVisible: false,
      // 添加药品
      drugListLoading: false,
      drugListQuery: {
        //条件查询
        current: 1,
        size: 10,
        drugInfo: ""
      },
      drugTotal: 0, //分页
      drugList: [],
      dialogDrugFormVisible: false,
      tempInfo: {
        planType: "",
        planPrd: "",
        docmkDate: moment(new Date()).format("YYYY-MM-DD"),
        delvDays: "",
        purcPlanDetlDReqDTOList: []
      },
      typeList: [
        { value: "补点", label: "补点" },
        { value: "临时", label: "临时" },
        { value: "月度", label: "月度" },
        { value: "季度", label: "季度" },
        { value: "年度", label: "年度" }
      ],
      cycleList: {
        mouth: [
          { value: "1月", label: "1月" },
          { value: "2月", label: "2月" },
          { value: "3月", label: "3月" },
          { value: "4月", label: "4月" },
          { value: "5月", label: "5月" },
          { value: "6月", label: "6月" },
          { value: "7月", label: "7月" },
          { value: "8月", label: "8月" },
          { value: "9月", label: "9月" },
          { value: "10月", label: "10月" },
          { value: "11月", label: "11月" },
          { value: "12月", label: "12月" }
        ],
        quarter: [
          { value: "第一季度", label: "第一季度" },
          { value: "第二季度", label: "第二季度" },
          { value: "第三季度", label: "第三季度" },
          { value: "第四季度", label: "第四季度" }
        ]
      },
      // 供应商弹框
      dialogComFormVisible: false,
      comListQuery: {
        current: 1,
        size: 10,
        splerName: ""
      },
      comListLoading: false,
      comList: [],
      comTotal: 0
      // 删除的数据
      // delList: []
    };
  },
  // filters: {
  //   arrfilter: function (value) {
  //     let list = value.filter((item) => {
  //       return item.delFlg != 1;
  //     });
  //     return list;
  //   }
  // },

  created() {
    if (this.$route.query.id) {
      this.tempInfo.id = this.$route.query.id;
      this.tempInfo.planType = this.$route.query.planType;
      this.tempInfo.planPrd = this.$route.query.planPrd;
      this.tempInfo.docmkDate = this.$route.query.docmkDate;
      this.tempInfo.delvDays = this.$route.query.delvDays;
      this.getData();
    }
  },

  methods: {
    goback() {
      this.$router.go(-1);
    },

    // 复选框禁用
    selected(row) {
      // if (row.planFlg == "1") {
      //   return false;
      // } else {
      if (
        this.tempInfo.purcPlanDetlDReqDTOList.some((item) => {
          return item.drugListId === row.drugListId;
        })
      ) {
        return false;
      } else {
        return true;
      }
      // }
    },

    numChange(index, row) {
      if (row.highInv && row.invCnt && row.ordCnt > row.highInv - row.invCnt) {
        this.$message({
          message: "订货数量加当前库存不能超过最高库存",
          type: "error"
        });
        row.ordCnt = 1;
        this.numOrPriceChange(index, row);
        throw new Error("订货数量加当前库存不能超过最高库存");
      }
      this.numOrPriceChange(index, row);
    },

    numOrPriceChange(index, row) {
      this.$set(
        this.tempInfo.purcPlanDetlDReqDTOList[index],
        "purcAmt",
        getTotalAmount(row.ordCnt, row.purcPric)
      );
      this.amount = countNumPrice(this.tempInfo.purcPlanDetlDReqDTOList);
    },

    // 新增目录 多选
    handleSelectionChange(val) {
      this.selectData = val;
    },
    // 计划类型发生改变时
    handleChange(val) {
      this.tempInfo.planPrd = "";
    },

    // 加载补货计划
    loadGoodsPlan() {
      this.$confirm("加载补货计划将会清空之前的数据，是否继续操作？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loadGoodsLoading = true;
        getSplmDeltList({})
          .then((response) => {
            this.$message({
              message: "加载补货计划成功",
              type: "success"
            });
            if (response.data && response.data.length > 0) {
              this.tempInfo.purcPlanDetlDReqDTOList = response.data;
              // 计算采购金额
              this.amount = countNumPrice(
                this.tempInfo.purcPlanDetlDReqDTOList
              );
            }
            this.$refs.planForm.clearValidate();
            this.loadGoodsLoading = false;
          })
          .catch((err) => {
            this.loadGoodsLoading = false;
          });
      });
    },

    //获取列表数据
    getData() {
      this.listLoading = true;
      getDetlList({ id: this.$route.query.id })
        .then((response) => {
          this.tempInfo.purcPlanDetlDReqDTOList = response.data;
          // this.tempInfo.purcPlanDetlDReqDTOList.forEach((item) => {
          //   item.delFlg = 0;
          // });
          // 计算采购金额
          this.amount = countNumPrice(this.tempInfo.purcPlanDetlDReqDTOList);
          this.listLoading = false;
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },

    // 删除
    handleDelete(row) {
      // if (row.delFlg === 0) {
      //   this.delList.push({
      //     id: row.id,
      //     drugListId: row.drugListId,
      //     splerId: row.splerId,
      //     ordCnt: row.ordCnt,
      //     delFlg: 1
      //   });
      // }
      const index = this.tempInfo.purcPlanDetlDReqDTOList.indexOf(row);
      this.tempInfo.purcPlanDetlDReqDTOList.splice(index, 1);
      // 计算采购金额
      this.amount = countNumPrice(this.tempInfo.purcPlanDetlDReqDTOList);
      this.$refs.planForm.clearValidate();
    },

    // 添加药品
    handleAdd() {
      this.dialogDrugFormVisible = true;
      this.$nextTick(() => {
        if (this.$refs.drugTable) {
          this.$refs.drugTable.clearSelection();
          this.selectData = [];
        }
      });

      this.getDrugData();
    },

    //新增药品列表
    getDrugData(type) {
      if (type == "search") {
        this.drugListQuery.current = 1;
      }
      // this.drugListQuery.planId = this.tempInfo.id;
      this.drugListLoading = true;
      getGrpDrugListPassed(this.drugListQuery)
        .then((response) => {
          this.drugList = response.data.records;
          // this.drugList[0].invdFlag = 1;
          this.drugTotal = response.data.total;
          this.drugListLoading = false;
        })
        .catch((err) => {
          this.drugListLoading = false;
        });
    },

    // 重置药品列表
    drugReset() {
      this.drugListQuery.drugInfo = "";
      this.getDrugData();
    },

    // 加载模板
    handleAddTemp() {
      this.dialogTempFormVisible = true;
      this.getTempData();
    },

    //加载模板列表
    getTempData(type) {
      this.tempListLoading = true;
      if (type == "search") {
        this.tempListQuery.current = 1;
      }

      getTempList(this.tempListQuery)
        .then((response) => {
          this.tempList = response.data.records;
          this.tempTotal = response.data.total;
          this.tempListLoading = false;
        })
        .catch((err) => {
          this.tempListLoading = false;
        });
    },

    // 重置加载模板列表
    tempReset() {
      this.tempListQuery.tmplName = "";
      this.getTempData();
    },

    // 添加药品确认
    commitData() {
      this.dialogDrugFormVisible = false;
      this.tempInfo.purcPlanDetlDReqDTOList.push(...this.selectData);
    },

    // 加载模板确认
    commitTempData(row) {
      this.$confirm("加载模板会清空之前的数据，是否继续操作？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.listLoading = true;
        this.dialogTempFormVisible = false;
        getTempDetlList({ tmplId: row.id, size: 9999999 })
          .then((response) => {
            if (
              response.data &&
              response.data &&
              response.data.records.length > 0
            ) {
              this.$message({
                message: "加载模板成功",
                type: "success"
              });

              this.tempInfo.purcPlanDetlDReqDTOList = response.data.records;
              this.$refs.planForm.clearValidate();

              // 计算采购金额
              this.amount = countNumPrice(
                this.tempInfo.purcPlanDetlDReqDTOList
              );
            } else {
              this.$message({
                message: "模板中没有数据",
                type: "warning"
              });
            }

            this.listLoading = false;
          })
          .catch((err) => {
            this.listLoading = false;
          });
      });
    },

    // 处理保存提交数据
    handleTempInfo() {
      let zanInfo = {
        id: this.tempInfo.id,
        planType: this.tempInfo.planType,
        planPrd: this.tempInfo.planPrd,
        delvDays: this.tempInfo.delvDays,
        docmkDate: this.tempInfo.docmkDate,
        purcPlanDetlDReqDTOList: []
      };
      this.tempInfo.purcPlanDetlDReqDTOList.forEach((item) => {
        zanInfo.purcPlanDetlDReqDTOList.push({
          id: item.id,
          drugListId: item.drugListId,
          splerId: item.splerId,
          ordCnt: item.ordCnt
          // delFlg: item.delFlg
        });
      });
      // zanInfo.purcPlanDetlDReqDTOList.push(...this.delList);
      return zanInfo;
    },

    // 保存
    save() {
      if (this.tempInfo.purcPlanDetlDReqDTOList.length == 0) {
        this.$message({
          message: "请至少选择一条数据！",
          type: "warning"
        });
        return false;
      }
      this.$refs["planForm"].validate((valid) => {
        if (valid) {
          this.$confirm("确认保存", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(() => {
              this.saveLoading = true;
              let zanInfo = this.handleTempInfo();
              savePlan(zanInfo)
                .then((response) => {
                  this.$message({
                    message: "保存成功",
                    type: "success"
                  });
                  if (response && response.data && response.data.id) {
                    this.tempInfo.id = response.data.id;
                  }

                  this.saveLoading = false;
                  this.$router.push({
                    path: "/purchasing-management/procurement-plan-management"
                  });
                })
                .catch((err) => {
                  this.saveLoading = false;
                });
            })
            .catch(() => {
              this.saveLoading = false;
            });
        }
      });
    },

    // 提交
    sunmit() {
      if (this.tempInfo.purcPlanDetlDReqDTOList.length == 0) {
        this.$message({
          message: "请至少选择一条数据！",
          type: "warning"
        });
        return false;
      }
      this.$refs["planForm"].validate((valid) => {
        if (valid) {
          this.$confirm("确认提交", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(() => {
              this.sunmitLoading = true;
              let zanInfo = this.handleTempInfo();
              submitPlan(zanInfo)
                .then((response) => {
                  this.$message({
                    message: "提交成功",
                    type: "success"
                  });
                  this.sunmitLoading = false;
                  this.$router.push({
                    path: "/purchasing-management/procurement-plan-management"
                  });
                })
                .catch((err) => {
                  this.sunmitLoading = false;
                });
            })
            .catch(() => {
              this.sunmitLoading = false;
            });
        }
      });
    },

    // 选择供应商弹框
    handleClick(index, row) {
      this.chooseComIndex = index;
      this.comListQuery.drugListId = row.drugListId;
      this.dialogComFormVisible = true;
      this.splerData();
    },

    handleRow(row) {
      this.tempInfo.purcPlanDetlDReqDTOList[this.chooseComIndex].splerName =
        row.splerName;
      this.tempInfo.purcPlanDetlDReqDTOList[this.chooseComIndex].splerId =
        row.splerId;
      this.tempInfo.purcPlanDetlDReqDTOList[this.chooseComIndex].purcPric =
        row.pric;
      this.dialogComFormVisible = false;
      if (
        this.tempInfo.purcPlanDetlDReqDTOList[this.chooseComIndex].splerName
      ) {
        this.$refs.planForm.clearValidate(
          "purcPlanDetlDReqDTOList." + this.chooseComIndex + ".splerName"
        );
      }
      // 计算采购金额
      this.numOrPriceChange(
        this.chooseComIndex,
        this.tempInfo.purcPlanDetlDReqDTOList[this.chooseComIndex]
      );
    },

    // 获取供应商列表
    splerData(type) {
      this.comListLoading = true;
      if (type == "search") {
        this.comListQuery.current = 1;
      }
      getSetEdDrugSplerRlts(this.comListQuery)
        .then((response) => {
          this.comList = response.data.records;
          // this.comList = [];
          // if (response.data.records.length > 0) {
          //   response.data.records.forEach((item) => {
          //     if (item.choFlg != 1) {
          //       this.comList.push(item);
          //     }
          //   });
          // }
          this.comTotal = response.data.total;
          this.comListLoading = false;
        })
        .catch((err) => {
          this.comListLoading = false;
        });
    },
    // 重置供应商列表
    comReset() {
      this.comListQuery.splerName = "";
      this.splerData();
    }
  }
};
</script>

<style scoped lang="scss">
</style>