<template>
  <div>
    <el-card>
      <el-form :model="filterQuery" size="mini" label-width="100px" v-show="showSearch">
        <el-row>
          <el-col :span="6">
            <el-form-item label="产品标识DI:">
              <el-input v-model="filterQuery.nameCode" style="width: 90%" placeholder="产品标识DI"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="产品名称:">
              <el-input v-model="filterQuery.cpmctymc" style="width: 90%" placeholder="产品名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="批次号:">
              <el-input v-model="filterQuery.batchNo" style="width: 90%" placeholder="批次号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="top-right-btn">
        <el-button-group style="display:flex;">
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="combine">确定</el-button>
        </el-button-group>
      </div>

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


      <el-table v-loading="loading" :data="list" style="width: 100%" highlight-current-row="false" border
                :row-class-name="tableRowClassName" @current-change="handleChange"
                ref="multipleTable">
        <el-table-column label width="45">
          <template slot-scope="scope">
            <el-radio :label="scope.row.id" v-model="radioCheck"><span></span></el-radio>
          </template>
        </el-table-column>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="产品标识" prop="nameCode"></el-table-column>
        <el-table-column label="产品名称" prop="cpmctymc">
        </el-table-column>
        <el-table-column label="规格型号" prop="ggxh"></el-table-column>
        <el-table-column label="批次号" prop="batchNo"></el-table-column>
        <el-table-column label="生产日期" prop="productionDate"></el-table-column>
        <el-table-column label="失效日期" prop="expireDate"></el-table-column>
        <el-table-column label="入库数量" prop="inCount"></el-table-column>
        <el-table-column label="出库数量" prop="outCount"></el-table-column>
        <el-table-column label="结余数量" prop="reCount"></el-table-column>
      </el-table>
    </el-card>

    <pagination
      v-show="total>0"
      :total="total"
      :limit.sync="filterQuery.limit"
      :page.sync="filterQuery.page"
      @pagination="handleCurrentChange"
    ></pagination>
  </div>
</template>

<script>
import {
  getInvProduct,
} from "@/api/inventory/invPorduct";
import {addOrderDetail} from "@/api/purchase/purReturned";


export default {
  name: "DialogInvProduct",
  props: {
    closeDialog: {
      type: Function,
      required: true,
    },
    invQueryData: {
      type: Object,
      required: true,
    },
    pId: {
      type: Object,
      required: true,
    },
    customerId: {
      type: Object,
      required: true,
    },
    invCode: {},
  },
  data() {
    return {
      showSearch: true,
      filterQuery: {
        cpmctymc: null,
        nameCode: null,
        relIdFk: null,
        batchNo: null,
        page: 1,
        limit: 10,
        customerId: null,
        supId: null,
        unitFk: null,
        invCode: this.invCode,
      },
      detailQuery: {
        code: null,
        productIdFk: null,
        page: 1,
        limit: 20,
      },
      list: [],
      radioCheck: null,
      total: 0,
      loading: true,
      index: null,
      dialogTableVisible: false,
      formLoading: false,
      dialogVisible: false,
      deleteLoading: false,
      busTypes: [],
      idQuery: null,
      showSup: false,
      fromOptions: [],
    };
  },
  components: {},
  methods: {
    onReset() {

      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        cpmctymc: null,
        nameCode: null,
        relIdFk: null,
        batchNo: null,
        customerId: null,
        unitFk: null,
        supId: null,
        page: 1,
        limit: 10,
        invCode: this.invCode,
      };
      this.getList();
    },
    onSubmit() {
      this.loading = true;
      this.filterQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.filterQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val.page;
      this.getList();
    },
    closeDetailDialog(val) {
      this.codeDetailVisible = false;
    },

    getList() {
      this.loading = true;
      this.filterQuery.supId = this.customerId
      getInvProduct(this.filterQuery)
        .then((response) => {
          this.showSup = response.data.showSup;
          this.loading = false;
          this.list = response.data.list || [];
          this.total = response.data.total || 0;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    combine() {
      this.loading = true;
      let tQuery = {
        productId: this.multipleSelection.relIdFk,
        productName: this.multipleSelection.cpmctymc,
        count: 1,
        orderIdFk: this.pId,
        supId: this.multipleSelection.customerId,
        zczbhhzbapzbh: this.multipleSelection.zczbhhzbapzbh
      };
      addOrderDetail(tQuery).then((response) => {
        this.loading = false;
        if (response.code === 20000) {
          this.closeDialog(response.data);
        } else {
          if (response.code == 601) {
            this.$alert(response.message, "提示", {
              confirmButtonText: "确定",
            });
          } else
            this.$message.error(response.message);
        }
      }).catch(() => {
        this.loading = false;
      });
    },
    tableRowClassName({row}) {
      if (row.checked) return "warning-row";
      return "";
    },
    checkSelectable(row) {
      return !row.check;
    },
    handleChange(val) {
      this.radioCheck = val.id;
      this.currentRow = val;
      this.multipleSelection = val
    },
  },
  filters: {
    statusFilterType(status) {
      const statusMap = {
        false: "success",
        true: "danger",
      };
      return statusMap[status];
    },
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    // 加载表格数据
    this.getList();
  },
};
</script>

<style type="text/scss" lang="scss">
</style>
