<template>
  <el-row class="el-row-container" v-loading="loading">
    <el-col
      :span="24"
      style="padding: 16px; height: 100%"
    >
      <el-card style="height: 100%; overflow-y: auto">
        <el-col :span="24" class="elrow-main__col-top">
          <div style="display:flex">
            <el-select
              style="width: 100%;margin-right:16px;"
              laceholder="请选择您的年份"
              v-model="waterrightYear"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <select-tree 
              :placeholder="'年份'" 
              style="width:100%"
              :treeData="treeData"
              @handlerClick="handlerClick"
              :checkVal="cusareaName"
            ></select-tree>
            <el-button
              style="margin-left: 16px;"
              type="primary"
              size="small"
              @click="getWaterrightList"
              >查询</el-button
            >
          </div>
          <div class="top-left">
            <el-button
              type="primary"
              size="small"
              @click="setAnnualWaterRight"
              >设置年度水权
              </el-button
            ></div>
        </el-col>
        <el-col :span="24" class="elrow-main__col-bottom">
          <!--  -->
          <!-- <b-table
            ref="tableRef"
            :args="{ 'highlight-current-row': true }"
            :data="tableData"
            :columns="columns"
            isShowIndex
          >
            <template #operate="scope">
              <el-link
                type="primary"
                :underline="false"
                @click="goDetail(scope.row)"
                >修改</el-link
              >
            </template>
          </b-table> -->
        <el-table
          ref="tableRef"
          :args="{ 'highlight-current-row': true }"
          :data="tableData"
          row-key="id"
          :columns="columns"
          isShowIndex
          :tree-props="{children: 'children',hasChildren: 'hasChildren'}"
          :header-row-style="{ background: '#E5EEED' }"
          :header-cell-style="{ background: '#E5EEED' }"
        >
          <el-table-column
            v-for="item in columns"
            :key="item.id"
            :prop="item.prop"
            :label="item.label"
          >
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-link
                size="small"
                :underline="false"
                type="primary"
                style="margin-right: 10px"
                @click="handlerRecompose(scope.row)"
                >修改</el-link
              >
            </template>
            
        </el-table-column>
        </el-table>
        </el-col>
      </el-card>
    </el-col>
    <recompose
      :visible.sync="recomposeShow"
      :editRow="editRow"
      @resetEdit="resetEdit"
      @refresh="getWaterrightList"
    />
    <water-price-setting :visible.sync="waterprice" @refresh="getWaterrightList"/>
  </el-row>
</template>

<script>
import { getTree } from '@/api/tree.js'
import { getWaterrightList,editWaterright,getWaterrightUpareaInfo } from '@/api/waterright/index.js'
import SelectTree from '@/components/SelectTree';
import BTable from '@/components/Table/index.vue';
import setAnnualWaterRight from './components/setAnnualWaterRight.vue'
import waterPriceSetting from './components/waterPriceSetting.vue'
import recompose from './components/recompose.vue'
import { assign, omit } from 'lodash-es';

export default {
  name: 'waterEstablishment',
  components: { BTable,setAnnualWaterRight, recompose,waterPriceSetting,SelectTree },
  data() {
    return {
      form: {
        productName: ''
      },
      options:[
        {
          value: '2023',
          label: '2023'
        }, {
          value: '2022',
          label: '2022'
        }, {
          value: '2021',
          label: '2021'
        }, {
          value: '2020',
          label: '2020'
        }, {
          value: '2019',
          label: '2019'
        }
      ],
      treeData: [],
      loading: false,
      recomposeShow: false,
      waterprice: false,
      cusareaName:'',
      waterrightYear: '2023',
      areaId:'',
      editRow: {},
      columns: [
        {
          label: '行政区',
          prop: 'cusareaName',
          customRender: 'cusareaName',
          align: 'center'
        },
        {
          label: '灌区总面积（亩）',
          prop: 'waterrightAreasize',
          customRender: '',
          align: 'center'
        },
        {
          label: '初始水权（m³）',
          prop: 'waterrightAmount',
          customRender: '',
          align: 'center'
        },{
          label: '亩均水权（亩/立方）',
          prop: 'phone',
          customRender: 'Permuarea',
          align: 'center'
        }
      ],
      tableData: []
    };
  },
  mounted() {
    this.getWaterrightList()
  },
  methods: {
    resetEdit() {
      this.editRow = {}
    },
    async getWaterrightList() {
      if(!this.areaId) {
        const treeList = this.$store.state.tree.treeList
        let res = []
        if (treeList.code === '000000') {
          res = treeList
        } else {
          res = await getTree()
          this.$store.dispatch('tree/setTree', res)
        }
        this.treeData = res?.data
        this.cusareaName ||= this.treeData[0].cusareaName
        this.areaId = this.treeData[0].cusareaId
      }
      this.getWaterrightListHandler()
    },
    async getWaterrightListHandler() {
      const res = await getWaterrightList({
        areaId: this.areaId,
        waterrightYear: this.waterrightYear
      })
      this.tableData = res?.data
    },
    handlerClick(value) {
      this.areaId = value?.cusareaId
    },
    setAnnualWaterRight() {
      this.waterprice = true
    },
    loadData(parameter) {
      const queryform = this.form;
      const payload = omit(assign({}, parameter, queryform, {orderByColumn:'tarcecodeapplyCreateddate',isAsc:'desc'}), []);
      return this.getTableData(payload);
    },
    getTableData(payload) {
      return {}
    },
    handlerRecompose(row) {
      this.editRow = row
      this.recomposeShow = true
    }
  }
};
</script>

<style lang="scss" scoped>
.el-row-container{
  height: 100%;
}
.elrow-main__col-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;
  .top-left {
    height: 100%;
    display: flex;
    align-items: center;
    i {
      background-color: #40d5ec;
      height: 45%;
      width: 5px;
      margin-right: 5px;
    }
  }
}
</style>
