<template>
  <!-- 退货 -->
  <div style="display: flex">
    <el-card style="width: 18%; margin-right: 18px; border-radius: 6px">
      <el-tabs v-model="activeNametree" type="card" @tab-click="tabClick" stretch>
        <!-- <el-tab-pane label="按分类" name="grp"></el-tab-pane> -->
        <el-tab-pane label="按机构" name="org"></el-tab-pane>
      </el-tabs>
      <el-tree
        :data="data"
        :props="defaultProps"
        node-key="spdId"
        @node-click="handleNodeClick"
        default-expand-all="true"
        style="margin-top: 10px"
      >
      <span class="custom-tree-node" slot-scope="{ node, data }">
          <div>{{node.label}}</div>
          <div class="redTit">{{data.wqrnum}}</div> 
        </span>
      </el-tree>
    </el-card>
    <div class="right-common-box">
      <el-form :model="listQuery" ref="queryForm" :inline="true">
        <el-form-item label="退货单号" prop="id">
          <el-input v-model="listQuery.id" placeholder="请输入退货单号"></el-input>
        </el-form-item>
        <el-form-item label="当前状态" prop="stas">
          <el-select v-model="listQuery.stas" placeholder="请选择" clearable>
            <el-option
              v-for="item in stasOptions"
              :key="item.value"
              :label="item.lable"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提交时间" prop="docmkDate">
          <el-date-picker
            v-model="listQuery.docmkDate"
            type="daterange"
            range-separator="至"
            :editable="false"
            value-format="yyyy-MM-dd"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getorderData('search')">查询 </el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="listLoading"
        element-loading-text="加载中"
        :data="list"
        fit
        stripe
        height="500"
        border
      >
        <template slot="empty">
          <img src="@/assets/nopage.png" alt />
          <p>暂无数据</p>
        </template>
        <el-table-column fixed type="index" label="序号" width="60" />
        <el-table-column label="退货单号" prop="id" />
        <el-table-column label="品总数" prop="detlCnt" />
        <el-table-column label="退货总数" prop="retnCnt" />
        <el-table-column label="退货金额" prop="retnAmt" />
        <el-table-column label="客户名称" prop="branchName" />
        <el-table-column label="发起人" prop="docmker" />
        <el-table-column label="发起时间" prop="docmkDate" />
        <el-table-column label="当前状态" prop="stas">
          <template #default="{ row }">
            <span v-if="row.stas == 'A'">待确认</span>
            <span v-if="row.stas == 'B'">已确认</span>
            <span v-if="row.stas == 'R'">已拒绝</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="handleDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <yl-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.current"
        :limit.sync="listQuery.size"
        @pagination="getorderData"
      />
    </div>
  </div>
</template>

<script>
import ylPagination from "@/components/yl-pagination";
import { selectRetnOrdD } from "@/api/productreturnmanage-sup/returnList";
import { selectSpdList} from "@/api/orderManage-sup/index";
export default {
  components: {
    ylPagination,
  },
  data() {
    return {
      activeNametree: "org",
      data: [],
      spdId: null,
      defaultProps: {
        children: "children",
        label: "spdName",
      },
      listQuery: {
        current: 1,
        size: 10,
        id: "",
        orgId: "",
        stas: "",
        docmkDate: [],
      },
      stasOptions: [
        { value: "A", lable: "待确认" },
        { value: "B", lable: "已确认" },
      ],
      listLoading: false,
      list: [],
      total: 0,
      routespdId:"",
    };
  },
  mounted() {
    this.getData();
    if (this.$route.query.spdId) {
      this.listQuery.spdId = this.$route.query.spdId;
      selectRetnOrdD(this.listQuery)
        .then((res) => {
          this.list = res.data.records;
          this.total = res.data.total;
          this.listLoading = false;
        })
        .catch((err) => {
          this.listLoading = false;
        });
    };
    if(this.$route.query.msgRouteUni && this.$route.query.spdId){
      this.listQuery.spdId = this.$route.query.spdId;
      this.listQuery.id = this.$route.query.msgRouteUni;
      this.getorderData()
    }
  },
  methods: {
    tabClick(tab) {
      if (tab.label == "按机构") {
        this.defaultProps.label = "spdName";
      }
      // else if(tab.label == "按分类"){
      //     // this.getData4();
      //     // this.defaultProps.label = 'orgGrpName'
      //     // this.listQuery.type = 'grp';
      // }
    },
    // 点击获取耗材列表
    handleNodeClick(data) {
      this.listLoading = true;
      this.spdId = data.spdId;
      this.listQuery.spdId = this.spdId;
      selectRetnOrdD(this.listQuery)
        .then((res) => {
          this.list = res.data.records;
          this.total = res.data.total;
          this.listLoading = false;
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },
    // 获取医院列表
    getData(type) {
      this.data = [];
      selectSpdList()
        .then((res) => {
          this.listQuery.spdId = res.data[0].spdId;
          this.data = res.data;
          // console.log(this.data, "dadad");
        })
        .catch((err) => {});
    },
    getorderData(type) {
      if (type == "search") {
        this.listQuery.current = 1;
      }
        this.listQuery.docmkDateStart = this.listQuery.docmkDate[0];
        this.listQuery.docmkDateEnd = this.listQuery.docmkDate[1];
      this.listLoading = true;
      selectRetnOrdD(this.listQuery)
        .then((res) => {
          this.list = res.data.records;
          this.total = res.data.total;
          this.listLoading = false;
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },
    reset() {
      this.$refs.queryForm.resetFields();
      this.getorderData();
    },
    // 详情
    handleDetail(row) {
      // console.log(row,"row")
      if (this.spdId) {
        this.routespdId = this.spdId;
      } else {
        this.routespdId = this.$route.query.spdId;
      }
      if (row.stas == "A") {
        this.$router.push({
          name: "returnDetail",
          query: {
            id: row.id,
            detlCnt:row.detlCnt,
            retnCnt:row.retnCnt,
            retnAmt:row.retnAmt,
            branchName:row.branchName,
            docmker:row.docmker,
            docmkDate:row.docmkDate,
            stas: row.stas,
            spdId:this.routespdId,
            rejtRea:row.rejtRea
          },
        });
      } else {
        this.$router.push({
          name: "returncomDetail",
          query: {
            id: row.id,
            detlCnt:row.detlCnt,
            retnCnt:row.retnCnt,
            retnAmt:row.retnAmt,
            branchName:row.branchName,
            docmker:row.docmker,
            docmkDate:row.docmkDate,
            stas: row.stas,
            spdId:this.routespdId,
            rejtRea:row.rejtRea
          },
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.custom-tree-node{
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
.custom-tree-node ::v-deep .redTit{
  min-width: 30px;
  background-color: #f56c6c;
  min-height: 1px;
  border-radius: 10px;
  text-align: center;
  margin-left: 10px;
  padding: 1px;
  color: #fff;
}
</style>
