<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <div class="query-box">
        <div class="query-box__left">
          <el-form-item label="" prop="code">
            <el-input
              v-model="queryParams.code"
              placeholder="请输入调价单号"
              clearable
              size="small"
              style="width:160px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="" prop="state">
            <el-select
              style="width:160px"
              v-model="queryParams.state"
              placeholder="请选择单据状态"
              clearable
              size="small"
            >
              <el-option
                v-for="item in billOptions"
                :key="item.dictValue"
                :value="item.dictValue"
                :label="item.dictLabel"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="" prop="keepingTime">
            <el-date-picker
              style="width:160px"
              clearable
              size="small"
              v-model="queryParams.keepingTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择调价时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
            <!-- <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['inventory:modifyPrice:add']"
              >新增</el-button
            > -->
          </el-form-item>
        </div>
      </div>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['inventory:modifyPrice:add']"
          >新增</el-button
        >
      </el-col>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="modifyPriceList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column label="调价单号" align="center" prop="code" />
      <el-table-column label="调价方式" align="center" prop="modifyMode">
        <template slot-scope="scope">
          <dict-tag :options="modifyModeArr" :value="scope.row.modifyMode" />
        </template>
      </el-table-column>
      <el-table-column label="调价文号" align="center" prop="modifyNumber" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="billOptions" :value="scope.row.state"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="调价标志" align="center" prop="state">
        <template slot-scope="scope">
          {{ scope.row.type == 1 ? "本地调价" : "统一调价" }}
        </template>
      </el-table-column>
      <el-table-column label="调价类型" align="center" prop="modifyType">
        <template slot-scope="scope">
          <dict-tag :options="modifyTypeArr" :value="scope.row.modifyType" />
        </template>
      </el-table-column>
      <el-table-column label="制单人" align="center" prop="createBy" />
      <el-table-column label="制单时间" align="center" prop="createTime" />
      <el-table-column label="记账人" align="center" prop="keepingBy" />
      <el-table-column
        label="记账时间"
        align="center"
        prop="keepingTime"
        width="180"
      >
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.keepingTime, "{y}-{m}-{d}") }}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="250"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            style="width: 40px"
            @click="handleUpdate(1, scope.row)"
            v-hasPermi="['inventory:modifyPrice:edit']"
            v-if="scope.row.state === '1'"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="primary"
            style="width: 40px"
            v-hasPermi="['inventory:modifyPrice:edit']"
            @click="handleUpdate(3, scope.row)"
            >详情</el-button
          >
          <!-- scope.row.modifyType !== '2'  -->
          <el-button
            size="mini"
            type="primary"
            style="width: 40px"
            v-hasPermi="['inventory:modifyPrice:edit']"
            @click="handleUpdate(2, scope.row)"
            v-if="scope.row.state === '1'"
            >记账</el-button
          >
          <el-button
            size="mini"
            type="primary"
            style="width: 40px"
            @click="handleDelete(scope.row)"
            v-hasPermi="['inventory:modifyPrice:remove']"
            v-if="scope.row.state === '1'"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改调价管理对话框 -->
    <el-dialog
      :fullscreen="true"
      :title="title"
      :visible.sync="open"
      width="1180px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-position="top">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="调价单号" prop="code">
              <el-input v-model="form.code" placeholder="请输入单号" disabled />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="调价方式" prop="modifyMode">
              <el-select
                v-model="form.modifyMode"
                placeholder="请选择调价方式"
                :disabled="disableDetails"
              >
                <el-option
                  v-for="item in modifyModeArr"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="调价文号" prop="modifyNumber">
              <el-input
                v-model="form.modifyNumber"
                placeholder="请输入调价文号"
                :disabled="disableDetails"
                style="100%"
              />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="调价类型" prop="modifyType">
              <el-select
                v-model="form.modifyType"
                placeholder="请选择调价类型"
                :disabled="disableDetails"
              >
                <el-option
                  v-for="item in modifyTypeArr"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="制单人" prop="createBy">
              <el-input
                v-model="form.createBy"
                placeholder="请输入制单人"
                disabled
                style="100%"
              />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="制单时间" prop="createTime">
              <!-- <el-date-picker
                disabled
                clearable
                size="small"
                v-model="form.createTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择制单时间"
                style="100%"
              >
              </el-date-picker> -->
              <el-input
                v-model="form.createTime"
                placeholder="请选择制单时间"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="记账人" prop="keepingBy">
              <el-input
                v-model="form.keepingBy"
                placeholder="请输入记账人"
                disabled
                style="100%"
              />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="记账时间" prop="keepingTime">
              <!-- <el-date-picker
                disabled
                clearable
                size="small"
                v-model="form.keepingTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择记账时间"
                style="100%"
              >
              </el-date-picker> -->
              <el-input
                placeholder="选择记账时间"
                v-model="form.keepingTime"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="备注" prop="remarks">
              <el-input
                v-model="form.remarks"
                placeholder="请输入备注"
                :disabled="disableDetails"
                style="100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-divider content-position="center">调价管理详情信息</el-divider>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAddModifyPriceDetails"
            v-if="!disableDetails"
            >添加</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="handleDeleteModifyPriceDetails"
            v-if="!disableDetails"
            >删除</el-button
          >
        </el-col>
      </el-row>
      <el-table
        :data="modifyPriceDetails"
        :row-class-name="rowWarehouseDetailIndex"
        @selection-change="handleWarehouseDetailSelectionChange"
        ref="warehouseDetail"
        border
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column type="selection" width="80" align="center" />
        <el-table-column label="序号" align="center" prop="index" width="65" />
        <el-table-column
          label="药品名称"
          prop="drugsName"
          width="110"
          align="center"
        >
          <template slot-scope="scope">
            <!-- <el-input
                v-model="scope.row.drugsId"
                placeholder="请输入药品名称"
              /> -->
            <!-- <el-select
              v-model="scope.row.drugsId"
              placeholder="请选择药品名称"
              @change="handleDrugsId($event, scope)"
              :disabled="disableDetails"
            >
              <el-option
                v-for="item in drugsArr"
                :key="item.drugsId"
                :value="item.drugsId"
                :label="item.drugsName"
                :disabled="item.state == 1 ? false : true"
              />
            </el-select> -->
            <selectOutTable type="deal" :info="scope.row" :pharmacyId="form.pharmacyId" :index="scope.$index" :disableDetails="disableDetails" @selectChange="selectChange"></selectOutTable>
          </template>
        </el-table-column>
        <el-table-column
          label="药品类型"
          prop="drugsType"
          width="110"
          align="center"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.drugsType">{{drugsTypeOptions.find(v=>v.dictValue==scope.row.drugsType).dictLabel}}</span>
            <!-- <el-input
                disabled
                v-model="scope.row.drugsType"
                placeholder="药品类型"
              /> -->
            <!-- <el-select
              v-model="scope.row.drugsType"
              placeholder="药品类型"
              disabled
            >
              <el-option
                v-for="item in drugsTypeOptions"
                :key="item.dictValue"
                :value="item.dictValue"
                :label="item.dictLabel"
              ></el-option>
            </el-select> -->
          </template>
        </el-table-column>
        <el-table-column
          label="药品规格"
          prop="specsName"
          width="110"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{scope.row.specsName}}</span>
            <!-- <el-input v-model="scope.row.specsId" placeholder="请输入规格" /> -->
           <!--  <el-select
              v-model="scope.row.specsId"
              placeholder="请选择药品规格"
              @change="handleSpecsId($event, scope)"
              :disabled="disableDetails"
            >
              <el-option
                v-for="item in scope.row.specsArr"
                :key="item.specsId"
                :value="item.specsId"
                :label="item.drugsSpecsName"
              />
            </el-select> -->
          </template>
        </el-table-column>
        <el-table-column
          label="药品产地"
          prop="priceName"
          width="110"
          align="center"
        >
          <template slot-scope="scope">
             <span>{{scope.row.placeName}}</span>
            <!-- <el-input
                v-model="scope.row.priceId"
                placeholder="请输入药品产地"
              /> -->
            <!-- <el-select
              v-model="scope.row.priceId"
              placeholder="请选择药品产地"
              @change="handlePriceId($event, scope)"
              :disabled="disableDetails"
            >
              <el-option
                v-for="item in scope.row.priceArr"
                :key="item.priceId"
                :value="item.priceId"
                :label="item.placeName"
                :disabled="item.state == 1 ? false : true"
              />
            </el-select> -->
          </template>
        </el-table-column>

        <el-table-column
          label="药品单位"
          prop="priceName"
          width="110"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{scope.row.packingUnit}}</span>
            <!-- <el-input
                v-model="scope.row.packingUnit"
                placeholder="请输入药品单位"
                disabled
              /> -->
            <!-- <el-select
              v-model="scope.row.packingUnit"
              placeholder="请输入单位"
              disabled
            >
              <el-option
                v-for="item in unitOptions"
                :key="item.dictValue"
                :value="item.dictValue"
                :label="item.dictLabel"
                :disabled="item.state == 1 ? false : true"
              ></el-option>
            </el-select> -->
          </template>
        </el-table-column>

        <el-table-column
          label="调价数量"
          prop="stock"
          align="center"
          width="110"
        >
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.stock"
              placeholder="调价数量"
              disabled
            />
          </template>
        </el-table-column>
        <el-table-column
          label="加价率"
          prop="markupRate"
          align="center"
          width="110"
        >
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.markupRate"
              placeholder="加价率"
              disabled
            />
          </template>
        </el-table-column>
        <el-table-column
          label="原进货单价"
          prop="oldPurchasePrice"
          align="center"
          width="110"
        >
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.oldPurchasePrice"
              placeholder="原进货单价"
              disabled
            />
          </template>
        </el-table-column>
        <el-table-column
          label="现进货单价"
          prop="purchasePrice"
          align="center"
          width="110"
        >
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.purchasePrice"
              placeholder="现进货单价"
              :disabled="disableDetails"
            />
          </template>
        </el-table-column>

        <el-table-column
          label="原进货总价"
          prop="purchasePriceAll"
          align="center"
          width="110"
        >
          <template slot-scope="scope">
            <!-- <el-input
                :v-model="scope.row.purchasePrice*scope.row.inventory"
                placeholder="请输入进货总价"
              /> -->
            <div>
              {{
                scope.row.stock
                  ? scope.row.stock * scope.row.oldPurchasePrice
                  : 0
              }}
            </div>
          </template>
        </el-table-column>

        <el-table-column
          label="现进货总价"
          prop="purchasePriceAll"
          align="center"
          width="110"
        >
          <template slot-scope="scope">
            <!-- <el-input
                :v-model="scope.row.purchasePrice*scope.row.inventory"
                placeholder="请输入进货总价"
              /> -->
            <div>{{ scope.row.stock * scope.row.purchasePrice }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="进货升降总价"
          prop="purchasePriceAll"
          align="center"
          width="110"
        >
          <template slot-scope="scope">
            <!-- <el-input
                :v-model="scope.row.purchasePrice*scope.row.inventory"
                placeholder="请输入进货总价"
              /> -->
            <div>
              {{
                scope.row.stock
                  ? (scope.row.stock * scope.row.purchasePrice -
                    scope.row.stock * scope.row.oldPurchasePrice).toFixed(4)
                  : 0
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="原零售单价"
          prop="oldRetailPrice"
          align="center"
          width="110"
        >
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.oldRetailPrice"
              placeholder="原零售单价"
              disabled
            />
          </template>
        </el-table-column>
        <el-table-column
          label="现零售单价"
          prop="retailPrice"
          align="center"
          width="110"
        >
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.retailPrice"
              placeholder="现零售单价"
              :disabled="disableDetails"
            />
          </template>
        </el-table-column>

        <el-table-column
          label="原零售总价"
          prop="purchasePriceAll"
          align="center"
          width="110"
        >
          <template slot-scope="scope">
            <!-- <el-input
                :v-model="scope.row.purchasePrice*scope.row.inventory"
                placeholder="请输入进货总价"
              /> -->
            <div>
              {{
                scope.row.stock ? (scope.row.stock * scope.row.oldRetailPrice).toFixed(4) : 0
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="现零售总价"
          prop="purchasePriceAll"
          align="center"
          width="110"
        >
          <template slot-scope="scope">
            <!-- <el-input
                :v-model="scope.row.purchasePrice*scope.row.inventory"
                placeholder="请输入进货总价"
              /> -->
            <div>{{ scope.row.stock * scope.row.retailPrice }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="零售升降总价"
          prop="purchasePriceAll"
          align="center"
          width="110"
        >
          <template slot-scope="scope">
            <div>
              {{
                scope.row.stock
                  ? (scope.row.stock * scope.row.retailPrice -
                    scope.row.stock * scope.row.oldRetailPrice).toFixed(4)
                  : 0
              }}
            </div>
          </template>
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-if="BtnState === 1"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>

          <el-button @click="handleUpdataKeeping" v-if="BtnState === 2"
            >记 账</el-button
          >
          <el-button @click="handleOff" v-if="BtnState === 4">冲销</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listModifyPrice,
  getModifyPrice,
  delModifyPrice,
  addModifyPrice,
  updateModifyPrice,
  exportModifyPrice,
  sumStock,
  updataKeeping,
} from "@/api/inventory/modifyPrice";

import { listDrugs } from "@/api/drugs/drugs";
import { listSpecs } from "@/api/drugs/specs";
import { listPrice } from "@/api/drugs/price";
import { oneList, getOneGroup } from "@/api/inventory/inventory";

import selectOutTable from '@/components/selectTable/selectOutTable.vue'
export default {
  components: {
    selectOutTable
  },
  name: "ModifyPrice",
  data() {
    return {
      disableDetails: false,
      billOptions: [
        { dictLabel: "制单", dictValue: "1" },
        { dictLabel: "记账", dictValue: "2" },
      ],
      BtnState: null,
      tableId: null,
      drugsTypeOptions: [],
      unitOptions: [],
      // 子表选中数据
      checkedWarehouseDetail: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 调价管理表格数据
      modifyPriceList: [],
      //添加详情
      modifyPriceDetails: [
        {
          drugsId: "",
          specsId: "",
          riceId: "",
          stock: "",
          inventory: "",
          purchasePrice: "",
          retailPrice: "",
          // approvalCode: "",
          site: "",
          // productionCode: "",
          validTime: "",
          specsArr: [],
          priceArr: [],
        },
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modifyType: null,
        modifyNumber: null,
        modifyMode: null,
        code: null,
        pharmacyId: window.localStorage.getItem("pharmacyId"),
        remarks: null,
        keepingBy: null,
        keepingTime: null,
        state: null,
        del: null,
      },
      // 表单参数
      form: {
        keepingBy: null,
        createBy: window.localStorage.getItem("userName"),
        pharmacyId: window.localStorage.getItem("pharmacyId"),
        modifyType: "1",
      },
      modifyTypeArr: [
        { name: "手动调价", id: "1" },
        { name: "自动调价", id: "2" },
      ],
      modifyModeArr: [
        { name: "正常调价", id: "1" },
        { name: "政策调价", id: "2" },
        { name: "其他", id: "3" },
      ],
      // 表单校验
      rules: {
        pharmacyId: [
          { required: true, message: "药房id不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getDicts("bill_state").then((response) => {
      this.billOptions = response.data;
    });

    this.getDicts("drugs_type").then((response) => {
      this.drugsTypeOptions = response.data;
    });
    this.getDicts("unit").then((response) => {
      this.unitOptions = response.data;
    });
    this.getList();
    //this.getListDrugs();
  },
  methods: {
    selectChange (data) {
      var index = data.index
      var r = data.data
      this.modifyPriceDetails[index].drugsType = String(r.drugsType);
      this.modifyPriceDetails[index].specsId = r.specsId;
      this.modifyPriceDetails[index].priceId = r.priceId;
      this.modifyPriceDetails[index].placeName = r.placeName;
      this.modifyPriceDetails[index].specsName = r.drugsSpecsName;
      this.modifyPriceDetails[index].retailPrice = r.retailPrice;
      this.modifyPriceDetails[index].approvalCode = r.approvalCode;
      this.modifyPriceDetails[index].packingUnit = r.packingUnit;
      this.modifyPriceDetails[index].oldRetailPrice = r.oldRetailPrice;
      this.modifyPriceDetails[index].stock = r.stock;
      this.$set(this.modifyPriceDetails[index], "specsArr", data.result);
      this.modifyPriceDetails[index].productionCode = r.productionCode;
      this.modifyPriceDetails[index].purchasePrice = r.purchasePrice;
      this.modifyPriceDetails[index].warehouseNum = r.stock;
    },
    /** 表单合计 **/
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "总价";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (
          index === 7 ||
          index === 8 ||
          index === 9 ||
          index === 10 ||
          index === 14 ||
          index === 15
        ) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = (sum[index]-0).toFixed(4);
        } else {
          sums[index] = "/";
        }

        if (index === 11) {
          let num = [];
          data.map((item) => {
            num.push(item.stock ? item.stock * item.oldPurchasePrice : 0);
          });
          let sum = num.reduce(function (prev, cur, index, array) {
            return prev + cur;
          });
          sums[11] = (sum-0).toFixed(4);
        }

        if (index === 12) {
          let num = [];
          data.map((item) => {
            num.push(item.stock * item.purchasePrice);
          });
          let sum = num.reduce(function (prev, cur, index, array) {
            return prev + cur;
          });
          sums[12] = (sum-0).toFixed(4);
        }

        if (index === 13) {
          let num = [];
          data.map((item) => {
            num.push(
              item.stock
                ? item.stock * item.purchasePrice -
                    item.stock * item.oldPurchasePrice
                : 0
            );
          });
          let sum = num.reduce(function (prev, cur, index, array) {
            return prev + cur;
          });
          sums[13] = (sum-0).toFixed(4);
        }

        if (index === 16) {
          let num = [];
          data.map((item) => {
            num.push(item.stock ? item.stock * item.oldRetailPrice : 0);
          });
          let sum = num.reduce(function (prev, cur, index, array) {
            return prev + cur;
          });
          sums[16] = (sum-0).toFixed(4);
        }

        if (index === 17) {
          let num = [];
          data.map((item) => {
            num.push(item.stock * item.retailPrice);
          });
          let sum = num.reduce(function (prev, cur, index, array) {
            return prev + cur;
          });
          sums[17] = (sum-0).toFixed(4);
        }

        if (index === 18) {
          let num = [];
          data.map((item) => {
            num.push(
              item.stock
                ? item.stock * item.retailPrice -
                    item.stock * item.oldRetailPrice
                : 0
            );
          });
          let sum = num.reduce(function (prev, cur, index, array) {
            return prev + cur;
          });
          sums[18] = (sum-0).toFixed(4);
        }
      });

      return sums;
    },
    /** 入库单详情添加按钮操作 */
    handleAddModifyPriceDetails() {
      let obj = {};
      obj.drugsId = "";
      obj.specsId = "";
      obj.priceId = "";
      obj.stock = "";
      obj.inventory = "";
      obj.purchasePrice = "";
      obj.retailPrice = "";
      obj.approvalCode = "";
      obj.site = "";
      obj.productionCode = "";
      obj.validTime = "";
      this.modifyPriceDetails.push(obj);
    },
    //获取药品名称
    getListDrugs() {
      getOneGroup({
        pharmacyId: window.localStorage.getItem("pharmacyId"),
      }).then((res) => {
        this.drugsArr = res.data;
      });
    },
    //通过药品名称获取药品类型
    /*handleDrugsId(e, row) {
      let newArr = this.drugsArr.filter((item) => {
        return item.drugsId === e;
      });
      this.drugsId = e;
      this.modifyPriceDetails[row.$index].drugsType = String(
        newArr[0].drugsType
      );
      this.getListSpecs(e, row.$index);

      this.modifyPriceDetails[row.$index].specsId = "";
      this.modifyPriceDetails[row.$index].priceId = "";
      this.modifyPriceDetails[row.$index].purchasePrice = "";
      this.modifyPriceDetails[row.$index].retailPrice = "";
      this.modifyPriceDetails[row.$index].approvalCode = "";
      this.modifyPriceDetails[row.$index].packingUnit = "";
    },
    //获取药品规格
    getListSpecs(drugsId, index) {
      let pharmacyId = window.localStorage.getItem("pharmacyId");
      oneList({ pharmacyId, drugsId }).then((res) => {
        // this.specsArr = res.rows;
        // this.modifyPriceDetails[row.$index].specsArr = res.rows
        this.$set(this.modifyPriceDetails[index], "specsArr", res.data);
      });
    },*/
    //通过药品规格获取
    handleSpecsId(e, row) {
      let newArr = this.modifyPriceDetails[row.$index].specsArr.filter(
        (item) => {
          return item.specsId === e;
        }
      );
      this.specsId = e;
      this.modifyPriceDetails[row.$index].packingUnit = String(
        newArr[0].packingUnit
      );
      this.getListPrice(e, row.$index);

      this.modifyPriceDetails[row.$index].priceId = "";
      this.modifyPriceDetails[row.$index].purchasePrice = "";
      this.modifyPriceDetails[row.$index].retailPrice = "";
      this.modifyPriceDetails[row.$index].approvalCode = "";
    },
    //获取产地
    getListPrice(specsId, index) {
      let pharmacyId = window.localStorage.getItem("pharmacyId");
      oneList({ specsId, pharmacyId, drugsId: this.drugsId }).then((res) => {
        this.$set(this.modifyPriceDetails[index], "priceArr", res.data);
      });
    },
    //通过药品产地获取
    handlePriceId(e, row) {
      let newArr = this.modifyPriceDetails[row.$index].priceArr.filter(
        (item) => {
          return item.priceId === e;
        }
      );
      this.priceId = e;
      // this.modifyPriceDetails[row.$index].purchasePrice = newArr[0].wholesale;
      // this.modifyPriceDetails[row.$index].retailPrice = newArr[0].retail;
      // this.modifyPriceDetails[row.$index].productionCode =
      //   newArr[0].approvalCode;
      if (
        window.localStorage.getItem("pharmacyId") &&
        this.drugsId &&
        this.specsId &&
        this.priceId
      ) {
        this.getSumStock(row.$index);
        this.getOneList(row.$index);
      }
    },
    //获取库存数量
    getOneList(index) {
      let params = {
        pharmacyId: window.localStorage.getItem("pharmacyId"),
        drugsId: this.drugsId,
        specsId: this.specsId,
        priceId: this.priceId,
      };
      oneList(params).then((res) => {
        // this.modifyPriceDetails[index].markupRate = res.data[0].markupRate;

        this.$set(
          this.modifyPriceDetails[index],
          "markupRate",
          res.data.length > 0 ? res.data[0].markupRate : 0
        );
        this.$set(
          this.modifyPriceDetails[index],
          "oldPurchasePrice",
          res.data.length > 0 ? res.data[0].purchasePrice : 0
        );
        this.$set(
          this.modifyPriceDetails[index],
          "oldRetailPrice",
          res.data.length > 0 ? res.data[0].retailPrice : 0
        );
      });
    },
    //获取调配数量
    getSumStock(index) {
      let params = {
        pharmacyId: window.localStorage.getItem("pharmacyId"),
        drugsId: this.drugsId,
        specsId: this.specsId,
        priceId: this.priceId,
      };
      sumStock(params).then((res) => {
        res.data
          ? this.$set(this.modifyPriceDetails[index], "stock", res.data.stock)
          : this.$set(this.modifyPriceDetails[index], "stock", 0);
      });
    },
    /** 入库单详情删除按钮操作 */
    handleDeleteModifyPriceDetails() {
      if (this.checkedWarehouseDetail.length == 0) {
        this.msgError("请先选择要删除的入库单详情数据");
      } else {
        const modifyPriceDetails = this.modifyPriceDetails;
        const checkedWarehouseDetail = this.checkedWarehouseDetail;
        this.modifyPriceDetails = modifyPriceDetails.filter(function (item) {
          return checkedWarehouseDetail.indexOf(item.index) == -1;
        });
      }
    },
    /** 入库单详情序号 */
    rowWarehouseDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 复选框选中数据 */
    handleWarehouseDetailSelectionChange(selection) {
      this.checkedWarehouseDetail = selection.map((item) => item.index);
    },
    /** 查询调价管理列表 */
    getList() {
      this.loading = true;
      listModifyPrice(this.queryParams).then((response) => {
        this.modifyPriceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        modifyType: "1",
        modifyNumber: null,
        modifyMode: null,
        code: null,
        remarks: null,
        createTime: null,
        keepingTime: null,
        state: null,
        del: null,
        keepingBy: null,
        createBy: window.localStorage.getItem("userName"),
        pharmacyId: window.localStorage.getItem("pharmacyId"),
      };
      this.modifyPriceDetails = [
        {
          drugsId: "",
          specsId: "",
          riceId: "",
          stock: "",
          inventory: "",
          purchasePrice: "",
          retailPrice: "",
          approvalCode: "",
          site: "",
          productionCode: "",
          validTime: "",
          specsArr: [],
          priceArr: [],
        },
      ];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.code = this.dayjs().format("YYYYMMDDHHmmssSSS");
      // this.form.createTime = new Date();
      // this.form.keepingTime = new Date();
      this.open = true;
      this.title = "添加调价管理";
      this.BtnState = 1;
      this.disableDetails = false;
    },
    /** 记账 **/
    handleUpdataKeeping() {
      updataKeeping(this.tableId).then((res) => {
        this.msgSuccess("记账成功");
        this.open = false;
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(num, row) {
      this.reset();
      this.BtnState = num;
      const id = row.id || this.ids;
      this.tableId = id;

      getModifyPrice(id).then((response) => {
        response.data.modifyPriceDetails.map((item) => {
          item.drugsType = String(item.drugsType);
          item.packingUnit = String(item.packingUnit);
        });
        this.form = response.data;
        this.modifyPriceDetails = response.data.modifyPriceDetails;
        /*this.modifyPriceDetails.forEach((item, index) => {
          this.getListSpecs(item.drugsId, index);
          this.getListPrice(item.specsId, index);
          // this.modifyPriceDetails[index].packingUnit = String(item.packingUnit)
        });*/
        this.open = true;
        num == 3
          ? ((this.title = "调价管理详情"), (this.disableDetails = true))
          : num == 1
          ? ((this.title = "修改入库单"), (this.disableDetails = false))
          : num == 2
          ? ((this.title = "记账入库单"), (this.disableDetails = true))
          : "";
      });
    },
    formatTime(date, fmt) {
      var date = new Date(date);
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          (date.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
      }
      var o = {
        "M+": date.getMonth() + 1,
        "d+": date.getDate(),
        "h+": date.getHours(),
        "m+": date.getMinutes(),
        "s+": date.getSeconds(),
      };
      for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
          var str = o[k] + "";
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length === 1 ? str : ("00" + str).substr(str.length)
          );
        }
      }
      return fmt;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.modifyPriceDetails = this.modifyPriceDetails;
          // this.form.createTime = this.formatTime(
          //   this.form.createTime,
          //   "yyyy-MM-dd hh:mm:ss"
          // );
          // this.form.keepingTime = this.formatTime(
          //   this.form.keepingTime,
          //   "yyyy-MM-dd hh:mm:ss"
          // );
          if (this.form.id != null) {
            updateModifyPrice(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addModifyPrice(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("确定需要删除选中数据吗？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delModifyPrice(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有调价管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportModifyPrice(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped>
/* .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 190px;
} */
.el-input--small .el-input__inner {
  height: 36px;
  line-height: 36px;
}
</style>