<template>
  <el-row class="el-row-container" style="height: 100%" v-loading="loading">
    <el-col
      :span="4"
      style="height:100%;padding-bottom:32px;"
    >
    <el-card style="margin:16px;height: 100%; overflow-y: auto">
      <data-report-left
        @setCurrentData="setCurrentData"
        :treeData="treeData"
      ></data-report-left>
    </el-card>
    </el-col>
    <el-col
      :span="20"
      style="padding: 16px 16px 16px 0; height: 100%"
    >
      <el-card style="height: 100%; overflow-y: auto">
        <el-col :span="24" class="elrow-main__col-top">
          <div>
            <el-input
              v-model="farmerName"
              style="width: 250px;margin-right:16px;"
              placeholder="请输入姓名/电话"
              @keyup.enter.native="handleSearch"
              clearable
            />
            <el-button
              type="primary"
              size="small"
              @click="handleSearch"
              >查询</el-button
            >
          </div>
          <div class="top-left">
            <el-button
              type="primary"
              size="small"
              @click="addAssociation"
              >新增农户
              </el-button
            ></div>
        </el-col>
        <el-col :span="24" class="elrow-main__col-bottom">
          <b-table
            ref="tableRef"
            :args="{ 'highlight-current-row': true }"
            :data="loadData"
            :columns="columns"
            isShowIndex
          >
            <template #styleImg="scope">
              <el-image
                :src="scope.row.imgPreview"
                :preview-src-list="[scope.row.imgPreview]"
                slot="error"
                class="image-slot"
                style="width: 100px; height: 50px"
              >
                <div slot="error" class="image-slot"></div>
              </el-image>
            </template>
            <template #codeNo="scope">
              <el-link
                type="primary"
                :underline="false"
                @click="goDetail(scope.row)"
                >{{ scope.row.traceCodeApplyConcat }}</el-link
              >
            </template>
            <template #status="scope">
              <span style="display:flex;align-items:center;justify-content:center">
                <span class="status"></span>
                {{ scope.row.name }}
              </span>
            </template>
            <template #useNumber="scope">
              {{ scope.row.useNumber + '/' + scope.row.totalNumber }}
            </template>
            <template #finishGoodName="scope">
              {{ scope.row.finishGoodName&&scope.row.finishgoodNo?scope.row.finishGoodName + '-' + scope.row.finishgoodNo:'' }}
            </template>

            <template #operate="scope">
              <el-link
                size="small"
                :underline="false"
                type="primary"
                style="margin-right: 10px"
                @click="irrigationShowHistory(scope.row)"
                >灌溉记录</el-link
              >
              <el-link
                size="small"
                :underline="false"
                type="primary"
                style="margin-right: 10px"
                @click="editFarmerHandler(scope.row)"
              >修改</el-link>
              <el-link
                size="small"
                :underline="false"
                type="primary"
                style="margin-right: 10px"
                @click="showDetails(scope.row)"
              >详情</el-link>
              <el-link
                slot="reference"
                size="small"
                :underline="false"
                type="danger"
                style="margin-right: 10px"
                @click="deleteRow(scope.row)"
              >删除</el-link>
            </template>
          </b-table>
        </el-col>
      </el-card>
    </el-col>
  <added-farmers
    :visible.sync="associationManageShow"
    :treeData="treeData"
    :editFarmerData="editFarmerData"
    :isEdit="isEdit"
    @refresh="handleSearch"
  />
  <farmer-management 
    :visible.sync="farmerManageShow"
    :detailsData="detailsData"
  />
  <irrigation-record :visible.sync="irrigationShow" />
  </el-row>
</template>

<script>
import { Message } from 'element-ui';
import { getTree } from '@/api/tree.js'
import { getFarmerList,deleteFarmer,getFarmerDetail } from '@/api/farmer/list.js'
import BTable from '@/components/Table/index.vue';
import DataReportLeft from '@/components/DataReportLeft/index.vue'
import AddedFarmers from './components/addedFarmers.vue'
import FarmerManagement from './components/farmerManagement.vue';
import IrrigationRecord from './components/IrrigationRecord.vue';

export default {
  name: 'peasantHouseholdManage',
  components: { BTable,DataReportLeft,AddedFarmers,FarmerManagement,IrrigationRecord },
  data() {
    return {
      visibles: false,
      farmerName:'',
      detailsData: {},
      treeData: [],
      currentClick:{},
      dateValue:"",
      imgUrl: '',
      baseImageUrl: process.env.VUE_APP_BASE_RESOURCE_PREFIX,
      form: {
        productName: ''
      },
      isEdit: false,
      loading: false,
      associationManageShow: false,
      farmerManageShow: false,
      irrigationShow: false,
      editFarmerData: {},
      farmerId: '',
      columns: [
        {
          label: '农户姓名',
          prop: 'farmerName',
          customRender: '',
          align: 'center'
        },
        {
          label: '农户身份证',
          prop: 'farmerIdcard',
          customRender: '',
          align: 'center',
          width: 160
        },
        {
          label: '联系电话',
          prop: 'farmerPhone',
          customRender: '',
          align: 'center'
        },{
          label: '种植面积（亩）',
          prop: 'farmerAreasize',
          customRender: '',
          align: 'center'
        },{
          label: '水权数量（立方）',
          prop: 'farmerWramount',
          customRender: '',
          align: 'center'
        },{
          label: '剩余水权（立方）',
          prop: 'farmerWrbalance',
          customRender: '',
          align: 'center'
        },
        {
          label: '操作',
          customRender: 'operate',
          width: 200,
          align: 'center'
        }
      ],
    };
  },
  methods: {
    editFarmerHandler(row) {
      this.associationManageShow = true
      this.isEdit = true
      this.editFarmerData = row
    },
    async getDetail(farmerId) {
      const res  = await getFarmerDetail({
        farmerId
      })
      this.detailsData = res?.data
    },
    async loadData() {
      if(!this.currentClickId ) {
        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.currentClick = res?.data[0]
        this.currentClickId = this.currentClick?.cusareaId
      }
      return this.getFarmerList(this.farmerName, this.currentClickId)
    },
    getFarmerList(farmerName,areaId) {
      return getFarmerList({
        farmerName,
        areaId
      })
    },
    setCurrentData(areaId) {
      this.currentClickId = areaId
      this.$refs.tableRef.refresh(false);
    },
    closePopOver(flag) {
      this.visible = flag
    },
    irrigationShowHistory() {
      this.irrigationShow = true
    },
    // 详情
    showDetails(row){
      this.farmerManageShow = true
      this.farmerId = row.farmerId
      this.getDetail(row.farmerId)
    },
    addAssociation() {
      this.isEdit = false
      this.editFarmerData = {}
      this.associationManageShow = true
    },
    handleSearch() {
      this.$refs.tableRef.refresh(true);
    },
    goDetail(row) {
      this.$refs.sourceCodeDetail.open(row.tarcecodeapplyBatchno)
    },
    download(row) {
      this.visibles = true
    },
    deleteRow(row) {
      this.$modal
      .confirm(`是否确认删除农户${row.farmerName}`)
      .then(()=> {
        deleteFarmer({
          farmerIds: row.farmerId
        }).then(() => {
          Message({ message: "删除成功", type: 'success' });
          this.refresh()
        })
      })
    }
  }
};
</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;
    }
  }
}
.status{
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: red;
  margin-right: 5px;
}
</style>
