<template>
  <el-drawer v-model="_visible" title="土地信息" :size="800" @close="handleClose">
    <el-card>
      <div class="title_">基础信息</div>
      <el-form ref="baseForm" :model="baseInfo" class="base_form" label-width="120px">
        <el-form-item label="土地名称" prop="landName">
          <el-input v-model="baseInfo.landName" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="所属网格" prop="gridId">
          <CustomSelect
            v-model:value="baseInfo.gridId"
            :set="{
              url: '/land-resource/gridManage/page',
              props: {
                value: 'id',
                label: 'gridName',
              },
            }"
            placeholder="请选择所属网格"
          />
        </el-form-item>
        <el-form-item label="用地分类" prop="LandType">
          <LandClassificationType v-model:value="baseInfo.landClassificationType" placeholder="请选择用地分类" />
        </el-form-item>
        <el-form-item label="位置" prop="villageCode">
          <el-input v-model="baseInfo.villageCode" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="是否土地流转" prop="isTransfer">
          <LandIsTranfer v-model:value="baseInfo.isTransfer" />
        </el-form-item>
        <el-form-item label="面积" prop="area">
          <el-input v-model="baseInfo.area" placeholder="请输入面积"></el-input>
        </el-form-item>
        <el-form-item label="产权人" prop="owner">
          <el-input v-model="baseInfo.owner" placeholder="请输入产权人姓名"></el-input>
        </el-form-item>
        <el-form-item label="土壤类型">
          <el-input v-model="baseInfo.soilType" placeholder="请输入土壤类型"></el-input>
        </el-form-item>
      </el-form>
    </el-card>
    <br />
    <el-card>
      <div class="title_">土地产权信息</div>
      <el-form ref="propertyForm" :model="propertyInfo" class="property_form" label-width="120px">
        <el-form-item label="产权人姓名" prop="propertyName">
          <el-input v-model="propertyInfo.propertyName" placeholder="请输入联系人"></el-input>
        </el-form-item>
        <el-form-item label="产权人联系电话" prop="propertyPhone">
          <el-input v-model="propertyInfo.propertyPhone" placeholder="请输入联系人"></el-input>
        </el-form-item>
        <el-form-item label="产权编号" prop="landCode">
          <el-input v-model="propertyInfo.landCode" placeholder="请输入联系人"></el-input>
        </el-form-item>
        <!-- <el-form-item label="产权证书" prop="propertyCertificateUrl">
          <el-upload class="custom-form__uploader" action="#" :show-file-list="false" accept="image/*" :limit="20" :http-request="rowUploadPicture">
            <el-icon class="custom-form__uploader__icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item> -->
      </el-form>
    </el-card>
    <section class="btns">
      <el-button type="primary" @click="handleSubmit">保存并提交审核</el-button>
      <el-button type="warning" @click="handleSubmit">保存草稿</el-button>
      <el-button @click="handleClose">取消</el-button>
    </section>
  </el-drawer>
</template>

<script setup>
import { reactive, ref, watch } from 'vue';
import LandClassificationType from '@/components/LandClassificationType.vue';
import CustomSelect from '@/components/CustomSelect.vue';
import LandIsTranfer from '@/components/LandIsTranfer.vue';
import { lnadSave } from '@/apis/land';
import { ElMessage } from 'element-plus';
import { CommonUpload } from '@/apis';

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
});
const emit = defineEmits(['close']);
/* --------------- data --------------- */
// #region

const _visible = ref(false);
const baseInfo = reactive({
  landName: '',
  gridId: '',
  landClassificationType: '',
  villageCode: '',
  isTransfer: '',
  area: '',
  owner: '',
  soilType: '',
});
const propertyInfo = reactive({
  propertyName: '',
  propertyPhone: '',
  landCode: '',
  propertyCertificateUrl: '',
});
watch(
  () => props.visible,
  () => {
    _visible.value = props.visible;
  }
);
watch(
  () => baseInfo,
  () => {
    console.log('---', baseInfo);
  }
);
const baseForm = ref();
const propertyForm = ref();
// #endregion

/* --------------- methods --------------- */
// #region
const rowUploadPicture = async ({ file }) => {
  const formData = new FormData();
  formData.append('file', file);
  const res = await CommonUpload(formData);
  if (res.code === 200) {
    console.log('---', res);
    // state.form.productUrl = res.data.url;
    // const base64 = await imageToBase64(file);
    // state.form.base64 = base64;
  }
};
async function handleSubmit() {
  const data = {
    ...baseInfo,
    ...propertyInfo,
    isDraftsSave: 0,
  };
  const res = await lnadSave(data);
  if (res.code == 200) {
    ElMessage.success('保存成功');
    resFrom();
  }
}
function handleClose() {
  resFrom();
  emit('close');
}
function resFrom() {
  baseForm.value.resetFields();
  propertyForm.value.resetFields();
}

// #endregion
</script>

<style lang="scss" scoped>
.title_ {
  margin-bottom: 12px;
  font-size: 16px;
  font-weight: bold;
}
.base_form,
.property_form {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}
.btns {
  margin-top: 12px;
  display: grid;
  justify-content: center;
  grid-template-columns: auto auto auto;
  gap: 12px;
}
</style>
