<template>
  <div>
    <el-card className="el-card">
      <el-form :model="filterQuery" class="query-form" size="mini" label-width="100px" v-if="showSearch">
        <el-row>
          <el-col :span="6">
            <el-form-item class="query-form-item" label="单号:">
              <el-input v-model="filterQuery.billNo" placeholder="单号"
                        style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item class="query-form-item" label="所属仓库:">
              <el-select v-model="filterQuery.invCode" placeholder="请选择所属仓库" clearable="true"
                         style="width: 90%"
                         @change="invChange"
              >
                <el-option
                  v-for="item in invList"
                  :key="item.name"
                  :label="item.name"
                  :value="item.code">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item class="query-form-item" label="单据类型:">
              <el-select v-model="filterQuery.billType" placeholder="请选择单据类型" style="width: 90%">
                <el-option
                  v-for="item in busTypes"
                  :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 class="query-form-item" label="提交状态:">
              <el-select v-model="filterQuery.submitStatus" placeholder="请选择提交状态" style="width: 90%">
                <el-option
                  v-for="item in submitStatusList"
                  :key="item.label"
                  :label="item.label"
                  :value="item.value">
                  <span style="float: left">{{ item.label }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item class="query-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-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 100%" highlight-current-row
                border
                @current-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="单据号" prop="billNo" show-overflow-tooltip width="150"></el-table-column>
        <el-table-column label="单据类型" prop="billTypeName" width="150"></el-table-column>
        <el-table-column label="往来单位" prop="fromName" width="180">
        </el-table-column>
<!--        <el-table-column label="创建时间" prop="createTime" show-overflow-tooltip width="150"-->
<!--                         sortable="custom"-->
<!--                         :sort-orders="['ascending', 'descending']">-->
<!--          <template slot-scope="scope">-->
<!--            <i class="el-icon-time"></i>-->
<!--            <span>{{ scope.row.createTime }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->

<!--        <el-table-column label="审核时间" prop="auditTime" show-overflow-tooltip width="150"-->
<!--                         sortable="custom"-->
<!--                         :sort-orders="['ascending', 'descending']">-->
<!--          <template slot-scope="scope">-->
<!--            <i class="el-icon-time"></i>-->
<!--            <span>{{ scope.row.auditTime }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->


<!--        <el-table-column label="所属部门" prop="deptName" width="120">-->
<!--        </el-table-column>-->
<!--        <el-table-column label="所属仓库" prop="invName" width="120">-->
<!--        </el-table-column>-->

        <el-table-column label="审核人" prop="reviewUserName" width="120">
        </el-table-column>

<!--        <el-table-column label="来源单号" prop="corpOrderId" show-overflow-tooltip width="180"></el-table-column>-->
<!--        <el-table-column label="来源" prop="fromType" width="120">-->
<!--          <template slot-scope="scope">-->
<!--            <span>{{ fromTypeMap[scope.row.fromType] }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="审核状态" prop="status" width="100">
          <template slot-scope="scope">
            <el-tag :type="statusAuditFilterType(scope.row.status)">{{
                checkStatus[scope.row.status]
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="提交状态" prop="exportStatus" width="100">
          <template slot-scope="scope">
            <el-tag :type="statusFilterType(scope.row.exportStatus)">{{
                exportStatusMap[scope.row.exportStatus]
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="确认状态" prop="checkStatus" width="100">
          <template slot-scope="scope">
            <el-tag :type="statusCheckType(scope.row.checkStatus)">{{
                checkDateStatus[scope.row.checkStatus]
              }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="结果信息" prop="result" width="120"></el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="submitOrder(scope.row)"
            >提交
            </el-button
            >
          </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>

    <el-tabs type="border-card" style="margin: 15px">
      <!--单据业务详情-->
      <el-tab-pane>
        <span slot="label">单据 {{ currentRow.billNo }}-单据详情</span>
        <el-form :inline="true" :model="bizQuery" class="query-form" size="mini">
          <el-form-item class="query-form-item" label="产品名称:">
            <el-input
              v-model="bizQuery.productName"
              placeholder="请输入产品名称"
              clearable
            >
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button-group>
              <el-button
                type="primary"
                icon="el-icon-refresh"
                @click="onBizReset"
              >重置
              </el-button>
              <el-button type="primary" @click="onBizSubmit"
                         icon="el-icon-search"
              >查询
              </el-button>
            </el-button-group>
          </el-form-item>
        </el-form>

        <el-table v-loading="bizDetailLoading" :data="bizDetailList" style="width: 100%" border
                  row-key="id"
        >
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column
            label="产品通用名称"
            prop="coName"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="DI/物资编码"
            prop="nameCode"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="规格型号"
            prop="spec"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="批次号"
            prop="batchNo"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="生产日期"
            prop="productDate"
          ></el-table-column>
          <el-table-column
            label="失效日期"
            prop="expireDate"
          ></el-table-column>
          <el-table-column
            label="单据数量"
            prop="reCount"
          ></el-table-column>
          <el-table-column
            label="价格"
            prop="price"
          ></el-table-column>
          <el-table-column
            label="生产企业"
            prop="manufacturer"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="注册/备案凭证号"
            prop="certCode"
            show-overflow-tooltip
          ></el-table-column>
        </el-table>
        <pagination
          v-show="bizTotal>0"
          :total="bizTotal"
          :page.sync="bizQuery.page"
          :limit.sync="bizQuery.limit"
          @pagination="getBizDetailList"
        />
      </el-tab-pane>

      <!--单据扫码明细-->
      <el-tab-pane>
        <span slot="label">单据 {{ currentRow.billNo }}-扫码明细</span>
        <el-form :inline="true" :model="resultQuery" class="query-form" size="mini">
          <el-form-item class="query-form-item" label="产品名称:">
            <el-input
              v-model="resultQuery.code"
              placeholder="请输入UDI码"
              clearable
            >
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button-group>
              <el-button
                type="primary"
                icon="el-icon-refresh"
                @click="onResultReset"
              >重置
              </el-button>
              <el-button type="primary" @click="onResultSubmit"
                         icon="el-icon-search"
              >查询
              </el-button
              >
            </el-button-group>
          </el-form-item>
        </el-form>

        <el-table v-loading="resultDetailLoading" :data="resultDetailList" style="width: 100%" border
                  row-key="id"
        >
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column
            label="UDI码"
            prop="code"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="生产日期"
            prop="produceDate"
            width="120"
          ></el-table-column>
          <el-table-column
            label="失效日期"
            prop="expireDate"
            width="120"
          ></el-table-column>
          <el-table-column
            label="批次号"
            prop="batchNo"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="序列号"
            prop="serialNo"
          ></el-table-column>
          <el-table-column
            label="供应商"
            prop="supName"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="扫码数量"
            prop="reCount"
          ></el-table-column>
          <el-table-column
            label="实际数量"
            prop="reCount"
          ></el-table-column>
        </el-table>
        <pagination
          v-show="resultTotal>0"
          :total="resultTotal"
          :page.sync="resultQuery.page"
          :limit.sync="resultQuery.limit"
          @pagination="getResultDetailList"
        />
      </el-tab-pane>
    </el-tabs>

  </div>
</template>

<script>
import {getLocalJoinByUser} from "@/api/basic/busType";
import {getInvListByUser} from "@/api/system/invWarehouse";
import {getResultDetailList} from "@/api/inout/orderDetailResult";
import {getCodeList} from "@/api/inout/code";
import {getUploadOrderList, submitOrderToThrSys} from "@/api/inout/order";


const formJson = {
  site_id: "",
  site_name: "",
  describe: "",
  ads: [],
};

export default {
  name: "uploadOrder",
  data() {
    return {
      filterQuery: {
        billType: null,
        corpName: null,
        invCode: this.$store.getters.locInvCode,
        startTime: null,
        endTime: null,
        billNo: null,
        submitStatus: null,
        vueType: "thirdUpload",
        page: 1,
        limit: 10
      },
      showSearch: true,
      invList: [],
      busTypes: [],
      orderStatusList: [
        {
          label: '未配货',
          value: '2'
        },
        {
          label: '待校验',
          value: '3'
        },
        {
          label: '已校验',
          value: '4'
        },
        {
          label: '已验收',
          value: '5'
        }
      ],
      submitStatusList: [
        {
          label: '未提交',
          value: 0
        },
        {
          label: '已提交',
          value: 1
        },
        {
          label: '提交失败',
          value: 2
        },
      ],
      actDateRange: null,
      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]);
            },
          },
        ],
      },
      fromTypeMap: {
        "1": "UDIMS平台",
        "2": "网页新增",
        "3": "pda即时校验",
        "4": "pda未校验",
        "5": "pc端扫码精灵",
        "6": "缺量补录单据",
        "7": "UDI供应商平台",
        "8": "平衡补录单据",
        "10": "手动补单",
        "11": "仓库盘点",
        "12": "采购计划",
        "13": "领用单据",
        "14": "第三方系统单据",
      },
      exportStatusMap: {
        null: "未提交",
        0: "未提交",
        1: "已提交",
        2: "提交失败"
      },
      checkDateStatus: {
        null:"未确认",
        1: '已确认',
        0: '未确认'
      },
      checkStatus: {
        1: "草稿",
        2: "等待处理",
        3: "等待校验",
        4: "处理异常",
        5: "待核对",
        6: "校验异常",
        7: "已审核",
        8: "审核拒绝",
        9: "正在处理", 10: "待审核", 11: "待配货",
      },
      loading: false,
      list: [],
      total: 0,
      currentRow: {
        billNo: ""
      },
      bizQuery: {
        orderIdFk: null,
        productName: null,
        page: 1,
        limit: 10
      },
      bizDetailLoading: false,
      bizDetailList: [],
      bizTotal: 0,
      resultQuery: {
        orderIdFk: null,
        code: null,
        page: 1,
        limit: 10
      },
      resultDetailLoading: false,
      resultDetailList: [],
      resultTotal: 0
    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.filterQuery = {
        billType: null,
        corpName: null,
        invCode: this.$store.getters.locInvCode,
        startTime: null,
        vueType: "thirdUpload",
        endTime: null,
        billNo: null,
        submitStatus: null,
        page: 1,
        limit: 10
      };
      this.actDateRange = [];
      this.getList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      if (this.actDateRange !== null) {
        this.filterQuery.startTime = this.actDateRange[0];
        this.filterQuery.endTime = this.actDateRange[1];
      } else {
        this.filterQuery.startTime = null;
        this.filterQuery.endTime = null;
      }
      this.getList();
    },
    getList() {
      this.loading = true;
      this.clearDetailTable();
      getUploadOrderList(this.filterQuery).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.list = res.data.list || [];
          this.total = res.data.total || 0;
        } else {
          this.list = [];
          this.total = 0;
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
        this.list = [];
        this.total = 0;
      })
    },
    clearDetailTable() {
      this.currentRow.billNo = "";
      this.bizDetailList = [];
      this.bizTotal = 0;
      this.resultDetailList = [];
      this.resultTotal = 0;
      this.bizQuery.page = 1;
      this.resultQuery.page = 1;
    },
    handleSelectionChange(row) {
      this.currentRow.billNo = row.billNo;
      this.getBizDetailList();
      this.getResultDetailList();
    },
    submitOrder(row) {
      if (row.exportStatus == 1) {
        this.$confirm("单据已经提交，是否重复提交？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true
        }).then(() => {
          this.uploadOrder(row);
        });
      } else {
        this.$confirm("确定手动上传单据？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true
        }).then(() => {
          this.uploadOrder(row);
        }).catch(() => {
        })
      }
    },
    uploadOrder(row) {
      this.loading = true;
      this.clearDetailTable();
      let params = {billNo: row.billNo};
      submitOrderToThrSys(params).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.$message.success("上传成功");
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
      })
    },
    onBizReset() {
      this.bizQuery.productName = null;
      this.onBizSubmit();
    },
    onBizSubmit() {
      this.bizQuery.page = 1;
      this.getBizDetailList();
    },
    getBizDetailList() {
      this.bizQuery.orderIdFk = this.currentRow.billNo;
      this.bizDetailLoading = true;
      getResultDetailList(this.bizQuery).then((res) => {
        this.bizDetailLoading = false;
        if (res.code === 20000) {
          this.bizDetailList = res.data.list || [];
          this.bizTotal = res.data.total || 0;
        } else {
          this.$message.error(res.message);
          this.bizDetailList = [];
          this.bizTotal = 0;
        }
      }).catch((error) => {
        this.bizDetailLoading = false;
        this.$message.error(error.message);
        this.bizDetailList = [];
        this.bizTotal = 0;
      })
    },
    onResultReset() {
      this.resultQuery.code = null;
      this.onResultSubmit();
    },
    onResultSubmit() {
      this.resultQuery.page = 1;
      this.getResultDetailList();
    },
    getResultDetailList() {
      this.resultDetailLoading = true;
      this.resultQuery.orderId = this.currentRow.billNo;
      getCodeList(this.resultQuery).then((res) => {
        this.resultDetailLoading = false;
        if (res.code === 20000) {
          this.resultDetailList = res.data.list || [];
          this.resultTotal = res.data.total || 0;
        } else {
          this.$message.error(res.message);
          this.resultDetailList = [];
          this.resultTotal = 0;
        }
      }).catch((error) => {
        this.resultDetailLoading = false;
        this.$message.error(error.message);
        this.resultDetailList = [];
        this.resultTotal = 0;
      })
    },
    getInvList() {
      getInvListByUser().then((res) => {
        this.invList = res.data || [];
        this.getBusType();
      })
    },
    invChange() {
      this.filterQuery.billType = null;
      this.getBusType();
    },
    getBusType() {
      let params = {
        code: this.filterQuery.invCode,
        vueType: "thirdUpload",
        enabled: true
      };
      getLocalJoinByUser(params).then((res) => {
        this.busTypes = res.data.list || [];
        this.onSubmit();
      });
    },
    statusAuditFilterType(status) {
      const statusMap = {
        5: "warning",
        1: "warning",
        6: "danger",
        7: "success",
      };
      return statusMap[status];
    },
    statusCheckType(status) {
      const statusMap = {
        0: 'warning',
        1: 'success',
      }
      return statusMap[status]
    },
    statusFilterType(status) {
      const statusMap = {
        null: "",
        1: "success",
        0: "warning",
        2: "danger"
      };
      return statusMap[status];
    },
  },
  filters: {

  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getInvList();
  },
};
</script>

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

