<template>
  <ContainerQuery>
    <div slot="left">
      <el-button
        v-if="$accessCheck($Access.LossReportAddLossReport)"
        type="primary"
        size="small"
        @click="
          $router.push({ path: '/stock/WarehouseManagement/AddLossReport' })
        "
      >
        新建报损
      </el-button>
      <!--      <el-button size="small" type="primary">导出</el-button>-->
    </div>
    <div 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.warehouseId"
            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 :data="loss_list" style="width: 100%">
      <el-table-column prop="id" label="ID" min-width="80"></el-table-column>
      <el-table-column prop="name" label="单号" min-width="200">
        <template slot-scope="scope">
          <span
            v-if="$accessCheck($Access.LossReportDetailLossReport)"
            class="click-div"
            @click="goDetail(scope.row)"
          >
            {{ scope.row.no }}
          </span>
          <span v-else class="click-div">
            {{ scope.row.no }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="calloutFlag"
        prop="warehouseName"
        label="仓库"
        min-width="140"
      ></el-table-column>
      <el-table-column
        v-if="quantityFlag"
        prop="skuNum"
        label="sku数量"
        min-width="100"
      ></el-table-column>
      <el-table-column
        v-if="moneyFlag"
        prop="amount"
        label="报损总金额"
        min-width="100"
      >
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.amount) }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="制单日期" min-width="120">
        <template slot-scope="scope">
          {{ $_common.formatDate(scope.row.createTime, "yyyy-MM-dd") }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="auditStatusFlag"
        prop="address"
        label="审核状态"
        min-width="100"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus === 2" class="success-status">
            已审核
          </span>
          <span v-else class="info-status">待审核</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="operationFlag"
        prop="operatorName"
        label="操作人"
        min-width="100"
      ></el-table-column>
      <el-table-column
        header-align="left"
        align="left"
        fixed="right"
        label="操作"
        min-width="120"
      >
        <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.LossReportAuditLossReport)"
            type="text"
            :disabled="parseInt(scope.row.auditStatus) === 2"
            @click="updateAuditStatus(scope.row)"
          >
            审核
          </el-button>
          <!--          <el-button-->
          <!--            type="text"-->
          <!--            @click="$router.push(`/stock/WarehouseManagement/EditLossReport`)"-->
          <!--          >-->
          <!--            编辑-->
          <!--          </el-button>-->
          <el-button
            v-if="$accessCheck($Access.LossReportDeleteLossReport)"
            type="text"
            :disabled="parseInt(scope.row.auditStatus) === 2"
            @click="delData(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
  </ContainerQuery>
</template>

<script>
  import {
    getAllWarehouse,
    getAllReportLoss,
    auditReportLoss,
    deleteReportLoss,
  } from "@/api/Stock";

  export default {
    name: "LossReport",
    data() {
      return {
        search_form: {
          keyword: "",
          warehouseId: "",
          status: "",
          time: [],
          start: "",
          end: "",
        },
        total: 0,
        page: 1,
        pageSize: 10,
        outWare: [],
        order_status: [
          {
            value: 1,
            label: "待审核",
          },
          { value: 2, label: "已审核" },
        ],
        loss_list: [],
        checkList: ["仓库", "sku数量", "报损总金额", "审核状态", "操作人"],
        columns: [
          {
            label: "仓库",
          },
          {
            label: "sku数量",
          },
          {
            label: "报损总金额",
          },
          {
            label: "操作人",
          },
          {
            label: "审核状态",
          },
        ],
        unitFlag: true,
        calloutFlag: true,
        foldFlag: true,
        quantityFlag: true,
        unitPriceFlag: true,
        moneyFlag: true,
        operationFlag: true,
        auditStatusFlag: true,
      };
    },
    created() {
      this.getAllReportLoss();
      this.getAllWarehouse();
    },
    activated() {
      if (this.$_isInit()) return;
      this.getAllReportLoss();
    },
    methods: {
      pageChange(val) {
        this.page = val;
        this.getAllReportLoss();
      },
      sizeChange(val) {
        this.pageSize = val;
        this.pageChange(1);
      },
      //  请求仓库列表
      async getAllWarehouse() {
        const data = await getAllWarehouse({
          page: 1,
          pageSize: 999,
        });

        this.outWare = data.data;
      },
      //  获取时间
      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);
      },
      change() {
        this.calloutFlag = this.checkList.some((item) => item === "仓库");
        this.quantityFlag = this.checkList.some((item) => item === "sku数量");
        this.moneyFlag = this.checkList.some((item) => item === "报损总金额");
        this.operationFlag = this.checkList.some((item) => item === "操作人");
        this.auditStatusFlag = this.checkList.some(
          (item) => item === "审核状态"
        );
      },
      goDetail(row) {
        this.$router.push({
          path: `/stock/WarehouseManagement/LossReportInfo/${row.id}`,
        });
      },
      async getAllReportLoss() {
        const { data, pageTotal } = await getAllReportLoss({
          page: this.page,
          pageSize: this.pageSize,
          warehouseId: this.search_form.warehouseId,
          no: this.search_form.keyword,
          auditStatus: this.search_form.status,
          star: this.search_form.start,
          end: this.search_form.end,
        });

        this.loss_list = data;
        this.total = pageTotal;
      },
      //  审核
      async updateAuditStatus(row) {
        this.$confirm("确定要审核通过该单据吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          const data = await auditReportLoss(row.id, {
            auditName: this.userName,
          });

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

          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getAllReportLoss();
        });
      },
    },
  };
</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 05px 0;
    }
  }
</style>
