<template>
  <div class="main-contain">
    <div class="position">
      <bread-crumb :crumbs="crumbs"></bread-crumb>
    </div>
    <div class="app-container">
      <div
        style="
          justify-content: space-between;
          margin: 0px 0 12px 0;
          display: flex;
          align-items: center;
        "
      >
        <div>
          <el-date-picker
            size="small"
            v-model="start_time"
            prefix-icon="el-icon-date"
            :editable="false"
            style="width: 196px"
            type="date"
            placeholder="选择日期时间"
            align="right"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            @change="startTimeChange"
          ></el-date-picker
          >-
          <el-date-picker
            size="small"
            v-model="end_time"
            prefix-icon="el-icon-date"
            :editable="false"
            style="width: 196px"
            type="date"
            placeholder="选择日期时间"
            align="right"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            @change="endTimeChange"
          ></el-date-picker>
          <span>审核状态:</span>
          <el-select
            v-model="check_id"
            style="width: 140px; margin-right: 10px"
            placeholder="请选择"
            @change="changeTypeName"
          >
            <el-option
              v-for="item in checkList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <el-input
            size="small"
            style="width: 200px; margin-left: 10px"
            class="filter-item"
            v-model.trim="keywords"
            placeholder="订单编号或供应商名称"
          />
          <el-button
            size="small"
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="search"
            >搜索</el-button
          >
        </div>
        <div>
          <el-button size="small" type="primary" @click="toAdd">新增</el-button>
        </div>
      </div>

      <el-table
        :row-style="{ color: '#303133' }"
        :header-cell-style="{
          backgroundColor: 'rgb(245, 247, 250)',
          color: '#606266',
        }"
        :data="tableList"
        :class="signAndWeighBoxPatients"
        border
        style="width: 100%"
      >
        <el-table-column label="单据日期" align="center" width="100">
          <template slot-scope="scope">
            <span>{{ getTimes(scope.row.document_date) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="单据编号" align="center" width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.number }}</span>
          </template>
        </el-table-column>
        <el-table-column label="供应商" align="center" width="200">
          <template slot-scope="scope">
            <span>{{ getName(scope.row.supplier_id) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="关联采购订单号" align="center" width="200">
          <template slot-scope="scope">
            <span
              v-if="
                scope.row.cancelOrder != undefined &&
                scope.row.cancelOrder.length > 0
              "
              >{{ scope.row.cancelOrder[0].order_number }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="关联采购单号" align="center" width="200">
          <template slot-scope="scope">
            <span
              v-if="
                scope.row.cancelOrder != undefined &&
                scope.row.cancelOrder.length > 0
              "
              >{{ scope.row.cancelOrder[0].good_number }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="采购金额" align="center" width="130">
          <template slot-scope="scope">
            <span v-if="getAllBuyPrice(scope.row.cancelOrder) > 0">{{
              getAllBuyPrice(scope.row.cancelOrder)
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="优惠后金额" align="center" width="130">
          <template slot-scope="scope">
            <span v-if="getAllBuyPrice(scope.row.cancelOrder) > 0">{{
              getAllBuyPriceOne(
                scope.row.cancelOrder,
                scope.row.discount_amount
              )
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="已退款" align="center" width="130">
          <template slot-scope="scope">
            <span v-if="scope.row.payment > 0">{{ scope.row.payment }}</span>
          </template>
        </el-table-column>
        <el-table-column label="数量" align="center" width="120">
          <template slot-scope="scope">
            {{ getCount(scope.row.cancelOrder) }}
          </template>
        </el-table-column>

        <el-table-column label="退款状态" align="center" width="120">
          <template slot-scope="scope">
            <!-- <span v-if="scope.row.is_check == 2">未审核</span>
           <span v-if="scope.row.is_check == 1">已审核</span> -->
          
           <span v-if="scope.row.payment == 0">未退款</span>
           <span v-if="scope.row.payment > 0 && scope.row.payment< getAllBuyPriceOne(scope.row.cancelOrder,scope.row.discount_amount)">部分退款</span>
           <span v-if="scope.row.payment > 0 && scope.row.payment == getAllBuyPriceOne(scope.row.cancelOrder,scope.row.discount_amount)">全部退款</span>
          </template>
        </el-table-column>

        <el-table-column label="制单人" align="center" width="120">
          <template slot-scope="scope">
            {{ getDocName(scope.row.creater) }}
          </template>
        </el-table-column>

        <el-table-column label="审核人" align="center" width="120">
          <template slot-scope="scope">
            {{ getDocName(scope.row.checker) }}
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" width="260" fixed="right">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit-outline"
              size="small"
              type="primary"
              @click="editGoodReturn(scope.row.id)"
              >编辑
            </el-button>
            <el-button
              icon="el-icon-delete"
              size="small"
              type="danger"
              @click="toClickOne(scope.row, scope.$index)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 50, 100, 200, 500, 1000]"
        :page-size="10"
        background
        align="right"
        style="margin-top: 20px"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import BreadCrumb from "@/xt_pages/components/bread-crumb";

import {
  getAllSupply,
  getAllGoodReturnOrderList,
  deleteReturnOrder,
} from "@/api/supply";
import { uParseTime } from "@/utils/tools";
export default {
  name: "stockIn",
  components: {},
  components: {
    BreadCrumb,
  },
  data() {
    return {
      crumbs: [
        { path: false, name: "采购管理" },
        { path: "/good/return/query", name: "退货单" },
      ],
      keywords: "",
      total: 0,
      multipleSelection: [],
      signAndWeighBoxPatients: "sign-and-weigh-box-patients",
      start_time: "",
      end_time: "",
      page: 1,
      limit: 10,
      goodType: [],
      goodInfo: [],
      org_id: 0,
      types: [],
      tableList: [1],
      type_name: "",
      checkList: [
        { id: 0, name: "请选择" },
        { id: 1, name: "已审核" },
        { id: 2, name: "未审核" },
      ],
      supplyList: [],
      check_id: 0,
      doctorList: [],
    };
  },
  methods: {
    toAdd() {
      this.$router.push({ path: "/good/return/add" });
    },
    changeTypeName() {
      this.getlist();
    },
    startTimeChange() {
      this.getlist();
    },
    endTimeChange() {
      this.getlist();
    },
    search() {
      this.getlist();
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getlist();
    },
    getAllSupply() {
      getAllSupply().then((response) => {
        if (response.data.state == 1) {
          var supplyList = response.data.data.supplyList;
          this.doctorList = response.data.data.doctorList;
          for (let i = 0; i < supplyList.length; i++) {
            this.supplyList.push(supplyList[i]);
          }
        }
      });
    },
    getTimes(time) {
      if (time === "") {
        return "";
      }
      return uParseTime(time, "{y}-{m}-{d}");
    },
    getlist() {
      var params = {
        check_id: this.check_id,
        start_time: this.start_time,
        end_time: this.end_time,
        keyword: this.keywords,
        page: this.page,
        limit: this.limit,
      };
      console.log("param23232323", params);
      getAllGoodReturnOrderList(params).then((response) => {
        if (response.data.state == 1) {
          var list = response.data.data.list;
          console.log("list2332232323232", list);
          this.tableList = list;
          var total = response.data.data.total;
          this.total = total;
        }
      });
    },
    getName(id) {
      var name = "";
      for (let i = 0; i < this.supplyList.length; i++) {
        if (id == this.supplyList[i].id) {
          name = this.supplyList[i].supplier_name;
        }
      }
      return name;
    },
    getDocName(id) {
      var user_name = "";
      for (let i = 0; i < this.doctorList.length; i++) {
        if (id == this.doctorList[i].admin_user_id) {
          user_name = this.doctorList[i].user_name;
        }
      }
      return user_name;
    },
    getAllBuyPrice(arr) {
      var buy_price = 0;
      if (arr != undefined && arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          buy_price += arr[i].supply_count * arr[i].price;
        }
      }

      return buy_price.toFixed(2);
    },
    getAllBuyPriceOne(arr, discount_amount) {
      var buy_price = 0;
      if (arr != undefined && arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          buy_price += (parseInt(arr[i].supply_count) * arr[i].price -(parseInt(arr[i].supply_count) * arr[i].price * arr[i].deposit_rate/100));
        }
      }
      return (buy_price - discount_amount).toFixed(2);
    },
    getAllCount(arr) {
      var count = "";
      if (arr != undefined && arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          count += arr[i].supply_count;
        }
      }
      return count;
    },
    editGoodReturn(id) {
      window.sessionStorage.setItem('return_start_time',this.start_time)
      window.sessionStorage.setItem('return_end_time',this.end_time)
      this.$router.push({ path: "/good/return/edit?id=" + id });
    },
    toClickOne(val, index) {
      console.log("val323232323232", val);
      if (val.is_check == 1) {
        this.$message.error("已审核单据，不能删除!");
        return false;
      }
      //  if(val.cancelOrder.length >0 ){
      //    this.$message.error("已关联单据，不能删除!")
      //    return false
      //  }
      this.$confirm("确认删除吗?", "删除", {
        confirmButtonText: "确 定",
        cancelButtonText: "取 消",
        type: "warning",
      })
        .then(() => {
          deleteReturnOrder(val.id).then((response) => {
            if (response.data.state == 1) {
              var msg = response.data.data.msg;
              this.tableList.splice(index, 1);
              this.getlist();
              this.$message.success("删除成功");
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {});
    },
    getCount(val) {
      console.log("val323223322323", val);
      var total = 0;
      if (val != undefined) {
        for (let i = 0; i < val.length; i++) {
          total += val[i].supply_count;
        }
        if (total > 0) {
          return total;
        } else {
          return "";
        }
      }
    },
  },
  created() {
    var now = new Date(); //当前日期
    var nowMonth = now.getMonth(); //当前月
    var nowYear = now.getFullYear(); //当前年
    //本月的开始时间
    var monthStartDate = new Date(nowYear, nowMonth, 1);
    this.start_time = this.getTimes(monthStartDate);
    this.end_time = this.getTimes(new Date());
    this.org_id = this.$store.getters.xt_user.org_id;
    this.getAllSupply();
    var start_time =  window.sessionStorage.getItem('return_start_time')
    var end_time =  window.sessionStorage.getItem('return_end_time')
    if(start_time !=null){
      this.start_time = ""
      this.start_time = start_time
    }
    if(end_time!=null){
      this.end_time = ""
      this.end_time = end_time
    }
    this.getlist();
  },
};
</script>

<style rel="stylesheet/css" lang="scss" scoped>
.information {
  border: 1px #dcdfe6 solid;
  padding: 30px 20px 30px 20px;

  .border {
    border-bottom: 1px #dcdfe6 solid;
    margin: 0px 0 20px 0;
  }
}

.title {
  background: #409eff;
  height: 44px;
  line-height: 44px;
  padding: 0 0 0 10px;
  color: #fff;
  margin: 0 0 10px 0;
}

.edit_separater {
  border-top: 1px solid rgb(233, 233, 233);
  margin-top: 15px;
  margin-bottom: 15px;
}
/deep/ .el-table__body-wrapper::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
</style>

<style>
.sign-and-weigh-box .sign-and-weigh-box-patients .cell {
  font-size: 12px;
}

.sign-and-weigh-box .sign-and-weigh-box-patients .current-row > td {
  background: #6fb5fa;
}

.count {
  color: #bd2c00;
}
.el-table td,
.el-table th.is-leaf,
.el-table--border,
.el-table--group {
  border-color: #d0d3da;
}
.el-table--border::after,
.el-table--group::after,
.el-table::before {
  background-color: #d0d3da;
}

.el-table__fixed-right {
  height: 100% !important;
}
/* .el-table__fixed-body-wrapper {
  height: 100%;
} */

</style>
