<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" size="mini" label-width="100px" v-if="showSearch">
        <el-row>
          <el-col :span="6">
            <el-form-item label="出入库单号:">
              <el-input v-model="filterQuery.orderIdFk" placeholder="请输入出入库单号" style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="DI/物资编码:">
              <el-input v-model="filterQuery.nameCode" placeholder="请输入DI/物资编码" style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物资名称:">
              <el-input v-model="filterQuery.coName" placeholder="请输入物资名称" style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="规格型号:">
              <el-input v-model="filterQuery.spec" placeholder="请输入规格型号" style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="批次号:">
              <el-input v-model="filterQuery.batchNo" placeholder="请输入批次号" style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="注册/备案号:">
              <el-input v-model="filterQuery.zczbhhzbapzbh" placeholder="注册/备案号"
                        style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="生产企业:">
              <el-input v-model="filterQuery.manufacturer" placeholder="生产企业"
                        style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="往来单位:">
              <el-input v-model="filterQuery.keyWords" placeholder="请选择往来单位"
                        style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="出入库类型:">
              <el-select v-model="filterQuery.mainAction" placeholder="请选择出入库类型" style="width: 90%">
                <el-option label="全部" value=""></el-option>
                <el-option label="入库" value="WareHouseIn"></el-option>
                <el-option label="出库" value="WareHouseOut"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item prop="action" label="单据类型:">
              <el-select
                v-model="filterQuery.action"
                placeholder="请选择单据类型"
                style="width: 90%"
              >
                <el-option
                  v-for="item in busTypeOptions"
                  :key="item.name"
                  :label="item.name"
                  :value="item.action"
                >
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="打印状态:">
              <el-select v-model="filterQuery.inoutPrintStatus" placeholder="请选择打印状态" style="width: 90%">
                <el-option label="全部" value=""></el-option>
                <el-option label="未打印" :value="0"></el-option>
                <el-option label="已打印" :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="出入库时间:">
              <el-date-picker
                :picker-options="pickerOptions"
                v-model="actDateRange"
                type="daterange"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width: 90%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="top-right-btn">
        <el-button-group>
          <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-document" @click="selectExport">选中导出Excel</el-button>
          <el-button type="primary" icon="el-icon-document" @click="searchExport">结果导出Excel</el-button>
          <el-button type="primary" icon="el-icon-printer" @click="optinPrint">选中打印</el-button>
          <el-button type="primary" icon="el-icon-printer" @click="selectPrint">查询结果打印</el-button>

        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>
      <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row borderv
                @selection-change="handleCheckedChange">

        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="供应商" prop="fromCorpName" width="160" v-if="showSup"></el-table-column>
        <el-table-column label="往来单位" prop="fromName" width="160"></el-table-column>
        <el-table-column label="出入库单号" prop="orderIdFk" width="160"></el-table-column>
        <el-table-column label="DI/物资编码" prop="nameCode" width="160"></el-table-column>
        <el-table-column label="物资名称" prop="coName" width="160" show-overflow-tooltip></el-table-column>
        <el-table-column label="规格型号" prop="spec" width="180" show-overflow-tooltip></el-table-column>
        <el-table-column label="批次号" prop="batchNo" width="120"></el-table-column>
        <el-table-column label="入库数量" prop="inCount" width="100"></el-table-column>
        <el-table-column label="出库数量" prop="outCount" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column label="计量单位" prop="measname" width="120"></el-table-column>
        <el-table-column label="单据类型" prop="billTypeName" width="120"></el-table-column>
        <el-table-column label="生产日期" prop="productDate" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column label="失效日期" prop="expireDate" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column label="出入库时间" prop="auditTime" width="170" show-overflow-tooltip></el-table-column>
        <el-table-column label="生产企业" prop="manufacturer" width="160" show-overflow-tooltip></el-table-column>
        <el-table-column label="注册/备案号" prop="certCode" width="160" show-overflow-tooltip></el-table-column>
        <el-table-column label="打印状态" width="100" prop="inoutPrintStatus">
          <template slot-scope="scope">
            <el-tag :type="statusFilterType(scope.row.inoutPrintStatus)">{{
                printMap[scope.row.inoutPrintStatus]
              }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>

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

<script>
import {
  getResultDetailList,
  getResultOrderList,
  verifyTemplateFile,
  printOrder,
  excelInoutImport, postExcelInoutImport
} from '@/api/inout/orderDetailResult'
import {findByInvUser, getLocalJoinByUser, getOrderDetailBus} from "@/api/basic/busType";
import {getCorrespondence} from "@/api/basic/basicUnitMaintain";
import {excelImport} from "@/api/inout/statData";

export default {
  name: "supInoutSearchOrder",
  data() {
    return {
      showSearch: true,
      list: {},
      busTypeOptions: [],
      filterQuery: {
        id: "",
        orderIdFk: null,
        fromCorpName: null,
        coName: null,
        spec: null,
        batchNo: null,
        page: 1,
        limit: 10,
        startAduditTime: null,
        endAduditTime: null,
        actionType: null,
        fromCorp: null,
        keyWords: null,
        inoutPrintStatus: null,

      },

      printMap: {
        0: "未打印",
        1: "已打印",
        null: "未打印",
      },

      fromOptions: [],
      loading: false,
      total: 0,
      orderSelection: [],
      showSup: false,
      customerId: this.$store.getters.customerId,
      actDateRange: [],
      actionType: null,

      auditDateRange: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  components: {},
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        id: "",
        orderIdFk: null,
        fromCorpName: null,
        coName: null,
        spec: null,
        batchNo: null,
        page: 1,
        limit: 10,
        startAduditTime: null,
        endAduditTime: null,
        actionType: null,
      };
      this.actDateRange = []
      this.getList();
    },
    handleCheckedChange(val) {
      this.orderSelection = val;
    },

    onSubmit() {
      this.loading = true;
      if (this.actDateRange !== null) {
        this.filterQuery.startAduditTime = this.actDateRange[0];
        this.filterQuery.endAduditTime = this.actDateRange[1];
      } else {
        this.filterQuery.startAduditTime = null;
        this.filterQuery.endAduditTime = null;
      }
      this.filterQuery.page = 1;

      this.getList();
    },

    statusFilterType(status) {
      const statusMap = {
        null: "",
        1: "success",
        0: "warning",
      };
      return statusMap[status];
    },

    hideSearch() {
      this.showSearch = !this.showSearch;
    },

    //根据仓库,用户获取单据类型
    getBusTypeByInv() {
      let query = {
        code: this.$store.getters.locInvCode,
        enabled: true,
        detailType: this.actionType
      };
      getOrderDetailBus(query)
        .then((response) => {
          this.busTypeOptions = response.data.list || [];
        })
        .catch(() => {
        });
    },

    getList() {
      this.loading = true;
      this.filterQuery.actionType = this.actionType;
      if (this.filterQuery.actionType == 'preInDetail') {
        this.filterQuery.invCode = '1000001'
      } else if (this.filterQuery.actionType == 'preDetail') {
        this.filterQuery.invCode = '1000002'
      }
      getResultOrderList(this.filterQuery)
        .then((response) => {
          if (response.code === 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message);
          }
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    findMethod(query) {
      this.fromOptions = [];
      let cQuery = {
        key: query,
        corpType: 2,
        page: 1,
        limit: 10,
      };
      getCorrespondence(cQuery)
        .then((response) => {
          this.loading = false;
          this.fromOptions = response.data.list || [];
        })
        .catch(() => {
          this.loading = false;
          this.fromOptions = [];
        });
    },
    optinPrint() {
      this.$confirm("是否确定打印选中出入库明细, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (this.orderSelection == null || this.orderSelection.length <= 0) {
            return this.$message.error("请选择打印的明细！");
          }
          let query = {id: 4};
          verifyTemplateFile(query).then((res) => {
            if (res.code === 20000) {
              let printParams = {
                templateId: res.data,
                list: this.orderSelection
              };
              printOrder(printParams).then((response) => {
                //将pdf文件转换为url。
                const binaryData = [];
                binaryData.push(response);
                //获取blob链接。
                let url = window.URL.createObjectURL(
                  new Blob(binaryData, {type: "application/pdf"})
                );
                this.loading = false;
                window.open(url);//打开新标签页，预览pdf。
              })
            } else {
              return this.$message.error(res.message);
            }
          }).catch((error) => {
            this.loading = false;
            this.$message.error(error.message);
          })
        })
        .catch(() => {
        });


    },
    selectPrint() {
      this.$confirm("是否确定打印所有查询结果数据", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {

          let query = {id: 4};
          verifyTemplateFile(query).then((res) => {
            if (res.code === 20000) {
              let printParams = {
                templateId: res.data,
                orderIdFk: this.filterQuery.orderIdFk,
                nameCode: this.filterQuery.nameCode,
                coName: this.filterQuery.coName,
                spec: this.filterQuery.spec,
                batchNo: this.filterQuery.batchNo,
                zczbhhzbapzbh: this.filterQuery.zczbhhzbapzbh,
                manufacturer: this.filterQuery.manufacturer,
                actionType: this.filterQuery.actionType,
                keyWords: this.filterQuery.keyWords,
                mainAction: this.filterQuery.mainAction,
                action: this.filterQuery.action

              };
              if (this.actDateRange !== null) {
                printParams.startAduditTime = this.actDateRange[0];
                printParams.endAduditTime = this.actDateRange[1];
              } else {
                printParams.startAduditTime = null;
                printParams.endAduditTime = null;
              }
              printOrder(printParams).then((response) => {
                //将pdf文件转换为url。
                const binaryData = [];
                binaryData.push(response);
                //获取blob链接。
                let url = window.URL.createObjectURL(
                  new Blob(binaryData, {type: "application/pdf"})
                );
                this.loading = false;
                window.open(url);//打开新标签页，预览pdf。
              })
            } else {
              this.$message.error(res.message);
              return
            }
          }).catch((error) => {
            this.loading = false;
            this.$message.error(error.message);
          })

        })
        .catch(() => {
        });
      return;


    },

    selectExport() {
      this.$confirm("是否确定导出选中出入库明细, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (this.orderSelection == null || this.orderSelection.length <= 0) {
            return this.$message.error("请选择需要导出的明细！");
          }
          let printParams = {
            list: this.orderSelection
          };
          postExcelInoutImport(printParams).then((response) => {
            let blob = new Blob([response], {type: "application/vnd.ms-excel"});
            let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象
            let a = document.createElement("a");
            a.href = url;
            a.click();
            this.$message("成功")
          })
        });


    },
    searchExport() {
      this.$confirm("是否确定导出所有查询结果数据", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {

          let printParams = {
            orderIdFk: this.filterQuery.orderIdFk,
            nameCode: this.filterQuery.nameCode,
            coName: this.filterQuery.coName,
            spec: this.filterQuery.spec,
            batchNo: this.filterQuery.batchNo,
            zczbhhzbapzbh: this.filterQuery.zczbhhzbapzbh,
            manufacturer: this.filterQuery.manufacturer,
            actionType: this.filterQuery.actionType,
            keyWords: this.filterQuery.keyWords,
            mainAction: this.filterQuery.mainAction,
            action: this.filterQuery.action
          };

          if (this.filterQuery.actionType == 'preInDetail') {
            printParams.invCode = '1000001'
          } else if (this.filterQuery.actionType == 'preDetail') {
            printParams.invCode = '1000002'
          }

          if (this.actDateRange !== null) {
            printParams.startAduditTime = this.actDateRange[0];
            printParams.endAduditTime = this.actDateRange[1];
          } else {
            printParams.startAduditTime = null;
            printParams.endAduditTime = null;
          }
          excelInoutImport(printParams).then((response) => {
            let blob = new Blob([response], {type: "application/vnd.ms-excel"});
            let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象
            let a = document.createElement("a");
            a.href = url;
            a.click();
            this.$message("成功")
          })

        });
    }
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  }
  ,
  created() {
    let supId = this.$store.getters.customerId;
    if (supId == "110") {
      this.showSup = true;
    }
    this.actionType = this.$route.query.name;
    this.getList();
    this.getBusTypeByInv();
  }
  ,
}
;
</script>

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

