<template>
  <div class="addLinkage">

    <div class="item">
      <div><span>*</span>四邻对象</div>
      <AiSelect class="right" v-model="form.residentId" :list="userList"/>
    </div>

    <div class="item">
      <div><span>*</span>事件日期</div>
      <div @click="isShowDate = true">
        <span v-if="form.linksageDate" >{{ form.linksageDate }}</span>
        <span v-else style="color: #999;">请选择</span>
        <u-icon name="arrow-right" color="#ddd"/>
      </div>
    </div>
    <u-picker mode="time" :params="params" v-model="isShowDate" @confirm="onDateChange"></u-picker>

    <div>
      <div class="title"><span style="color: #FF4466">*</span>事件描述</div>
      <div>
        <u-input type="textarea" v-model="form.description" placeholder="请输入事件内容描述" height="200" maxlength="200"/>
      </div>
      <div class="tips">{{form.description.length}}/200</div>
    </div>

    <div class="btn">
      <div class="addBtn" @click="confirm">提交</div>
    </div>

  </div>
</template>

<script>
export default {
  name: "addLinkage",
  appName:  "添加联动记录",  // :"修改联动记录"
  data() {
    return {
      params: {
        year: true,
        month: true,
        day: true,
      },
      isShowDate: false,
      form: {
        residentId: '',
        linksageDate: '',
        description: '',
      },
      userList: [],
      id: '',
      flag: false,
    }
  },
  onShow() {
    this.getTitle()
    this.$dict.load('agriculturalType')
    this.getUserList()
  },
  onLoad(o) {
    if (o?.id) {
      this.id = o.id
      this.getDetail()
      this.form = JSON.parse(JSON.stringify(this.data))
    }
  },
  methods: {
    onDateChange(e) {
      this.form.linksageDate = `${e.year}-${e.month}-${e.day}`
    },
    getDetail() {
      this.$instance.post('/app/apppartyfourlinkage/queryDetailById',null,{
        params: {
          id: this.id,
        }
      }).then(res => {
        if(res?.data) {
          this.form = {...res.data}
        }
      })
    },
    getUserList() {
      this.$instance.post('/app/apppartyfourresident/listFourResidentByApplet').then((res) => {
        if(res?.data) {
          res.data.map((item) => {
            this.userList.push({
              label: item.name,
              value: item.id,
            })
          })
        }
      })
    },

    confirm() {
      if(this.flag) reutrn
      if(!this.form.residentId) {
        return this.$u.toast('请选择四邻对象')
      }
      if(!this.form.linksageDate) {
        return this.$u.toast('请选择事件日期')
      }
      if(!this.form.description) {
        return this.$u.toast('请输入事件描述')
      }
      this.flag = true
      this.$instance.post('/app/apppartyfourlinkage/addByWxApplet',{
        ...this.form,
        id: this.id || '',
      }).then(res => {
        if(res.code == 0) {
          this.$u.toast('提交成功')
          uni.$emit('update')
          if (!this.id) {
            setTimeout(() => {
              uni.navigateBack()
            },600)
          } else {
            setTimeout(() => {
              uni.navigateBack({
                delta: 2
              })
            },600)
          }
        }
      })
    },

    getTitle() {
      if (this.id) {
        uni.setNavigationBarTitle({
          title: '修改联动记录'
        });
      }
    }

  }
}
</script>

<style lang="scss" scoped>
.addLinkage {
  padding: 0 32px;
  box-sizing: border-box;
  background: #FFF;
  .item {
    display: flex;
    justify-content: space-between;
    padding: 32px 0;
    border-bottom: 1px solid #DDDDDD;
    div:first-child {
      span {
        color: #FF4466;
      }
    }
  }
  .title {
    padding: 32px 0
  }
  .tips {
    text-align: right;
    color: #999;
    padding: 15px 0;
  }
  .btn {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 120px;
    padding: 16px 32px;
    box-sizing: border-box;
    background: #F3F6F9;
    .addBtn {
      width: 100%;
      height: 88px;
      line-height: 88px;
      text-align: center;
      background: #4181FF;
      border-radius: 16px;
      color: #FFF;
    }
  }
}
</style>