<template>
  <div class="app-container">

    <van-nav-bar left-arrow fixed placeholder @click-left="onClickLeft" >
      <template #title>
        <div class="tb_main">
          新增承包方信息
        </div>
      </template>
    </van-nav-bar>

    <div class="list_main">
      <van-form ref="formData">
        <van-field v-model="form.cbfbm" label="代码：" placeholder="请输入代码" required :rules="[{ required: true }]" :border="false" input-align="left" maxlength="18">
          <template #button>
            <van-button size="mini" type="primary" native-type="button" @click="generateCode">生成代码</van-button>
          </template>
        </van-field>
        <van-field
          v-model="cbflxText"
          label="类型："
          placeholder="请选择类型"
          required
          :rules="[{ required: true }]"
          :border="false"
          input-align="left"
          right-icon="arrow-down"
          readonly
          clickable
          @click="showType = true"
        />
        <van-popup v-model="showType" position="bottom">
          <van-picker
            show-toolbar
            :columns="contractorTypeOptions"
            value-key="dictLabel"
            @confirm="onConfirmTypeOptions"
            @cancel="showType = false"
          />
        </van-popup>
        <van-field v-model="form.cbfmc" label="名称：" placeholder="请输入名称" required :rules="[{ required: true }]" :border="false" input-align="left" maxlength="50" />
        <van-field
          v-model="cbfzjlxText"
          label="证件类型："
          placeholder="请选择证件类型"
          required
          :rules="[{ required: true }]"
          :border="false"
          input-align="left"
          right-icon="arrow-down"
          readonly
          clickable
          @click="showZjType = true"
        />
        <van-popup v-model="showZjType" position="bottom">
          <van-picker
            show-toolbar
            :columns="credentialTypeOptions"
            value-key="dictLabel"
            @confirm="onConfirmZjTypeOptions"
            @cancel="showZjType = false"
          />
        </van-popup>

        <van-field v-model="form.cbfzjhm" label="证件号码：" placeholder="请输入证件号码" required :rules="[{ required: true }]" :border="false" input-align="left" maxlength="20" />
        <van-field v-model="form.cbfdz" label="地址：" placeholder="请输入地址" required :rules="[{ required: true }]" :border="false" input-align="left" maxlength="100" />
        <van-field v-model="form.yzbm" label="邮编：" placeholder="请输入邮编" required :rules="[{ required: true }]" :border="false" input-align="left" maxlength="6" />
        <van-field v-model="form.lxdh" label="电话：" placeholder="请输入电话" required :rules="[{ required: true }]" :border="false" input-align="left" maxlength="15" />
        <van-field v-model="form.cbfdcjs" label="调查记事：" type="textarea" rows="3" placeholder="请输入调查记事" required :rules="[{ required: true }]" :border="false" input-align="left" maxlength="250" />
      </van-form>
      <p class="btn" @click="submitForm">保存</p>
    </div>


  </div>
</template>
<script>
  import Cookies from "js-cookie";
  import { addCbf, generateContractorCode } from "@/api/contracted/cbf";

  export default {
    name: "contractedVillageContractorAdd",
    data() {
      return {
        showType:false,
        showZjType:false,
        // 承包方信息表单
        form: {
          deptId: null,
          cbfbm: null,
          cbflx: '1',
          cbfmc: null,
          cbfzjlx: '1',
          cbfzjhm: null,
          cbfdz: null,
          yzbm: null,
          lxdh: null,
          cbfcysl: null,
          cbfdcjs: null,
        },
        // 承包方类型(标签名)
        cbflxText: '农户',
        // 承包方证件类型(标签名)
        cbfzjlxText: '居民身份证',
        // 承包方类型字典
        contractorTypeOptions: [],
        // 承包方证件类型字典
        credentialTypeOptions: [],
      };
    },
    created() {
      this.getDicts("cbf_type").then(response => {
        this.contractorTypeOptions = response.data;
      });
      this.getDicts("cert_type").then(response => {
        this.credentialTypeOptions = response.data;
      });
    },
    methods: {
      onConfirmTypeOptions(value){
        this.form.cbflx = value.dictValue;
        this.cbflxText = value.dictLabel;
        this.showType = false;
      },
      onConfirmZjTypeOptions(value){
        this.form.cbfzjlx = value.dictValue;
        this.cbfzjlxText = value.dictLabel;
        this.showZjType = false;
      },
      submitForm() {
        this.$refs.formData.validate().then(() => {
          this.form.deptId = this.$route.params.deptId;
          this.form.cbfcysl = 1;
          addCbf(this.form).then(response => {
            if (response.code == 200) {
              this.$toast({
                icon: 'success',
                message: '保存成功',
                duration:"1000",
                onClose: function() {
                  history.back(-1);
                }
              })
            }
          });
        }).catch(() => {
          this.$notify({ type: 'danger', message: '请填写完整的表单项' });
        });
      },
      generateCode() {
        generateContractorCode({deptId: this.$route.params.deptId}).then(response => {
          this.$set(this.form, 'cbfbm', response.data);
        });
      },
    },
  };
</script>
<style scoped lang="scss">

  .app-container{
    background: #fff url("../../../../../static/images/contracted/contracted_index_bg.png") no-repeat center;
    background-size: 100% 100%;
    height: 100vh;
    padding: 0 4vw;
    overflow: hidden;
  }

  /deep/ .van-nav-bar{
    background: transparent;
  }

  /deep/ .van-nav-bar .van-icon{
    color: #000000;
  }

  /deep/ .van-hairline--bottom::after{
    border: none;
  }

  /deep/ .van-search__content{
    background: rgba(255,255,255,.5);
  }
  /deep/ .van-search{
    padding: 0;
    flex: 1;
  }

  /deep/ .van-ellipsis{
    overflow: initial;
  }

  .tb_main{
    position: relative;
    p{
      position: absolute;
      display: inline-block;
      margin-left: 10PX;
    }
  }

  /* .tb{
    font-size: 12px;
    color: #ff8900;
    background: #daf6e7;
    border: 1px solid #d7be6e;
    padding: 2PX 8PX;
    border-radius: 50PX;
    margin-right: 5PX;
  } */

  .tap_block{
    width: 100%;
    display: flex;
    justify-content: space-between;
    background: #ebfaf2;
    padding: 2PX 4PX;
    border-radius: 10PX;
    margin-top: 1vh;
    .active{
      background-image: linear-gradient(to right,#c6fe8b,#48e5a2);
      box-shadow: 0 0 10PX #cccccc;
      color: #333333;
    }
    p{
      width: 25%;
      text-align: center;
      padding: 5PX 0;
      border-radius: 10PX;
      color: #666666;
    }
  }

  .search_main{
    display: flex;
    margin-top: 2vh;
    .search_btn{
      background: rgba(255,255,255,.5);
      width: 25%;
      border-radius: 50PX;
      margin-left: 10PX;
      padding: 2PX;
      .active{
        background-image: linear-gradient(to right,#c6fe8b,#48e5a2);
        color: #333333;
        border-radius: 50PX;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100%;
      }
    }
  }

  .second_tap{
    display: flex;
    align-items: center;
    margin-top: 1vh;
    p{
      background: #dbf1ea;
      border: 1px solid #cdcdcd;
      color: #5f5f5f;
      padding: 5PX 15PX;
      margin-right: 3vw;
      border-radius: 50PX;
    }
    .active{
      background: #99eecb;
      border-color: #48e5a2;
      color: #333333;
    }
  }

  .list_main{
    margin-top: 2vh;
    overflow-y: scroll;
    text-align: center;
    background: #ffffff;
    border-top-left-radius: 10PX;
    border-top-right-radius: 10PX;
    height: 88vh;
    .btn{
      background-image: linear-gradient(to right,#c6fe8b,#48e5a2);
      box-shadow: 0 0 10PX #cccccc;
      padding: 10PX 0PX;
      border-radius: 50PX;
      display: inline-block;
      width: 40%;
      margin-top: 2vh;
    }
  }

</style>
