<template>
  <section class="Add">
    <ai-detail v-if="!id || isEdit==true">
      <template #title>
        <ai-title :title="params.id ? '编辑宅基地所有权登记' : '新增宅基地所有权登记'" :isShowBack="true" :isShowBottomBorder="true" @onBackClick="cancel(false)"></ai-title>
      </template>
      <template #content>
        <ai-card title="宅基地所有权信息">
          <template #right v-if="params.id">
            <span style="color:#2266FF;margin-left: 16px;cursor: pointer;font-size: 12px;" v-if="isEdit==true" @click="isEdit=false">取消</span>
            <span style="color:#2266FF;margin-left: 16px;cursor: pointer;font-size: 12px;" v-if="isEdit==true" @click="confirm()">保存</span>
          </template>
          <template #content>
            <el-form ref="form" :model="form" :rules="rules" size="small"  label-width="150px">
              <el-form-item label="使用证书" prop="useCertificate">
                <el-input v-model="form.useCertificate" placeholder="请输入宅基地使用证书号"></el-input>
              </el-form-item>
              <el-form-item label="所在地区" prop="areaId">
                <ai-area-select clearable always-show :instance="instance" v-model="form.areaId" :disabled-level="disabledLevel"></ai-area-select>
              </el-form-item>
              <el-row type="flex">
                <el-col :span="20">
                  <el-form-item label="使用人" prop="usePerson">
                    <el-input v-model="form.usePerson" placeholder="请输入使用人姓名"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-form-item label="家庭人口" prop="houseNumber">
                    <el-input type="number" v-model="form.houseNumber" placeholder="请输入使用人家庭人口"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row type="flex">
                <el-col :span="20">
                  <el-form-item label="身份证号" prop="idNumber">
                    <el-input v-model="form.idNumber" placeholder="请输入使用人身份证号"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-form-item label="土地所有权" prop="landOwnership">
                    <el-input v-model="form.landOwnership" placeholder="请输入土地所有权人/组织名称"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row type="flex">
                <el-col :span="12">
                  <el-form-item label="土地面积/㎡" prop="landArea">
                    <el-input v-model="form.landArea" placeholder="请输入土地面积"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="土地四至" prop="landFourTo">
                <el-input type="textarea" v-model="form.landFourTo" placeholder="请说明宅基地的地块名称地界四至" maxlength="500" show-word-limit :rows="5" />
              </el-form-item>
              <el-form-item label="使用情况" prop="useSituation">
                <el-input type="textarea" v-model="form.useSituation" placeholder="请说明宅基地的使用情况" maxlength="500" show-word-limit :rows="5" />
              </el-form-item>
              <el-form-item label="权证照片" prop="fileList">
              <ai-uploader v-model="form.fileList" :instance="instance" :limit="9" isShowTip/>
            </el-form-item>
            </el-form>
          </template>
        </ai-card>
      </template>
      <template slot="footer" class="footer">
        <el-button class="delete-btn footer-btn" @click="cancel(false)">取消</el-button>
        <el-button class="footer-btn" type="primary" @click="confirm()">保存</el-button>
      </template>
    </ai-detail>
    <ai-detail class="add" v-if="params.id && !isEdit">
      <template slot="title">
        <ai-title title="详情" isShowBack isShowBottomBorder @onBackClick="cancel(false)"></ai-title>
      </template>
      <template slot="content">
        <ai-card title="合同信息">
          <template #right>
              <span style="color:#2266FF;cursor: pointer;font-size: 12px;" class="iconfont iconEdit" v-if="isEdit==false" @click="isEdit=true,form=data">修改</span>
            </template>
          <template #content>
            <ai-wrapper>
              <ai-info-item label="使用证书" :value="data.useCertificate" isLine></ai-info-item>
              <ai-info-item label="所在地区" :value="data.areaName" isLine></ai-info-item>
              <ai-info-item label="使用人" :value="data.usePerson"></ai-info-item>
              <ai-info-item label="家庭人口" :value="data.houseNumber"></ai-info-item>
              <ai-info-item label="身份证号" :value="data.idNumber"></ai-info-item>
              <ai-info-item label="土地所有权" :value="data.landOwnership">{{ dict.getLabel('landOwnership', data.landOwnership) }}</ai-info-item>
              <ai-info-item label="土地面积/㎡" :value="data.landArea"></ai-info-item>
              <ai-info-item label="土地四至" :value="data.landFourTo" isLine></ai-info-item>
              <ai-info-item label="使用情况" :value="data.useSituation" isLine></ai-info-item>
              <ai-info-item label="权证照片" isLine>
                <ai-uploader v-model="data.fileList" :instance="instance" :limit="9" disabled/>
              </ai-info-item>
            </ai-wrapper>
          </template>
        </ai-card>
      </template>
    </ai-detail>
  </section>
</template>

<script>
import {mapState} from 'vuex'
export default {
  name: "Add",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function,
    params: Object,
  },
  data() {
    return { 
      id: '',
      form: {
        useCertificate: '',
        areaId: '',
        areaName: '',
        usePerson: '',
        houseNumber: '',
        idNumber: '',
        landOwnership: '',
        landArea: '',
        landFourTo: '',
        useSituation: '',
        fileList: [],
      },
      isEdit: false,
      radioFrom: '',
      disabledLevel: 3,
      data: {},
    }
  }, 
  computed: {
    rules() {
      return {
        useCertificate: [{required: true, message: '请输入使用证书', trigger: 'blur'}],
        areaId: [{required: true, message: '请选择所在地区', trigger: 'blur'}],
        usePerson: [{required: true, message: '请输入使用人', trigger: 'blur'}],
        houseNumber: [{required: true, message: '请输入家庭人口', trigger: 'blur'},
                      {pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: '请输入正整数',trigger: 'blur'}],
        idNumber: [{required: true, message: '请输入身份证号', trigger: 'blur'}],
        landOwnership: [{required: true, message: '请输入土地所有权', trigger: 'blur'}],
        landArea: [{required: true, message: '请输入土地面积', trigger: 'blur'}],
        landFourTo: [{required: true, message: '请输入土地四至', trigger: 'blur'}],
        useSituation: [{required: true, message: '请输入使用情况', trigger: 'blur'}],
        fileList: [{required: true, message: '请上传图片', trigger: 'blur'}],
      }
    },
    ...mapState(['user']),
  },
  created() {
    if (this.params && this.params.id) {
      this.id = this.params.id
      this.getDetail(this.params.id)
    }
    this.form.areaId = this.user.info.areaId
  },
  methods: {
    cancel(isRefresh) {
      this.$emit('change', {
        type: 'List',
        isRefresh: !!isRefresh,
      })
    },
    getDetail(id) {
      this.instance.post(`/apphomesteadinfo/queryDetailById?id=${id}`).then(res => {
        if (res.code === 0) {
          this.data = res.data
        }
      })
    },
    confirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.instance.post(`/apphomesteadinfo/addOrUpdate`, {
            ...this.form,
            id: this.params.id || '',
          }).then(res => {
            if (res.code == 0) {
              this.$message.success('提交成功')
              setTimeout(() => {
                this.cancel(true)
              }, 600)
            }
          })
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.Add {
  height: 100%;
  background: #f3f4f5;

  ::v-deep .ai-detail .ai-detail__footer {
    background: #FFF;
  }
}
</style>