<template>
  <div class="container">
    <Settings id="mainLeft" :msg="treeData" @handClick="handTreeClick"></Settings>
    <div id="mainRight">
      <div v-if="dhierarchy == 1">
        <div class="button">
          <el-button type="success" @click="addDirectlyLibrary()">新增直属库公司</el-button>
        </div>
        <el-table
          border
          stripe
          :data="granaryData"
          highlight-current-row
          @current-change="handleCurrentChange"
          style="width: 100%"
        >
          <el-table-column type="index" align="center" label="序号"></el-table-column>
          <el-table-column property="dname" label="直属库公司名称" align="center"></el-table-column>
          <el-table-column property="operation" label="操作" align="center">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEditfk(scope.$index, scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="deletefk(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="pageParam.pageIndex"
          :limit.sync="pageParam.pageSize"
          @pagination="getList"
        />
      </div>

      <div v-if="dhierarchy != 1">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="分库列表" name="SubList">
            <div class="button">
              <el-button @click="addSub()" v-show="dhierarchy == 2">新增分库</el-button>
            </div>
            <el-table
              border
              stripe
              :data="granaryDatafk"
              highlight-current-row
              @current-change="handleCurrentChange"
              style="width: 100%"
            >
              <el-table-column type="index" align="center" label="序号"></el-table-column>
              <el-table-column property="dname" label="库级粮库名称" align="center"></el-table-column>
              <el-table-column property="parentName" label="所属直属库" align="center"></el-table-column>
              <el-table-column property="operation" label="操作" align="center">
                <template slot-scope="scope">
                  <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination
              v-show="totalfk > 0"
              :total="totalfk"
              :page.sync="pageParamfk.pageIndex"
              :limit.sync="pageParamfk.pageSize"
              @pagination="getListfk"
            />
          </el-tab-pane>
          <el-tab-pane label="仓房列表" name="WarehouseList">
            <div class="button">
              <el-button @click="addcf()">新增仓房</el-button>
            </div>
            <el-table
              border
              stripe
              :data="warehouseListData"
              highlight-current-row
              @current-change="handleCurrentChange"
              style="width: 100%"
            >
              <el-table-column type="index" align="center" label="序号"></el-table-column>
              <el-table-column property="hname" label="仓房" align="center"></el-table-column>
              <el-table-column property="operation" label="操作" align="center">
                <template slot-scope="scope">
                  <el-button size="mini" @click="cfEdit(scope.$index, scope.row)">修改</el-button>
                  <el-button size="mini" type="danger" @click="cfDelete(scope.$index, scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination
              v-show="totalcf > 0"
              :total="totalcf"
              :page.sync="listQuerycf.page"
              :limit.sync="listQuerycf.limit"
              @pagination="getListcf"
            />
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
  </div>
</template>
<script>
//引入左侧分公司菜单
import Settings from "../../../../layout/components/Settings/index"
import Pagination from "@/components/Pagination"
import WarehouseList from "./subWarehouse/warehouseList"
import { findbyTreeData } from "@/api/branchoff/index.js"
import {
  getzs,
  deletezs,
  getcf,
  deletecf
} from "@/api/organizationalStructureManagement/index.js"
import { getUser } from "@/utils/auth"
export default {
  components: { Pagination, WarehouseList, Settings },
  data() {
    return {
      id: "", // 是从登陆成功以后取过来的
      treeData: [],
      activeName: "SubList",
      //分页
      total: 0,
      totalfk: 0,
      listLoading: true,

      granaryData: [],
      granaryDatafk: [],
      miss: 1,
      queryid: "1",
      pageParam: {
        condition: "",
        pageIndex: 1,
        pageSize: 10
      },
      pageParamcf: {
        condition: "",
        pageIndex: 1,
        pageSize: 10
      },
      pageParamfk: {
        condition: '',
        pageIndex: 1,
        pageSize: 10,
       
      },
      listQuery: {
        page: 1,
        limit: 10,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: "+id"
      },
     
      listQuerycf: {
        page: 1,
        limit: 10,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: "+id"
      },
      totalcf: 0,
      subListData: [],
      warehouseListData: [],
      dhierarchy: ""
    }
  },
  created() {
    this.pageParam.condition = '{ parentId:"' + this.queryid + '" }'
    this.id = JSON.parse(getUser()).depotId
    this.gettreedata()
    this.getList()
    this.getListcf()
    this.getListfk()
  },
  methods: {
    gettreedata() {
      findbyTreeData(this.id).then(res => {
        this.treeData = res.data
        this.dhierarchy = res.data[0].dhierarchy
        console.log(this.dhierarchy, " this.dhierarchy this.dhierarchy")
      })
    },
    getList() {
      getzs(this.pageParam).then(res => {
        this.granaryData = res.data.records
        this.total = res.data.total
      })
    },
    getListfk() {
      getzs(this.pageParamfk).then(res => {
        this.granaryDatafk = res.data.records
        this.totalfk = res.data.total
      })
    },
    // 当表格的当前行发生变化的时候会触发该事件
    handleCurrentChange() {},
    //编辑
    handleEdit(index, row) {
      this.$router.push({
        name: "AddDirectlyLibrary",
        query: {
          id: row.did,
          state: "edit"
        }
      })
    },
    //删除
    handleDelete(index, row) {
      this.$confirm("您确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deletezs(row.did).then(response => {
            this.$message({
              message: "删除成功！",
              type: "success"
            })
            this.getList()
             this.gettreedata()
          })
        })
        .catch(error => {
          console.log(err)
        })
    },
    //新增直属库按钮
    addDirectlyLibrary() {
      if (this.queryid == "") {
        this.$message({
          type: "warning",
          message: "请先选择左侧菜单树"
        })
        return
      }
      this.$router.push({
        name: "AddDirectlyLibrary",
        query: {
          id: this.queryid,
          state: "new"
        }
      })
    },
    //点击仓房，分库tabs时

    handleClick(tab, event) {
      console.log(tab, event)
      // this.$router.push(tab.name);
    },
    //点击左侧树时
    handTreeClick(data) {
      this.dhierarchy = data.dhierarchy
      this.queryid = data.depotCode
      console.log(this.dhierarchy, "this.dhierarchy")
      this.pageParam.condition = '{ parentId:"' + this.queryid + '" }'
      this.pageParamfk.condition = '{ depotId:"' + this.queryid + '",pBelonging:1}'
      // this.pageParamfk.condition = '{ depotId:"' + this.queryid + '"}'
      this.pageParamcf.condition = '{ depotId:"' + this.queryid + '" }'
      if (this.dhierarchy == 1) {
        this.getList()
      } else if (this.dhierarchy == 2) {
        this.getListfk()
          this.getListcf()
      } else if (this.dhierarchy == 3) {
        this.getListcf()
         this.getListfk()
      }
    },

    handleCurrentChange() {},

    //新增分库
    addSub() {
      if (this.queryid == "") {
        this.$message({
          type: "warning",
          message: "请先选择左侧菜单树"
        })
        return
      }
      this.$router.push({
        name: "AddSub",
        query: {
          id: this.queryid,
          state: "new"
        }
      })
    },
    handleEditfk(index, row) {
      this.$router.push({
        name: "AddSub",
        query: {
          id: row.did,
          state: "edit"
        }
      })
    },
    deletefk(index, row) {
      this.$confirm("您确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deletezs(row.did).then(response => {
            this.$message({
              message: "删除成功！",
              type: "success"
            })
            this.getList()
             this.gettreedata()

          })
        })
        .catch(error => {
          console.log(err)
        })
    },
    getListcf() {
      getcf(this.pageParamcf).then(res => {
        this.warehouseListData = res.data.records
        this.totalcf = res.data.total
      })
    },
    addcf() {
      if (this.queryid == "") {
        this.$message({
          type: "warning",
          message: "请先选择左侧菜单树"
        })
        return
      }
      this.$router.push({
        name: "AddWarehouse",
        query: {
          id: this.queryid,
          state: "new"
        }
      })
    },
    cfEdit(index, row) {
      this.$router.push({
        name: "AddWarehouse",
        query: {
          id: this.queryid,
          hid: row.hid,
          state: "edit"
        }
      })
    },
    cfDelete(index, row) {
      this.$confirm("您确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deletecf(row.hid).then(response => {
            this.$message({
              message: "删除成功！",
              type: "success"
            })
            this.getListcf()
             this.gettreedata()
          })
        })
        .catch(error => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  height: 100%;
  display: flex;
  #mainLeft {
    width: 300px;
    height: calc(100vh - 200px);
  }
  #mainRight {
    margin-left: 20px;
    width: calc(100% - 320px);
  }
}
.button {
  text-align: right;
  margin-right: 20px;
  padding-bottom: 20px;
}
</style>