<template>
  <ContainerQuery>
    <div slot="left">
      <el-dropdown
        v-if="$accessCheck($Access.AllocationFormAddAllocate)"
        type="primary"
        split-button
        @click="$router.push('/stock/WarehouseManagement/AddAllocationForm')"
      >
        新建调拨
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>
            <div class="dropdown-div" @click="getAllAllocate(1)">导出</div>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div v-if="$accessCheck($Access.AllocationFormSearchAllocate)" slot="more">
      <el-form :inline="true" size="small">
        <el-form-item>
          <el-input
            v-model="search_form.keyword"
            placeholder="调拨单号/商品名称"
            style="width: 220px"
            clearable
            @keyup.enter.native="pageChange(1)"
            @clear="pageChange(1)"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="pageChange(1)"
            ></el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="search_form.inStatus"
            placeholder="调入仓库"
            clearable
            style="width: 150px"
            @change="pageChange(1)"
          >
            <el-option
              v-for="item in outWare"
              :key="item.id"
              :label="item.warehouseName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="search_form.outStatus"
            placeholder="调出仓库"
            clearable
            style="width: 150px"
            @change="pageChange(1)"
          >
            <el-option
              v-for="item in outWare"
              :key="item.id"
              :label="item.warehouseName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="search_form.status"
            clearable
            style="width: 150px"
            placeholder="审核状态"
            @change="pageChange(1)"
          >
            <el-option
              v-for="item in order_status"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="search_form.time"
            clearable
            type="daterange"
            value-format="timestamp"
            range-separator="-"
            start-placeholder="调拨开始日期"
            end-placeholder="调拨结束日期"
            @change="LocationFrom"
          ></el-date-picker>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <el-table
      border
      :data="stock_list"
      :span-method="objectSpanMethod"
      @row-dblclick="goDetail"
    >
      <el-table-column
        prop="id"
        align="left"
        label="ID"
        min-width="50"
      ></el-table-column>
      <el-table-column prop="no" align="left" label="单号" min-width="200">
        <template slot-scope="scope">
          <span
            v-if="$accessCheck($Access.AllocationFormGetAllocateInfo)"
            class="click-div"
            @click="goDetail(scope.row)"
          >
            {{ scope.row.no }}
          </span>
          <span v-else>{{ scope.row.no }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="materielCode"
        align="left"
        label="商品编码"
        min-width="140"
      ></el-table-column>
      <el-table-column
        prop="materielName"
        label="商品名称"
        align="left"
        min-width="160"
      ></el-table-column>
      <el-table-column
        v-if="unitFlag"
        prop="unitName"
        label="规格"
        min-width="80"
      >
        <template slot-scope="scope">
          {{ scope.row.unitName }};{{ scope.row.skuName }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="batchFlag"
        align="left"
        label="批次"
        min-width="160"
      >
        <template slot-scope="scope">
          <el-popover
            v-if="scope.row.batch && scope.row.batch.length > 0"
            placement="right"
            width="500"
            trigger="hover"
          >
            <el-table border :data="scope.row.batch">
              <el-table-column
                property="batchNo"
                label="批次"
              ></el-table-column>
              <el-table-column
                property="num"
                label="批次可用库存"
              ></el-table-column>
            </el-table>
            <el-button slot="reference" size="mini">查看</el-button>
          </el-popover>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="calloutFlag"
        prop="warehouseName"
        align="left"
        label="调出仓库"
        min-width="140"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        v-if="foldFlag"
        prop="inWarehouseName"
        align="left"
        label="调入仓库"
        min-width="140"
      ></el-table-column>
      <el-table-column
        v-if="quantityFlag"
        prop="num"
        align="left"
        label="数量"
        min-width="80"
      >
        <template slot-scope="scope">
          {{ $_common.formatNub(scope.row.num) }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="unitPriceFlag"
        prop="costUnitPrice"
        align="left"
        label="单价"
        min-width="80"
      >
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.costUnitPrice) }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="moneyFlag"
        prop="costUnitPrice"
        align="left"
        label="金额"
        min-width="100"
      >
        <template slot-scope="scope">
          {{ $NP.times(scope.row.costUnitPrice, scope.row.num) }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="operationFlag"
        prop="operatorName"
        align="left"
        label="操作人"
        min-width="120"
      ></el-table-column>
      <el-table-column
        v-if="auditStatusFlag"
        prop="auditId"
        align="left"
        label="审核状态"
        min-width="100"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus === 1" class="info-status">
            待审核
          </span>
          <span v-if="scope.row.auditStatus === 2" class="success-status">
            已审核
          </span>
        </template>
      </el-table-column>
      <el-table-column
        header-align="left"
        align="left"
        fixed="right"
        label="操作"
        min-width="160"
      >
        <template slot="header" slot-scope="scope">
          <span v-if="false">{{ scope.$index }}</span>
          <span class="operation">操作</span>
          <el-popover popper-class="custom-table-checkbox" trigger="click">
            <el-checkbox-group v-model="checkList">
              <el-checkbox
                v-for="(item, index) in columns"
                :key="index"
                :label="item.label"
                @change="change"
              ></el-checkbox>
            </el-checkbox-group>
            <el-button
              slot="reference"
              icon="el-icon-setting"
              type="text"
            ></el-button>
          </el-popover>
        </template>
        <template slot-scope="scope">
          <el-button
            v-if="$accessCheck($Access.AllocationFormAuditAllocate)"
            :disabled="parseInt(scope.row.auditStatus) === 2"
            type="text"
            @click="updateAuditStatus(scope.row)"
          >
            审核
          </el-button>
          <el-button
            v-if="$accessCheck($Access.AllocationFormUpdateAllocate)"
            type="text"
            :disabled="parseInt(scope.row.auditStatus) === 2"
            @click="
              $router.push(
                `/stock/WarehouseManagement/EditAllocationForm/${scope.row.id}`
              )
            "
          >
            编辑
          </el-button>
          <el-button
            v-if="$accessCheck($Access.AllocationFormDeleteAllocate)"
            :disabled="parseInt(scope.row.auditStatus) === 2"
            type="text"
            @click="delData(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <FooterPage
      :page-size="pre_page"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
  </ContainerQuery>
</template>

<script>
  import {
    getAllWarehouse,
    getAllAllocate,
    searchAllocate,
    auditAllocate,
    deleteAllocate,
    exportgetAllAllocate,
    exportgetInventoryByWarehouseId,
    getInventoryByWarehouseId,
  } from "@/api/Stock";
  export default {
    name: "AllocationForm",
    data() {
      return {
        spanArr: [],
        pos: 0,
        outWare: [],
        search_form: {
          keyword: "",
          status: "", // 审核状态
          inStatus: "", // 调入仓库
          outStatus: "", // 出库
          singlestatus: "", // 单据状态
          start: "",
          end: "",
        },
        order_status: [
          {
            value: 1,
            label: "待审核",
          },
          { value: 2, label: "已审核" },
        ],
        single_status: [
          { value: 1, label: "暂存" },
          { value: 2, label: "未出库" },
          { value: 3, label: "已出库" },
          { value: 4, label: "已完成" },
        ],
        stock_status: "",
        search_key: "",
        stock_list: [],
        page: 1,
        pre_page: 10,
        total: 0,
        checkList: [
          "规格",
          "调出仓库",
          "调入仓库",
          "数量",
          "单价",
          "金额",
          "操作人",
          "审核状态",
        ],
        columns: [
          {
            label: "规格",
          },
          {
            label: "批次",
          },
          {
            label: "调出仓库",
          },
          {
            label: "调入仓库",
          },
          {
            label: "数量",
          },
          {
            label: "单价",
          },
          {
            label: "金额",
          },
          {
            label: "操作人",
          },
          {
            label: "审核状态",
          },
        ],
        unitFlag: true,
        batchFlag: false,
        calloutFlag: true,
        foldFlag: true,
        quantityFlag: true,
        unitPriceFlag: true,
        moneyFlag: true,
        operationFlag: true,
        auditStatusFlag: true,
        amount: "",
        count: 1,
      };
    },
    created() {
      this.getAllAllocate();
      this.getAllWarehouse();
    },
    activated() {
      if (this.$_isInit()) return;
      this.getData();
    },
    methods: {
      //  获取时间
      LocationFrom(val) {
        if (val && val.length) {
          this.search_form.start = val[0] / 1000;
          this.search_form.end = val[1] / 1000 + 86399;
        } else {
          this.search_form.start = "";
          this.search_form.end = "";
        }
        this.pageChange(1);
      },
      //  请求仓库列表
      async getAllWarehouse() {
        const data = await getAllWarehouse({
          page: 1,
          pageSize: 999,
        });

        this.outWare = data.data;
        // this.search_form.inStatus = data.data[0].id
        // this.search_form.outStatus = data.data[0].id
      },
      //  仓库调拨列表
      async getAllAllocate(exports) {
        let params = {
          page: this.page,
          pageSize: this.pre_page,
        };
        if (exports) {
          params.export = 1;
          const target = await exportgetAllAllocate({
            ...params,
          });
        } else {
          const res = await getAllAllocate({
            ...params,
          });
          this.stock_list = res.data;
          this.total = res.pageTotal;
          // this.amount = res.amount;
          // this.count = res.count;
        }
        // 合并单元格
        const getSpanArr = this.$_common.getSpanArr(this.stock_list, "no");
        this.spanArr = getSpanArr.spanArr;
        this.pos = getSpanArr.pos;
      },
      //  调拨搜索
      async searchAllocate() {
        const { data, pageTotal } = await searchAllocate({
          warehouseId: this.search_form.outStatus,
          inWarehouseId: this.search_form.inStatus,
          auditStatus: this.search_form.status,
          start: this.search_form.start,
          end: this.search_form.end,
          search: this.search_form.keyword,
          page: this.page,
          pageSize: this.pre_page,
        });

        this.stock_list = data;
        this.total = pageTotal;
        // 合并单元格
        const getSpanArr = this.$_common.getSpanArr(this.stock_list, "no");
        this.spanArr = getSpanArr.spanArr;
        this.pos = getSpanArr.pos;
      },
      //  判断
      getData() {
        const isKey = this.$_common.isSerch(this.search_form);
        if (isKey) {
          this.searchAllocate();
        } else {
          this.getAllAllocate();
        }
      },
      objectSpanMethod({ row, column, rowIndex, columnIndex }) {
        if (
          [
            "ID",
            "单号",
            "调出仓库",
            "调入仓库",
            "操作人",
            "审核状态",
            "操作",
          ].includes(column.label)
        ) {
          const _row = this.spanArr[rowIndex];
          const _col = _row > 0 ? 1 : 0;
          return {
            rowspan: _row,
            colspan: _col,
          };
        }
      },
      /** 分页切换 */
      pageChange(page) {
        this.page = page;
        this.getData();
      },
      /** 分页切换 */
      sizeChange(size) {
        this.pre_page = size;
        this.pageChange(1);
      },
      /** 状态筛选 启用或禁用 */
      statusChange() {
        this.pageChange(1);
      },
      //  审核
      async updateAuditStatus(row) {
        this.$confirm("确定要审核通过该单据吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          const data = await auditAllocate(row.id, {
            auditName: this.userName,
          });

          this.$message({
            type: "success",
            message: "审核成功",
          });
          this.getAllAllocate();
        });
      },
      delData(id) {
        this.$confirm("确定要删除该单据吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          const data = await deleteAllocate(id);

          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getAllAllocate();
        });
      },
      goDetail(row) {
        if (!this.$accessCheck(this.$Access.AllocationFormGetAllocateInfo)) {
          return;
        }
        this.$router.push({
          path: `/stock/WarehouseManagement/DetailAllocationForm/${row.id}`,
        });
      },
      change() {
        this.unitFlag = this.checkList.some((item) => item === "规格");
        this.batchFlag = this.checkList.some((item) => item === "批次");
        this.calloutFlag = this.checkList.some((item) => item === "调出仓库");
        this.foldFlag = this.checkList.some((item) => item === "调入仓库");
        this.quantityFlag = this.checkList.some((item) => item === "数量");
        this.unitPriceFlag = this.checkList.some((item) => item === "单价");
        this.moneyFlag = this.checkList.some((item) => item === "金额");
        this.operationFlag = this.checkList.some((item) => item === "操作人");
        this.auditStatusFlag = this.checkList.some(
          (item) => item === "审核状态"
        );
      },
    },
  };
</script>
<style scoped lang="scss">
  .goods-img-div {
    background-color: #f4f4f4;
    width: 60px;
    height: 60px;
    margin-right: 10px;
  }
  .goods-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  .goods-info {
    width: calc(100% - 84px);
  }
  .custom-table-checkbox {
    .el-checkbox {
      display: block !important;
      margin: 0 0 $base-padding/4 0;
    }
  }
</style>
