<template>
  <view class="">
    <view class="card">
      <u--form labelPosition="left" :model="formData" :rules="rules" ref="archives">
        <view class="item">
          <view class="title">基本信息</view>
          <u-form-item labelWidth="auto" label="户主姓名" required prop="name" borderBottom>
            <u--input v-model="formData.name" placeholder="请输入姓名"></u--input>
          </u-form-item>
          <u-form-item label="性别" required prop="sex" borderBottom>
            <u-radio-group v-model="formData.sex" style="margin: 16rpx;">
              <u-radio :customStyle="{marginRight: '16px'}"
                v-for="(item, index) in [{value:1,label:'男'},{value:0,label:'女'}]" :key="index" :label="item.label"
                :name="item.value">
              </u-radio>
            </u-radio-group>
          </u-form-item>
          <u-form-item label="年龄" required prop="age" borderBottom>
            <u--input v-model="formData.age" placeholder="请输入年龄"></u--input>
          </u-form-item>
          <u-form-item labelWidth="auto" label="身份证号" required prop="id_card" borderBottom>
            <u--input v-model="formData.id_card" placeholder="请输入身份证号" type="idcard"></u--input>
          </u-form-item>
          <u-form-item labelWidth="auto" label="联系方式" required prop="phone" borderBottom>
            <u--input v-model="formData.phone" placeholder="请输入手机号" type="number"></u--input>
          </u-form-item>
        </view>
        <view class="item">
          <districtSelector ref="districtSelectorRef"></districtSelector>
          <u-form-item labelWidth="auto" label="详细地址" required prop="address" borderBottom>
            <u--input v-model="formData.address" placeholder="请输入详细地址"></u--input>
          </u-form-item>
          <u-form-item labelWidth="auto" label="汽车是否能到家" required prop="highway" borderBottom>
            <u-radio-group v-model="formData.highway" style="margin: 16rpx;">
              <u-radio :customStyle="{marginRight: '16px'}"
                v-for="(item, index) in [{value:1,label:'是'},{value:0,label:'否'}]" :key="index" :label="item.label"
                :name="item.value">
              </u-radio>
            </u-radio-group>
          </u-form-item>
          <block value="智能手机">
            <u-form-item labelWidth="auto" label="是否使用智能手机" required prop="smart_phone" borderBottom>
              <u-radio-group v-model="formData.smart_phone" style="margin: 16rpx;">
                <u-radio :customStyle="{marginRight: '16px'}"
                  v-for="(item, index) in [{value:1,label:'是'},{value:0,label:'否'}]" :key="index" :label="item.label"
                  :name="item.value">
                </u-radio>
              </u-radio-group>
            </u-form-item>
            <block v-if="formData.smart_phone>0">
              <u-form-item labelWidth="auto" label="微信号" required borderBottom>
                <u--input v-model="formData.wechat" placeholder="请输入微信号"></u--input>
              </u-form-item>
            </block>
          </block>
        </view>
        <residents ref="residentsRef"></residents>
        <breeding ref="breedingRef"></breeding>
        
        
        <block v-for="(item, index) in showDemandList" :key="'demand'+index">
          <block v-if="item.category_id==0">
            <breeding v-if="item.category_child==0" ref="demandRef00"></breeding>
            <store v-if="item.category_child==1" ref="demandRef01"></store>
            <deepProcessing v-if="item.category_child==2" ref="demandRef02"></deepProcessing>
            <thickProcessing v-if="item.category_child==3" ref="demandRef03"></thickProcessing>
          </block>
          <block v-if="item.category_id==1">
            <houseRepair v-if="item.category_child==0" ref="demandRef10"></houseRepair>
            <houseDecoration v-if="item.category_child==1" ref="demandRef11"></houseDecoration>
            <houseRenovate v-if="item.category_child==2" ref="demandRef12"></houseRenovate>
            <houseTransaction v-if="item.category_child==3" ref="demandRef13"></houseTransaction>
          </block>
          <block v-if="item.category_id==2">
            <banquetMarry v-if="item.category_child==0" ref="demandRef20"></banquetMarry>
            <banquetBirthday v-if="item.category_child==1" ref="demandRef21"></banquetBirthday>
            <banquetFullMoon v-if="item.category_child==2" ref="demandRef22"></banquetFullMoon>
            <banquetOther v-if="item.category_child==3" ref="demandRef23"></banquetOther>
            <banquetFuneral v-if="item.category_child==4" ref="demandRef24"></banquetFuneral>
          </block>
          <button type="primary" class="delete" @click="removeShowDemand">-</button>
        </block>
        <button type="primary" class="plus demand" @click="demandShow=true">点击添加更多需求</button>
        <u-picker :show="demandShow" ref="demandRef" :loading="demandLoading" :columns="[demandList, demandListChild]"
          @change="changeHandler" @cancel="demandShow = false" @confirm="changeDemand"></u-picker>
        
      </u--form>
      <button @click="addArchives" type="primary" class="btn">完成登记</button>
    </view>
  </view>
</template>

<script>
  import { Toast } from "../../libs/uniApi"
  import { informationAdd } from "@/api/information.js"
  import districtSelector from "@/components/districtSelector/districtSelector.vue" //地区选择器
  import residents from "@/components/newArchives/residents.vue" // 常住人口
  import breeding from "@/components/newArchives/breeding.vue" // 种养殖
  import store from "@/components/newArchives/store.vue" // 开设店铺
  import deepProcessing from "@/components/newArchives/deepProcessing.vue" // 深加工
  import thickProcessing from "@/components/newArchives/thickProcessing.vue" // 粗加工
  import houseRepair from "@/components/newArchives/houseRepair.vue" // 修房
  import houseDecoration from "@/components/newArchives/houseDecoration.vue" // 装房
  import houseRenovate from "@/components/newArchives/houseRenovate.vue" // 翻新
  import houseTransaction from "@/components/newArchives/houseTransaction.vue" // 买卖房子
  import banquetMarry from "@/components/newArchives/banquetMarry.vue" // 婚宴
  import banquetBirthday from "@/components/newArchives/banquetBirthday.vue" // 寿宴
  import banquetFullMoon from "@/components/newArchives/banquetFullMoon.vue" // 满月酒
  import banquetOther from "@/components/newArchives/banquetOther.vue" // 其他庆祝宴
  import banquetFuneral from "@/components/newArchives/banquetFuneral.vue" // 白事
  export default {
    components: { districtSelector, residents, breeding, store, deepProcessing, thickProcessing, houseRepair, houseDecoration, houseRenovate,houseTransaction, banquetMarry, banquetBirthday,banquetFullMoon, banquetOther, banquetFuneral },
    data() {
      return {
        demandShow: false,
        demandLoading: false,
        demandList: ['生产投入明细', '修房装修', '宴席', '生活采购'], //更多需求
        demandListChild: ['种养殖', '开设店铺', '深加工', '粗加工'], //更多二级需求
        showDemandList: [], //更多需求展示列表
        formData: {
          id_card: '',
          sex: '',
          name: '',
          phone: '', //电话
          highway: '', //汽车是否能到家
          smart_phone: '', //是否使用智能手机
          wechat: '', //微信号
          address: '', //家庭地址
          age: '', //年龄
          category_id: '1',
          category_child: '2',
          data_type: '1',
        },
        rules: {
          phone: {
            required: true,
            pattern: /^(1[3456789]\d{9})$/,
            message: '请输入正确的手机号码',
            trigger: ['change', 'blur']
          },
          id_card: {
            required: true,
            pattern: /^\d{17}[\dX]$|^\d{15}[\dX]$/,
            message: '请输入正确的身份证号',
            trigger: ['change', 'blur']
          },
          sex: {
            type: 'number',
            max: 1,
            required: true,
            message: '请选择男或女',
            trigger: ['blur', 'change']
          },
          age: {
            type: 'number',
            required: true,
            message: '请输入年龄',
            trigger: ['blur', 'change']
          },
          name: {
            type: 'string',
            required: true,
            message: '姓名不能为空',
            trigger: ['change', 'blur']
          },
          address: {
            type: 'string',
            required: true,
            message: '地址不能为空',
            trigger: ['change', 'blur']
          },
          highway: {
            validator: (rule, value, callback) => {
              typeof value === 'number' ? callback() : callback('不能为空')
            },
            trigger: ['change', 'blur']
          },
          smart_phone: {
            validator: (rule, value, callback) => {
              typeof value === 'number' ? callback() : callback('不能为空')
            },
            trigger: ['change', 'blur']
          },
        },
      }
    },
    onLoad() {},
    onShow() {},
    methods: {
      // 新增档案
      async addArchives() {
        Toast('开发中')
        let flag1 = this.$refs.districtSelectorRef.validate();
        let flag2 = this.$refs.residentsRef.validate();
        let flag3 = this.$refs.breedingRef.validate();
        this.$refs.archives.validate().then(async (e) => {
          if (e && flag1 && flag2 && flag3) {
            uni.showLoading()
            let districtForm = {
              area_id: this.$refs.districtSelectorRef.formData.area,
              street_id: this.$refs.districtSelectorRef.formData.street,
              village_id: this.$refs.districtSelectorRef.formData.village,
              brigade_id: this.$refs.districtSelectorRef.formData.brigade,
            }
            let res = await informationAdd({
              ...this.formData,
              ...districtForm,
              ...this.$refs.residentsRef.residents,
              datas: [{
                category_id: '1',
                category_child: '2',
                datas: {
                  ...this.$refs.breedingRef.formData
                }
              }]
            });
            uni.hideLoading()
            uni.showToast({
              icon: "none",
              title: "添加成功",
              success: () => {
                setTimeout(() => {
                  uni.navigateBack()
                }, 1000)
              }
            })
          }
        })

      },
      // 选择更多需求
      changeHandler(e) {
        if (e.columnIndex == 0) { //滑动左侧时才加载,右侧不加载
          this.demandLoading = true;
          setTimeout(() => {
            switch (e.indexs[0]) {
              case 0:
                this.demandListChild = ['种养殖', '开设店铺', '深加工', '粗加工'];
                break;
              case 1:
                this.demandListChild = ['修房', '装房', '翻新', '买卖房'];
                break;
              case 2:
                this.demandListChild = ['婚宴', '寿宴', '满月酒', '其他庆祝宴', '白事'];
                break;
              case 3:
                this.demandListChild = ['烟酒', '水电气电话', '交通工具', '生活用品', '农副特产品'];
                break;
              default:
                this.demandListChild = [];
                break;
            }
            this.demandLoading = false;
          }, 800)
        }
      },
      // 确认添加需求
      changeDemand(e) {
        this.showDemandList.push({
          category_id: e.indexs[0],
          category_child: e.indexs[1],
        })
        // console.log(this.showDemandList);
        this.demandShow = false;
      },
      // 删除需求
      removeShowDemand(index){
        uni.showModal({
          title:'确定删除这一部分内容吗',
          success: (res) => {
            if(res.confirm)this.showDemandList.splice(index, 1);
          }
        })
      }
    },
    onPullDownRefresh() {
      uni.stopPullDownRefresh()
    }
  }
</script>

<style lang="scss">
  .card {
    background-color: #fff;
    margin: 28rpx;
    padding: 28rpx;
    border-radius: 14rpx;

    .item {
      margin-top: 30rpx;

      &:nth-child(1) {
        margin-top: 0;
      }

      .title {
        font-weight: 500;
        font-size: 34rpx;

        &::before {
          width: 8rpx;
          height: 26rpx;
          border-radius: 4rpx;
          background-color: #3175f9;
          content: "";
          display: inline-block;
          margin-right: 8rpx;
        }
      }
    }

    .btn {
      margin-top: 32rpx;
      // margin-bottom: 40rpx;
      width: 100%;
      height: 84rpx;
      background: $theme-oa-color;
      border-radius: 42rpx 42rpx 42rpx 42rpx;
      color: #fff;
      line-height: 80rpx;
      text-align: center;
    }
  }

  .delete {
    margin: 22rpx 0;
    // margin-bottom: 40rpx;
    width: 100%;
    height: 54rpx;
    line-height: 54rpx;
    background: #f56c6c;
    border-radius: 14rpx;
    font-size: 28rpx;
    color: #fff;
    text-align: center;
  }

  .plus {
    margin: 22rpx 0;
    // margin-bottom: 40rpx;
    width: 100%;
    height: 54rpx;
    line-height: 54rpx;
    background: $theme-oa-color;
    border-radius: 14rpx;
    color: #fff;
    text-align: center;
  }

  .demand {
    margin-top: 40rpx;
    margin-bottom: 50rpx;
  }
</style>