<template>
  <ContainerQuery>
    <div
      v-if="$accessCheck($Access.ReturnWarehousingOrderSearchAllOrderReturn)"
      slot="left"
    >
      <el-dropdown
        v-if="$accessCheck($Access.ReturnWarehousingOrderAddOrderReturn)"
        split-button
        type="primary"
        @click="$router.push(`/order/manageO/AddWareOrder`)"
      >
        新增退货
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item
            v-if="$accessCheck($Access.ReturnWarehousingOrderGetAllOrderReturn)"
          >
            <div class="dropdown-div" @click="getData(1)">导出</div>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-button
        v-else-if="
          $accessCheck($Access.ReturnWarehousingOrderGetAllOrderReturn)
        "
        size="small"
        type="primary"
        plain
        @click="getData(1)"
      >
        导出
      </el-button>
    </div>
    <div
      v-if="$accessCheck($Access.ReturnWarehousingOrderSearchAllOrderReturn)"
      slot="more"
    >
      <el-form :inline="true" size="small">
        <el-form-item>
          <el-input
            v-model="search_form.keyWord"
            clearable
            style="width: 250px"
            placeholder="订单号/退货单号/商品名称"
            @keyup.enter.native="pageChange(1)"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="pageChange(1)"
            ></el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <SelectShop
            v-model="search_form.shopId"
            placeholder="所属商铺"
            width="150"
            @clear="clearShop"
            @change="selShop"
          />
        </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="returWare"
          ></el-date-picker>
        </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-select
            v-model="search_form.inStatus"
            clearable
            style="width: 150px"
            placeholder="入库状态"
            @change="pageChange(1)"
          >
            <el-option
              v-for="item in single_status"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="search_form.staff"
            style="width: 150px"
            clearable
            placeholder="业务员"
            @blur="search_form.staff = ''"
          >
            <i
              slot="suffix"
              class="el-input__icon el-icon-search"
              @click="staff_show = true"
            ></i>
          </el-input>
        </el-form-item>
        <!--        <el-form-item>-->
        <!--          <el-input-->
        <!--            v-model="goods"-->
        <!--            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>
    </div>
    <el-table :data="tableData" @row-dblclick="goDetail">
      <el-table-column
        prop="id"
        fixed="left"
        label="ID"
        min-width="50"
      ></el-table-column>
      <el-table-column
        prop="no"
        fixed="left"
        label="退货单号"
        min-width="160"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span
            v-if="$accessCheck($Access.ReturnWarehousingOrderGetOrderReturn)"
            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="customerName"
        label="客户名称"
        min-width="160"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="num"
        label="退货数量"
        min-width="100"
      ></el-table-column>
      <el-table-column
        v-if="returnMoneyFlag"
        prop="returnTotalPrice"
        label="退货金额"
        min-width="100"
      >
        <template slot-scope="scope">
          {{ $_common.formattedNumber(scope.row.returnTotalPrice, 2) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="originNo"
        label="订单编号"
        min-width="160"
      ></el-table-column>
      <el-table-column prop="createTime" label="创建时间" min-width="160">
        <template slot-scope="scope">
          {{ $_common.formatDate(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="auditStatus" label="审核状态" min-width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus === 1" class="warning-status">
            待审核
          </span>
          <span v-if="scope.row.auditStatus === 2" class="success-status">
            已审核
          </span>
          <span v-if="scope.row.auditStatus === 3" class="info-status">
            已驳回
          </span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="timeFlag"
        prop="auditTime"
        label="审核时间"
        min-width="160"
      >
        <template slot-scope="scope">
          {{
            scope.row.auditStatus === 2
              ? $_common.formatDate(scope.row.auditTime)
              : "--"
          }}
        </template>
      </el-table-column>
      <el-table-column prop="auditStatus" label="入库状态" min-width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.inStatus === 4" class="info-status">
            未入库
          </span>
          <span v-else class="success-status">已入库</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" min-width="180">
        <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.ReturnWarehousingOrderGetOrderReturn) &&
              $accessCheck($Access.ReturnWarehousingOrderAuditOrderReturn)
            "
            :disabled="~~scope.row.auditStatus !== 1"
            type="text"
            @click="
              $router.push(`/order/manageO/EditWareOrder/${scope.row.id}`)
            "
          >
            编辑
          </el-button>
          <el-button
            v-if="$accessCheck($Access.ReturnWarehousingOrderUpdateOrderReturn)"
            :disabled="~~scope.row.auditStatus !== 1"
            type="text"
            @click="updateAuditStatus(scope.row)"
          >
            审核
          </el-button>
          <el-dropdown>
            <span class="el-dropdown-link">
              更多
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-if="
                  $accessCheck(
                    $Access.ReturnWarehousingOrderUpdateOrderReturn
                  ) && scope.row.auditStatus !== 2
                "
                :disabled="~~scope.row.auditStatus !== 1"
              >
                <div class="dropdown-div" @click="refuseOrder(scope.row)">
                  驳回
                </div>
              </el-dropdown-item>
              <el-dropdown-item
                v-if="
                  $accessCheck(
                    $Access.ReturnWarehousingOrderDeleteOrderReturn
                  ) && scope.row.auditStatus !== 2
                "
              >
                <div class="dropdown-div" @click="del(scope.row.id)">删除</div>
              </el-dropdown-item>
              <el-dropdown-item>
                <div class="dropdown-div" @click="openUrl(scope.row.id)">
                  打印
                </div>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!--    业务员-->
    <staffListModal
      v-if="staff_show"
      :is-check="false"
      :is-show="staff_show"
      @cancel="staff_show = false"
      @confirm="staffSel"
    />
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
  </ContainerQuery>
</template>

<script>
  import staffListModal from "@/component/common/staffListModal";
  import {
    searchAllOrderOut,
    exportSearchAllOrderOut,
    exportGetAllOrderOut,
    getAllOrderOut,
    auditOrderOut,
    deleteOrderOut,
    rejectOrderReturn,
  } from "@/api/Order";
  import SelectShop from "@/component/goods/SelectShop.vue";

  export default {
    name: "ReturnWarehousingOrder",
    components: {
      staffListModal,
      SelectShop,
    },
    data() {
      return {
        goods: "",
        tableData: [],
        staff_show: false,
        pageSize: 10,
        page: 1,
        total: 0,
        outWare: [],
        order_status: [
          { value: 2, label: "已审核" },
          { value: 1, label: "待审核" },
          { value: 3, label: "已驳回" },
        ],
        single_status: [
          { value: 4, label: "未入库" },
          { value: 5, label: "已入库" },
        ],
        search_form: {
          shopId: "",
          shopName: "",
          wareStatus: "",
          inStatus: "",
          start: "",
          end: "",
          keyWord: "",
          customer: "",
          customer_id: "",
          operatorId: "",
          staff: "",
          status: "",
          // categoryId: [],
        },
        checkList: [
          "商铺",
          "规格",
          "数量",
          "销售单价",
          "销售金额",
          "退货单价",
          "退货金额",
          "操作人",
          "审核时间",
        ],
        columns: [
          {
            label: "商铺",
          },
          {
            label: "规格",
          },
          {
            label: "数量",
          },
          {
            label: "销售单价",
          },
          {
            label: "销售金额",
          },
          {
            label: "退货单价",
          },
          {
            label: "退货金额",
          },
          {
            label: "操作人",
          },
          {
            label: "审核时间",
          },
        ],
        shopFlag: true,
        unitFlag: true,
        quantityFlag: true,
        unitPriceFlag: true,
        moneyFlag: true,
        returnFlag: true,
        returnMoneyFlag: true,
        personFlag: true,
        timeFlag: true,
      };
    },
    created() {
      this.getAllOrderOut();
    },
    activated() {
      if (this.$_isInit()) return;
      this.getData();
    },
    methods: {
      // 打印
      openUrl(id) {
        let routeData = this.$router.resolve({
          path: `/ReturnOrderPrinting/${id}`,
        });
        window.open(routeData.href, "_blank");
      },
      //  退货单搜索
      async searchAllOrderOut(exports) {
        let params = {
          inStatus: this.search_form.inStatus,
          shopId: this.search_form.shopId,
          operatorId: this.search_form.operatorId,
          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.pageSize,
          goodsName: this.goods,
        };
        if (exports) {
          params.export = 1;
          const target = await exportSearchAllOrderOut(params);
        } else {
          const { data, pageTotal } = await searchAllOrderOut(params);
          this.tableData = data;
          this.total = pageTotal;
        }
      },
      //  请求退货单列表
      async getAllOrderOut(exports) {
        let params = {
          page: this.page,
          pageSize: this.pageSize,
        };
        if (exports) {
          params.export = 1;
          const target = await exportGetAllOrderOut(params);
        } else {
          const { data, pageTotal } = await getAllOrderOut(params);

          this.tableData = data;
          this.total = pageTotal;
        }
      },
      getData(exports) {
        const isKey = this.$_common.isSerch(this.search_form);
        if (isKey) {
          this.searchAllOrderOut(exports);
        } else {
          this.getAllOrderOut(exports);
        }
      },
      //驳回
      refuseOrder(row) {
        this.$prompt("请输入驳回原因", "驳回退货", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
        }).then(async ({ value }) => {
          if (!value) {
            this.$message.warning("驳回原因不能为空");
            return;
          }
          const { data } = await rejectOrderReturn({
            id: row.id,
            auditFailReason: value,
          });
          this.$message({
            type: "success",
            message: "驳回原因: " + value,
          });
          this.pageChange(1);
        });
      },
      //  审核
      async updateAuditStatus(row) {
        this.$confirm("确定要审核通过该单据吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          const data = await auditOrderOut(row.id, {
            auditName: this.userName,
          });

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

          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getData();
        });
      },
      //  选择时间
      returWare(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);
      },
      // 选择员工
      staffSel(val) {
        const row = val[0];
        this.search_form.operatorId = row.id;
        this.search_form.staff = row.staffName;
        this.pageChange(1);
      },
      //  选择商铺
      selShop(val, row) {
        // this.search_form.shopId = row[0].id
        this.search_form.shopName = row[0].name;
        this.pageChange(1);
      },
      clearShop() {
        this.search_form.shopId = "";
        this.search_form.shopName = "";
        this.pageChange(1);
      },
      pageChange(page) {
        this.page = page;
        this.getData();
      },
      sizeChange(size) {
        this.pageSize = size;
        this.pageChange(1);
      },
      goDetail(row) {
        if (
          !this.$accessCheck(this.$Access.ReturnWarehousingOrderGetOrderReturn)
        ) {
          return;
        }
        this.$router.push(`/order/manageO/LookWareOrder/${row.id}`);
      },
      change() {
        this.shopFlag = this.checkList.some((item) => item === "商铺");
        this.unitFlag = 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.returnFlag = this.checkList.some((item) => item === "退货单价");
        this.returnMoneyFlag = this.checkList.some(
          (item) => item === "退货金额"
        );
        this.personFlag = this.checkList.some((item) => item === "操作人");
        this.timeFlag = this.checkList.some((item) => item === "审核时间");
      },
    },
  };
</script>

<style scoped lang="scss">
  .num-ul > li {
    padding-right: 10px;
  }
  .custom-table-checkbox {
    .el-checkbox {
      display: block !important;
      margin: 0 0 $base-padding/4 0;
    }
  }
</style>
