<template>
  <el-dialog
    title="农户管理"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="handleClose"
    width="700px"
  >
    <el-form
      ref="baseForm"
      class="base-form"
      label-position="right"
      label-width="95px"
      :model="baseForm"
      :rules="rules"
      size="small"
    >
      <el-row>
        <el-col :span="24" style="background:#F5F7FA;padding-top:20px;margin-bottom:20px;">
          <el-form-item label="所属区域" prop="areaId">
            <el-row>
              <el-col :span="24" style="padding-right:5%">
                <select-tree 
                  :placeholder="'请选择区域'" 
                  style="width:100%"
                  :treeData="treeData"
                  @handlerClick="handlerClick"
                  :checkVal="cusareaName"
                ></select-tree>
              </el-col>
            </el-row>
          </el-form-item>
          <el-col>
            <el-row class="area">
              <el-col :span="5">
                <label>总面积：</label>65446亩
              </el-col>
              <el-col :span="5">
                <label>总水权：</label>65446m³
              </el-col>
              <el-col :span="7" style="width:27%">
                <label>亩产水权：</label>65446亩/m³
              </el-col>
              <el-col :span="6">
                <label>剩余水权：</label>65446m³
              </el-col>
            </el-row>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-row>
            <el-col :span="12">
              <el-form-item label="农户姓名" prop="farmerName">
                <el-row>
                  <el-col>
                    <el-input
                      v-model="baseForm.farmerName"
                      filterable
                      style="width: 100%"
                      placeholder="请输入农户姓名"
                    >
                    </el-input>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="身份证号" prop="farmerIdcard">
                <el-row>
                  <el-col>
                    <el-input
                      v-model="baseForm.farmerIdcard"
                      filterable
                      style="width: 100%"
                      placeholder="请输入身份证号"
                    >
                    </el-input>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="24">
          <el-row>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="farmerPhone">
                <el-row>
                  <el-col>
                    <el-input
                      v-model="baseForm.farmerPhone"
                      filterable
                      style="width: 100%"
                      placeholder="请输入联系电话"
                    >
                    </el-input>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="水权" prop="farmerWramount">
                <el-row>
                  <el-col>
                    <el-input
                      v-model="baseForm.farmerWramount"
                      filterable
                      style="width: 100%"
                      placeholder="请输入水权"
                    >
                    </el-input>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="24">
          <el-row>
            <el-col :span="12">
              <el-form-item label="土地面积" prop="farmerAreasize">
                <el-row>
                  <el-col style="position:relative">
                    <el-input
                      v-model="baseForm.farmerAreasize"
                      filterable
                      style="width: 90%"
                      placeholder="请输入土地面积"
                    >
                    </el-input>
                    <span style="position:absolute;margin-left:10px">亩</span>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
    <div style="text-align: right">
      <el-button type="info" plain @click="resetForm('baseForm')"
        >取消</el-button
      >
      <el-button
        type="primary"
        style="margin-left:16px;"
        @click="submitForm('baseForm')"
        :disabled="dialogSubmitLoading"
        :loading="dialogSubmitLoading"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { assign } from 'lodash-es';
import SelectTree from '@/components/SelectTree';
import { addFarmer,editFarmer } from '@/api/farmer/list.js'

export default {
  name: 'addedFarmers',
  components:{
    SelectTree
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    treeData: {
      type: Array,
      default: () => []
    },
    editFarmerData: {
      type: Object,
      default: () => {}
    },
    isEdit:{
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogSubmitLoading: false,
      dialogVisible: false,
      baseForm: this.editFarmerData,
      hasFetched: false,
      cusareaName: '',
      rules: {
        areaId: [
          { required: true, message: '请选择所属区域', trigger: 'blur' }
        ],
        farmerName: [
          { required: true, message: '请输入农户姓名', trigger: 'blur' }
        ],
        farmerIdcard: [
          { required: true, message: '请输入农户身份证号', trigger: 'blur' }
        ],
        farmerPhone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        farmerAreasize: [
          { required: true, message: '请输入水权', trigger: 'blur' }
        ],
        farmerWramount: [
          { required: true, message: '请输入土地面积', trigger: 'blur' }
        ]
      }
    };
  },
  watch: {
    visible(val) {
      if (val !== this.dialogVisible) {
        this.dialogVisible = val;
        if (val) {
          assign(this.baseForm, this.data);
          if (!this.hasFetched) {
            this.hasFetched = true;
          }
        }
      }
    },
    editFarmerData: {
      handler(val) {
        this.baseForm = val
        this.cusareaName = val.cusareaName
      },
      deep: true
    }
  },
  methods: {
    handlerClick(value) {
      this.baseForm.areaId = value?.cusareaId
    },
    resetForm() {
      this.dialogVisible = false;
    },
    async handlerAddFarmer() {
      if(!this.isEdit){
        const res = await addFarmer(this.baseForm)
        if(res?.code === "000000") {
          this.handleClose()
          this.cusareaName = ''
          this.$emit('refresh')
        }
      }else{
        const res = await editFarmer(this.baseForm)
        if(res?.code === "000000") {
          this.handleClose()
          this.cusareaName = ''
          this.$emit('refresh')
        }
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        this.handlerAddFarmer()
      })
    },
    handleClose() {
      this.$emit('update:visible', false);
      this.resetForm('baseForm');
    },
  }
};
</script>

<style lang="scss" scoped>
.input-number {
  width: 100%;
}
.area{
  margin:0 0 20px 20px;
  label{
    color:#999;
    font-weight: 400;
    font-size: 14px;
  }
}
.base-form {
  max-height: 70vh;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 20px;
}
.history{
  text-align: right;
  line-height: 32px;
  span{
    color:rgb(20, 164, 120);
    cursor: pointer;
  }
}
</style>
<style lang="css" scoped>
::v-deep .el-dialog__header {
  border-bottom: 1px solid #ebeef5;
}
</style>
