<template>
  <!-- 采购明细出库 -->
  <div>
    <div class="common-card">
      <el-form :model="listForm" ref="planForm" :inline="true">
        <el-form-item label="随货同行单号" prop="aa" :rules="rules.required">
          <el-input v-model="listForm.aa" placeholder="请输入随货同行单号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getDetails('search')"
            >提取采购明细
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-card">
      <el-form :model="tableData" ref="tabledata">
        <el-table
          :data="tableData"
          v-loading="loding"
          element-loading-text="加载中..."
          border
          :span-method="handleSpan"
        >
          <template slot="empty">
            <img src="@/assets/nopage.png" alt />
            <p>暂无数据</p>
          </template>
          <el-table-column fixed type="index" label="序号" width="60" />
          <el-table-column label="耗材编码" prop="materialId" />
          <el-table-column label="医保编码" prop="ybbm" />
          <el-table-column label="耗材名称" prop="materialName" />
          <el-table-column label="商品类别" prop="prodType" />
          <el-table-column label="商品分类" prop="drugListId" />
          <el-table-column label="规格" prop="spec" />
          <el-table-column label="型号" prop="mol" />
          <el-table-column label="材质" prop="matl" />
          <el-table-column label="生产来源" prop="source" />
          <el-table-column label="注册证号" prop="approveNo" />
          <el-table-column label="生产企业" prop="factory" />
          <el-table-column label="单价" prop="purcPric" />
          <el-table-column label="需求数量" prop="purcCnt" />
          <el-table-column label="已出库数量" prop="delvCntYck" width="90px" />
          <el-table-column label="剩余出库量" prop="toDelvCnt" width="90px" />
          <el-table-column label="出库数量" prop="delvCnt" width="150">
            <template slot-scope="scope">
              <el-form-item
                label-width="0"
                class="item"
                :prop="scope.$index + '.delvCnt'"
                :rules="[
                  {
                    required: true,
                    pattern: /^[0-9]\d*$/,
                    message: '请输入正整数',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input-number
                  v-model="scope.row.delvCnt"
                  controls-position="right"
                  @change="updateNum(scope.row, scope.$index)"
                  :min="0"
                ></el-input-number>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="批号" prop="lotNum" width="120">
            <template slot-scope="scope">
              <el-form-item
                label-width="0"
                class="item"
                :prop="scope.$index + '.lotNum'"
                :rules="[
                  {
                    required: true,
                    message: '请输入',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input placeholder="请输入" v-model="scope.row.lotNum"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="生产日期" prop="manuDate" width="220">
            <template slot-scope="scope">
              <el-form-item
                label-width="0"
                class="item"
                :prop="scope.$index + '.manuDate'"
                :rules="[
                  {
                    required: true,
                    message: '请选择生产日期',
                    trigger: 'change',
                  },
                ]"
              >
                <el-date-picker
                  style="width: 100%"
                  v-model="scope.row.manuDate"
                  type="date"
                  placeholder="请选择生产日期"
                  value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="有效期至" width="220" prop="prodExpy">
            <template slot-scope="scope">
              <el-form-item
                label-width="0"
                class="item"
                :prop="scope.$index + '.prodExpy'"
                :rules="[
                  {
                    required: true,
                    message: '请选择有效期',
                    trigger: 'change',
                  },
                ]"
              >
                <el-date-picker
                  style="width: 100%"
                  v-model="scope.row.prodExpy"
                  type="date"
                  placeholder="请选择有效期"
                  value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="报关单" prop="invoFileId" width="200">
            <template slot-scope="scope">
              <yl-upload
                v-if="scope.row.source == '进口'"
                :fileId="scope.row.invoFileId"
                @getUpload="getUpload1"
                urlName="acco"
                :index="scope.$index"
              >
                <template v-slot:dec>只能上传pdf文件</template>
              </yl-upload>
            </template>
          </el-table-column>
          <!-- <el-table-column label="随货单" prop="dyntFileId" width="200">
            <template slot-scope="scope">
                <yl-upload
                :fileId="scope.row.dyntFileId"
                @getUpload="getUpload"
                urlName="acco"
                :index="scope.$index"
                >
                <template v-slot:dec>只能上传pdf文件</template>
                </yl-upload>
            </template>
          </el-table-column> -->
          <el-table-column label="备注" prop="memo" width="120">
            <template slot-scope="scope">
              <el-form-item label-width="0" class="item">
                <el-input placeholder="请输入" v-model="scope.row.memo"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column align="center" fixed="right" label="操作" width="120">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.showDeleteButton"
                class="red"
                type="text"
                @click="handleDelete(scope.row, scope.$index)"
                >删除</el-button
              >
              <el-button v-else type="text" @click="handleBatch(scope.row, scope.$index)"
                >添加批次
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </div>
    <div class="foot-button" style="margin-top: 30px">
      <!-- <el-button type="success" @click="handleSave">保存</el-button> -->
      <el-button type="primary" @click="handleDelivery">出库</el-button>
    </div>
    <el-dialog title="提取采购单" :visible.sync="dialog" width="70%">
      <template>
        <el-form :model="listQuery" ref="listQuery" :inline="true" label-width="90px">
          <el-form-item size="mini" label="采购单号" prop="id" class="long">
            <el-input v-model="listQuery.id" placeholder="请输入采购单号"></el-input>
          </el-form-item>
          <el-form-item size="mini" label="采购员" prop="docmker" class="long">
            <el-input v-model="listQuery.docmker" placeholder="请输入采购员"></el-input>
          </el-form-item>
          <el-form-item size="mini" label="耗材名称" prop="prodName" class="long">
            <el-input
              v-model="listQuery.prodName"
              placeholder="请输入耗材名称"
            ></el-input>
          </el-form-item>
          <el-form-item size="mini" label="医保编码" prop="hiCode" class="long">
            <el-input v-model="listQuery.hiCode" placeholder="请输入医保编码"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="getdialog('search')">查询</el-button>
            <el-button @click="resetdialog('consumableData')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table
          ref="multipleTable"
          v-loading="dialogLoading"
          element-loading-text="加载中"
          fit
          stripe
          border
          :data="consumableData"
          border
          style="width: 100%"
          row-key="id"
          height="500"
          @selection-change="handleSelectionChange"
          highlight-current-row
          :default-sort="{ prop: 'hiCode', order: 'descending' }"
        >
          <el-table-column
            type="selection"
            :reserve-selection="true"
            width="55"
          ></el-table-column>
          <el-table-column prop="hiCode" label="医保编码"> </el-table-column>
          <el-table-column prop="prodName" label="耗材名称" width="180">
          </el-table-column>
          <el-table-column prop="pubonlnPric" label="挂网价格"> </el-table-column>
          <el-table-column prop="isFas" label="是否集采">
            <template #default="{ row }">
              <span v-if="row.isFas == '1'">否</span>
              <span v-if="row.isFas == '0'">是</span>
            </template>
          </el-table-column>
          <el-table-column prop="fasBtch" label="集采批次"> </el-table-column>
          <el-table-column prop="spec" label="规格"> </el-table-column>
          <el-table-column prop="mol" label="型号"> </el-table-column>
          <el-table-column prop="matl" label="材质"> </el-table-column>
          <el-table-column prop="pacmatl" label="包装材质"> </el-table-column>
          <el-table-column prop="prodSouc" label="生产来源">
            <template #default="{ row }">
              <span v-if="row.prodSouc == '1'">国产</span>
              <span v-if="row.prodSouc == '0'">进口</span>
            </template>
          </el-table-column>
          <el-table-column prop="regcertno" label="注册证号"> </el-table-column>
          <el-table-column prop="prodEntp" label="生产企业" width="120">
          </el-table-column>
          <el-table-column prop="agnt" label="代理企业" width="150"> </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="handleConfirm">确认</el-button>
        </div>
        <!--分页-->
        <yl-pagination
          v-show="total > 0"
          :total="total"
          :page.sync="listQuery.current"
          :limit.sync="listQuery.size"
          @pagination="getdialog"
        />
      </template>
    </el-dialog>
  </div>
</template>

<script>
import rules from "@/utils/rules";
export default {
  data() {
    return {
      rules,
      listForm: {
        aa: "",
      },
      planForm: {},
      // 表格数据
      loding: false,
      tableData: [],
      // 弹框表格数据
      dialog: false,
      listQuery: {
        hiCode: "",
        prodName: "",
        id: "",
        docmker: "",
      },
      consumableData: [],
      selectData: [],
      dialogLoading:false,
      Loading:false,
    };
  },
  methods: {
    getDetails() {
      this.$refs["planForm"].validate((valid) => {
        if (valid) {
          this.dialog = true;
          this.getdialog();
          this.$nextTick(() => {
            this.$refs.tidata.clearSelection();
            this.tableData.forEach((i) => {
              this.consumableData.forEach((row) => {
                if (i.id == row.id) {
                  this.$refs.tidata.toggleRowSelection(row, true);
                }
              });
            });
          });
        }
      });
    },
    // 列表数据
    getData() {
      this.Loading = true;
      getPurcOrdDrugList(this.listForm)
        .then((res) => {
          this.tableData = res.data.records;
          this.Loading = false;
        })
        .catch((err) => {
          this.Loading = false;
        });
    },
    // 采购明细数据
    getdialog() {
      if (type == "search") {
        this.listQuery.current = 1;
      }
      this.dialogLoading = true;
      getPurcOrdDrugList(this.listQuery)
        .then((res) => {
          this.consumableData = res.data.records;
          this.consumableData.forEach((i) => {
            i.docmkDate = moment(i.docmkDate).format("YYYY-MM-DD HH:mm:ss");
          });
          this.total = res.data.total;
          this.dialogLoading = false;
        })
        .catch((err) => {
          this.dialogLoading = false;
        });
    },
    // 合并列
    handleSpan({ row, column, rowIndex, columnIndex }) {
      if (
        columnIndex === 1 ||
        columnIndex === 2 ||
        columnIndex === 3 ||
        columnIndex === 4 ||
        columnIndex === 5 ||
        columnIndex === 6 ||
        columnIndex === 7 ||
        columnIndex === 8 ||
        columnIndex === 9 ||
        columnIndex === 10 ||
        columnIndex === 11 ||
        columnIndex === 12 ||
        columnIndex === 13 ||
        columnIndex === 14
      ) {
        // console.log("单元格数组，若下一项为0，则代表合并上一项", this.cellList);
        const rowCell = this.cellList[rowIndex];
        if (rowCell > 0) {
          const colCell = 1;
          // console.log(`动态竖向合并单元格, 第${colCell}列，竖向合并${rowCell}个单元格 `);
          return {
            rowspan: rowCell,
            colspan: colCell,
          };
        } else {
          // 清除原有的单元格，必须要加，否则就会出现单元格会被横着挤到后面了！！！
          // 本例中数据是写死的不会出现，数据若是动态后端获取的，就会出现了！！！
          return {
            rowspan: 0,
            colspan: 0,
          };
        }
      }

      // if (columnIndex === 2 && rowIndex == 0) {
      //     return {
      //         rowspan: this.pos,
      //         colspan: 1
      //     };
      // }else{
      //     for(let i=1;i<this.pos;i++){
      //         if(rowIndex === this.pos-i && columnIndex == 2){
      //             return {
      //                 rowspan: 0,
      //                 colspan: 0
      //             };
      //         }
      //     }
      // }
    },
    //弹框选择
    handleSelectionChange() {
      this.selectData = e;
    },
    // 出库
    handleDelivery() {
      if (this.tableData.length != 0) {
        this.$refs.tabledata.validate((valid) => {
          if (valid) {
            let data = this.tableData.map((i) => {
              return i.purcOrdId;
            });
            let data2 = data.slice(1);
            let qwe = data2.some((item) => {
              if (item != data[0]) {
                return true;
              }
            });
            if (qwe == true) {
              this.$message.info("请勾选同一采购单明细");
            } else {
              this.$confirm("确认出库", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
              }).then(() => {
                let data = this.tableData.map((i) => {
                  return {
                    drugListId: i.drugListId,
                    delvCnt: i.delvCnt,
                    lotNum: i.lotNum,
                    manuDate: i.manuDate,
                    prodExpy: i.prodExpy,
                    invoFileId: i.invoFileId,
                    dyntFileId: i.dyntFileId,
                    delvPric: i.purcPric,
                  };
                });
                let data2 = {
                  delvOrdId: "",
                  spdId: "",
                  dyntNo: this.listForm.dyntNo,
                  purcOrdId: this.tableData[0].purcOrdId,
                  detlList: data,
                };
                this.confirmLoadLoad = true;
                submitDelvOrd(data2)
                  .then((res) => {
                    this.confirmLoadLoad = false;
                    this.getTiData();
                    this.tableData = [];
                    this.selectData = [];
                    this.$message({
                      message: "出库成功",
                      type: "success",
                    });
                    this.listForm.dyntNo = "";
                  })
                  .catch((err) => {
                    this.confirmLoadLoad = false;
                  });
              });
            }
          } else {
            this.$message({
              type: "info",
              message: "请填写必填项信息",
            });
          }
        });
        // }else{
        //     this.$message({
        //         type: "info",
        //         message: "随货同行单号不能为空",
        //     });
        // }
      } else {
        this.$message({
          type: "info",
          message: "出库数据不能为空",
        });
      }
    },
  },
};
</script>

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