<template>
  <section class="Add">
    <ai-detail class="add" v-if="!id || isEdit==true">
      <template #title>
        <ai-title :title="params.id ? '编辑土地流转经营登记' : '新增土地流转经营登记'" :isShowBack="true" :isShowBottomBorder="true" @onBackClick="cancel(false)" />
      </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="getDetail(params.id),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-row type="flex">
                <el-col :span="20">
                  <el-form-item label="土地流出方" prop="landOutflowName">
                    <el-input v-model="form.landOutflowName" placeholder="请输入土地流出方名称"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-form-item label="土地流出方证件号码" prop="landOutflowNumber">
                    <el-input v-model="form.landOutflowNumber" placeholder="请输入土地流出方证件号码" maxlength="18"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row type="flex">
                <el-col :span="20">
                  <el-form-item label="土地承接方" prop="landUndertakeName">
                    <el-input v-model="form.landUndertakeName" placeholder="请输入土地承接方名称"></el-input>
                  </el-form-item>    
                </el-col>
                <el-col :span="20">
                  <el-form-item label="土地承接方证件号码" prop="landUndertakeNumber">
                    <el-input v-model="form.landUndertakeNumber" placeholder="请输入土地承接方证件号码" maxlength="18"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row type="flex">
                <el-col :span="20">
                  <el-form-item label="合同编号" prop="contractNo">
                    <el-input v-model="form.contractNo" placeholder="请输入合同编号"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-form-item label="流转形式" prop="circulateType">
                    <ai-select v-model="form.circulateType" placeholder="请选择" :selectList="$dict.getDict('landCirculateType')"></ai-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row type="flex">
                <el-col :span="20">
                  <el-form-item label="土地面积/㎡" prop="landArea">
                    <el-input v-model="form.landArea" placeholder="请输入土地面积" />
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-form-item label="每亩年租金(元)" prop="yearRent">
                    <el-input v-model="form.yearRent" placeholder="请输入"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row type="flex">
              <el-col :span="20">
                <el-form-item label="开始日期" prop="startDate">
                  <el-date-picker v-model="form.startDate" value-format="yyyy-MM-dd" :picker-options="startTime" type="date" placeholder="选择日期" style="width:410px">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="截止日期" prop="endDate">
                  <el-date-picker v-model="form.endDate" value-format="yyyy-MM-dd" :picker-options="endTime" type="date" placeholder="选择日期" style="width:410px">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="土地信息" prop="landInfo">
              <el-input type="textarea" v-model="form.landInfo" placeholder="请说明土地流转的地块名称地界四至" maxlength="500" show-word-limit :rows="5" />
            </el-form-item>
            <el-form-item label="土地用途" prop="landUse">
              <el-input type="textarea" v-model="form.landUse" 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="edit">修改</span>
            </template>
          <template #content>
            <ai-wrapper>
              <ai-info-item label="土地流出方" :value="data.landOutflowName" ></ai-info-item>
              <ai-info-item label="土地流出方证件号码" :value="data.landOutflowNumber"></ai-info-item>
              <ai-info-item label="土地承接方" :value="data.landUndertakeName"></ai-info-item>
              <ai-info-item label="土地承接方证件号码" :value="data.landUndertakeNumber"></ai-info-item>
              <ai-info-item label="合同编号" :value="data.contractNo"></ai-info-item>
              <ai-info-item label="流转形式">{{ $dict.getLabel('landCirculateType',data.circulateType) }}</ai-info-item>
              <ai-info-item label="土地面积/㎡" :value="data.landArea"></ai-info-item>
              <ai-info-item label="每亩年租金(元)" :value="data.yearRent"></ai-info-item>
              <ai-info-item label="开始日期" :value="data.startDate"></ai-info-item>
              <ai-info-item label="截止日期" :value="data.endDate"></ai-info-item>
              <ai-info-item label="土地信息" :value="data.landInfo" isLine></ai-info-item>
              <ai-info-item label="土地用途" :value="data.landUse" 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>
export default {
  name: "Add",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function,
    params: Object,
  },
  data() {
    return {
      radioTo: '',
      radioFrom: '',
      form: {
        landOutflowName: '',
        landOutflowNumber: '',
        landUndertakeName: '',
        landUndertakeNumber: '',
        contractNo: '',
        circulateType: '',
        landArea: '',
        yearRent: '',
        startDate: '',
        endDate:'' ,
        landInfo: '',
        landUse: '',
        fileList: [],
      },
      data: {},
      isEdit: false,
      id: '',
      startTime: {
        disabledDate: time => {
          if (this.form.endTime) {
            return (
              time.getTime() > new Date(this.form.endTime).getTime()
            );
          } else {
            return time.getTime() > Date.now();
          }
        }
      },
      endTime: {
        disabledDate: time => {
          if (this.form.startDate) {
            return (
              time.getTime() < new Date(this.form.startDate).getTime()
            );
          } 
        }
      },
    }
  },
  computed: {
    rules() {
      return {
        landOutflowName: [{required: true, message: "请输入土地流出方名称", trigger: "blur"}],
        landOutflowNumber: [{required: true, message: "请输入土地流出方证件号码", trigger: "blur"},
        {min: 18, message: "请输入18位土地流出方证件号码", trigger: "blur"}],
        landUndertakeName: [{required: true, message: "请输入土地承接方名称", trigger: "blur"}],
        landUndertakeNumber: [{required: true, message: "请输入土地承接方证件号码", trigger: "blur"},
        {min: 18, message: "请输入18位土地承接方证件号码", trigger: "blur"}],
        contractNo: [{required: true, message: "请输入合同编号", trigger: "blur"}],
        circulateType: [{required: true, message: "请选择流转形式", trigger: "blur"}],
        landArea: [{required: true, message: "请输入土地面积", trigger: "blur"}],
        // yearRent: [{required: true, message: "请输入每亩年租金", trigger: "blur"}],
        startDate: [{required: true, message: "请选择开始日期", trigger: "blur"}],
        // endDate: [{required: true, message: "请选择截止日期", trigger: "blur"}],
        landInfo: [{required: true, message: "请输入土地信息", trigger: "blur"}],
        // landUse: [{required: true, message: "请输入土地用途", trigger: "blur"}],
        fileList: [{required: true, message: "请上传图片", trigger: "blur"}],
      }
    },

    
  },
  created() {
    this.$dict.load('landCirculateType')
    if(this.params && this.params.id) {
      this.id = this.params.id
      this.getDetail(this.params.id)
    }
  },
  methods: {
    cancel(isRefresh) {
      this.$emit('change', {
        type: 'List',
        isRefresh: !!isRefresh,
      })
    },
    edit() {
      this.isEdit=true
      this.form = JSON.parse(JSON.stringify(this.data))
    },
    getDetail(id) {
      this.instance.post(`/applandcirculateinfo/queryDetailById?id=${id}`).then(res => {
        if (res.code === 0) {
          this.data = res.data
        }
      })
    },
    confirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.instance.post(`/applandcirculateinfo/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 .footer {
    background: #FFF !important;
  }

  // ::v-deep .ai-info-item label {
  //   width: 130px;
  // }
}
</style>