<template>
  <div class="app-container">
    <div class="header_main">
      登记修改
      <div class="return_btn" @click="onClickLeft"></div>
    </div>
    <div style="height: 58px;"></div>

    <van-form @submit="goSubmit" :show-error-message="false" ref="form">
      <div class="main_box">
        <van-cell>
          <template #title>
            <van-icon name="../../../static/images/lawEnforcement/icon/icon_zxfa.png" size="20"></van-icon>
            <span class="custom-title">登记<i class="bgBlue"></i></span>
          </template>
        </van-cell>

        <van-field
          readonly
          clickable
          required
          :rules="[{ required: true , message:'请选择案件来源'  }]"
          v-model="caseSource"
          label="案件来源"
          placeholder="请选择案件来源"
          @click="showCaseSource = true"
          input-align="right"
          right-icon="arrow-down"
          :border="false"
          label-width="auto"
        />
        <van-popup v-model="showCaseSource" position="bottom">
          <van-picker
            show-toolbar
            :columns="caseSourceOptions"
            @confirm="onConfirmCaseSource"
            @cancel="showCaseSource = false"
          />
        </van-popup>

        <van-field
          readonly
          clickable
          v-model="scheme"
          label="关联方案"
          placeholder="请选择关联方案"
          @click="showScheme = true"
          input-align="right"
          right-icon="arrow-down"
          :border="false"
          label-width="auto"
        />
        <van-popup v-model="showScheme" position="bottom">
          <van-picker
            show-toolbar
            :columns="schemeOptions"
            @confirm="onConfirmScheme"
            @cancel="showScheme = false"
          />
        </van-popup>
  <!-- <van-field v-model="form.schemeId" label="关联方案" placeholder="请输入关联方案" :border="false" label-width="auto" input-align="right" />-->

        <van-field
          readonly
          clickable
          required
          :rules="[{ required: true , message:'请选择案件属地'  }]"
          v-model="deptName"
          label="案件属地"
          placeholder="请选择案件属地"
          @click="showDeptId = true"
          input-align="right"
          right-icon="arrow-down"
          :border="false"
          label-width="auto"
        />
        <van-popup v-model="showDeptId" position="bottom">
          <van-cascader
            v-model="villageValue"
            title="请选择案件属地"
            :options="deptOptions"
            @close="showDeptId = false"
            @finish="onConfirmDept"
            active-color="#1989fa"
            :field-names="hcAreaInfoFieldName"
          />
        </van-popup>
  <!--      <van-field v-model="form.deptId" label="案件属地" placeholder="请输入案件属地" :border="false" label-width="auto" input-align="right" is-link arrow-direction="down" />-->
        <van-field
          readonly
          clickable
          required
          :rules="[{ required: true , message:'请选择执行主体'  }]"
          v-model="belongTeam"
          label="执行主体"
          placeholder="请选择执行主体"
          @click="showBelongTeam = true"
          input-align="right"
          right-icon="arrow-down"
          :border="false"
          label-width="auto"
        />
        <van-popup v-model="showBelongTeam" position="bottom">
          <van-picker
            show-toolbar
            :columns="belongTeamOptions"
            @confirm="onConfirmBelongTeam"
            @cancel="showBelongTeam = false"
          />
        </van-popup>
  <!--      <van-field v-model="form.belongTeam" label="执行主体" placeholder="请输入执行主体" :border="false" label-width="auto" input-align="right" is-link arrow-direction="down" />-->
        <van-field v-model="form.caseName" required :rules="[{ required: true , message:'请输入案件名称'  }]" label="案件名称" placeholder="请输入案件名称" :border="false" label-width="auto" input-align="right" />

        <van-field
          readonly
          clickable
          required
          :rules="[{ required: true , message:'请选择登记日期'  }]"
          v-model="form.registerDate"
          label="登记日期"
          placeholder="请选择登记日期"
          @click="showRegisterDate = true"
          input-align="right"
          right-icon="arrow-down"
          :border="false"
          label-width="auto"
        />
        <van-popup v-model="showRegisterDate" position="bottom">
          <van-datetime-picker
            v-model="registerDate"
            type="date"
            title="选择年月日"
            :min-date="minDate"
            @confirm="onConfirmRegisterDate"
            @cancel="showRegisterDate = false"
          />
        </van-popup>

        <van-field
          readonly
          clickable
          required
          :rules="[{ required: true , message:'请选择执法类别'  }]"
          v-model="enforceCategory"
          label="执法类别"
          placeholder="请选择执法类别"
          @click="showEnforceCategory = true"
          input-align="right"
          right-icon="arrow-down"
          :border="false"
          label-width="auto"
        />
        <van-popup v-model="showEnforceCategory" position="bottom">
          <van-picker
            show-toolbar
            :columns="enforceCategoryOptions"
            @confirm="onConfirmEnforceCategory"
            @cancel="showEnforceCategory = false"
          />
        </van-popup>
  <!--      <van-field v-model="form.enforceCategory" label="执法类别" placeholder="请输入执法类别" :border="false" label-width="auto" input-align="right" is-link arrow-direction="down" />-->
        <van-field v-model="form.caseDescribe" label="案件简述" placeholder="请输入案件简述" :border="false" label-width="auto" input-align="right" />
      </div>

      <div class="main_box" style="margin-top: 10px;">
        <van-collapse v-model="activeNames">
          <van-collapse-item name="1">
            <template #title>
              <van-icon name="../../../static/images/lawEnforcement/icon/icon_dsr.png" size="20"></van-icon>
              <span class="custom-title">当事人<i class="bgBlue"></i></span>
            </template>

            <van-field v-model="form.type" label="当事人类型" :border="false" label-width="auto" input-align="right" >
              <template #input>
                <van-radio-group v-model="form.type" direction="horizontal">
                  <van-radio name="1">个人/个体工商户</van-radio>
                  <van-radio name="2">企业</van-radio>
                </van-radio-group>
              </template>
            </van-field>
            <template v-if="form.type=='1'">
              <van-field v-model="form.name" required :rules="[{ required: true , message:'请输入当事人姓名' }]" label="当事人姓名" placeholder="请输入当事人姓名" :border="false" label-width="auto" input-align="right" />
              <van-field v-if="form.type=='1'" v-model="form.sex" label="性别" placeholder="请输入性别" :border="false" label-width="auto" input-align="right" >
                <template v-if="form.type=='1'" #input>
                  <van-radio-group v-if="form.type=='1'" v-model="form.sex" direction="horizontal">
                    <van-radio name="1">男</van-radio>
                    <van-radio name="2">女</van-radio>
                  </van-radio-group>
                </template>
              </van-field>
              <van-field v-model="form.cardNum" required :rules="[{ required: true , message:'请输入身份证号' }]" label="身份证号" placeholder="请输入身份证号" :border="false" label-width="auto" input-align="right" />

              <van-field
                readonly
                clickable
                v-model="form.birthday"
                label="出生日期"
                placeholder="请选择出生日期"
                @click="showBirthday = true"
                input-align="right"
                right-icon="arrow-down"
                :border="false"
                label-width="auto"
              />
              <van-popup v-model="showBirthday" position="bottom">
                <van-datetime-picker
                  v-model="birthday"
                  type="date"
                  title="选择年月日"
                  :min-date="minDate"
                  @confirm="onConfirmBirthday"
                  @cancel="showBirthday = false"
                />
              </van-popup>

              <van-field
                readonly
                clickable
                v-model="nation"
                label="民族"
                placeholder="请选择民族"
                @click="showNation = true"
                input-align="right"
                right-icon="arrow-down"
                :border="false"
                label-width="auto"
              />
              <van-popup v-model="showNation" position="bottom">
                <van-picker
                  show-toolbar
                  :columns="nationOptions"
                  @confirm="onConfirmNation"
                  @cancel="showNation = false"
                />
              </van-popup>
    <!--          <van-field v-model="form.nation" label="民族" placeholder="请输入民族" :border="false" label-width="auto" input-align="right" is-link arrow-direction="down" />-->
              <van-field v-model="form.companyPosition" label="工作单位及职务" placeholder="请输入工作单位及职务" :border="false" label-width="auto" input-align="right" />
              <van-field v-model="form.zihao" label="字号名称" placeholder="请输入字号名称" :border="false" label-width="auto" input-align="right" />
            </template>
            <template v-if="form.type=='2'">
              <van-field v-model="form.companyName" :rules="[{ required: true , message:'请输入企业名称' }]" label="企业名称" placeholder="请输入企业名称" :border="false" label-width="auto" input-align="right" />
              <van-field v-model="form.legalName" label="法人/负责人" placeholder="请输入法人/负责人" :border="false" label-width="auto" input-align="right" />
            </template>

            <van-field v-model="form.phone" label="联系电话" placeholder="请输入联系电话" :border="false" label-width="auto" input-align="right" />
            <van-field v-model="form.address" label="联系地址" placeholder="请输入联系地址" :border="false" label-width="auto" input-align="right" />
            <van-field v-model="form.uniformCode" label="统一社会信用代码" placeholder="请输入统一社会信用代码" :border="false" label-width="auto" input-align="right" />
          </van-collapse-item>
        </van-collapse>
      </div>

      <div class="main_box" style="margin-top: 10px;">
        <van-collapse v-model="activeNames1">
          <van-collapse-item name="1">
            <template #title>
              <van-icon name="../../../static/images/lawEnforcement/icon/icon_ajwd.png" size="20"></van-icon>
              <span class="custom-title">附件<i class="bgBlue"></i></span>
            </template>
            <p style="color: #FE1313;text-align: center;margin-top: 5PX;font-size: .3rem">*请上传不超过5MB 格式为doc/xls/png/jpg/jpeg的文件</p>
            <div class="cf">
              <van-uploader v-model="form.attachementList" :after-read="afterReadEvidenceForm" :before-delete="deleteFileEvidenceForm" />
            </div>
          </van-collapse-item>
        </van-collapse>
      </div>

<!--      <div class="main_box" style="margin-top: 10px;">-->
<!--        <van-collapse v-model="activeNames2">-->
<!--          <van-collapse-item name="1">-->
<!--            <template #title>-->
<!--              <van-icon name="../../../static/images/lawEnforcement/icon/icon_zxry.png" size="20"></van-icon>-->
<!--              <span class="custom-title">执法人员<i class="bgBlue"></i></span>-->
<!--            </template>-->
<!--            <div class="peopleList">-->
<!--              <van-row class="peoplett">-->
<!--                <van-col :span="4">序号</van-col>-->
<!--                <van-col :span="6">执行人员</van-col>-->
<!--                <van-col :span="10">执法证号</van-col>-->
<!--                <van-col :span="4">带队人</van-col>-->
<!--              </van-row>-->
<!--              <van-row v-for="(item,index) in tEnforceCaseHandlerList" :key="index" >-->
<!--                <van-col :span="4">{{index+1}}</van-col>-->
<!--                <van-col :span="6">{{item.enforcerName}}</van-col>-->
<!--                <van-col :span="10">{{item.enforcerNum}}</van-col>-->
<!--                <van-col :span="4">-->
<!--                  <div class="icon_box">-->
<!--                    <van-checkbox v-model="item.enforcerLeader" shape="square" @change="checkBoxChange(item.enforcerLeader,index)"></van-checkbox>-->
<!--                    <van-icon name="../../../static/images/lawEnforcement/icon/icon_delete.png" size="20" @click="removePeople(index)"></van-icon>-->
<!--                  </div>-->
<!--                </van-col>-->
<!--              </van-row>-->
<!--              <img src="../../../../static/images/lawEnforcement/new/addPeop_btn.png" style="margin: 0 auto;" @click="$router.push({name:'lawEnforcementCaseLawEnforcer'})">-->
<!--            </div>-->
<!--          </van-collapse-item>-->
<!--        </van-collapse>-->
<!--      </div>-->
      <div class="submit_box">
        <p class="submitButton" @click="$refs.form.submit()">保存</p>
        <p class="submitButton" @click="keepSubmit()">提交</p>
      </div>
    </van-form>

  </div>
</template>

<script>
  import { updateCase , addProgress , treeselect , schemeList , addSurvey , getCase , getDeptName , getSurveyByCaseId , commonUpload } from "@/api/lawEnforcement/index";
  import Cookies from "js-cookie";
  export default {
    name: "programmeDetail",
    data() {
      return {
        showCaseSource:false,
        showBelongTeam:false,
        showEnforceCategory:false,
        showDeptId:false,
        showScheme:false,
        showNation:false,
        showBirthday:false,
        showRegisterDate:false,
        form:{
          type:'1',
          sex:'1',
          registerDate:this.format(new Date(),'yyyy-MM-dd'),
          caseProgress:'1',
          caseStatus:'1',
        },
        registerDate:new Date(),
        birthday:new Date(),
        caseSource : '',
        belongTeam : '',
        enforceCategory : '',
        nation : '',
        deptName : '',
        scheme : '',
        villageValue : '',

        tEnforceSchemeHandlerList:[],
        minDate: new Date(1900, 0, 1),

        activeNames: ['1'],
        activeNames1: ['1'],
        activeNames2: ['1'],

        caseSourceOptions:[],
        belongTeamOptions:[],
        enforceCategoryOptions:[],
        nationOptions:[],
        deptOptions:[],
        caseProgressOptions:[],
        schemeOptions:[],

        tEnforceCaseHandlerList:[],
        radio:'',
        fileList: [],
        hcAreaInfoFieldName: {
          text: "label",
          value: "value",
          children: "children",
        },

        deptId:''
      };
    },
    created() {
      //案件来源
      this.getDicts("case_source").then(response => {
        for (var i = 0; i < response.data.length; i++) {
          this.caseSourceOptions.push({text: response.data[i].dictLabel, value: response.data[i].dictValue});
        }
        this.caseSourceOptions2 = response.data;
      });
      //执行主体
      this.getDicts("team_category").then(response => {
        for (var i = 0; i < response.data.length; i++) {
          this.belongTeamOptions.push({text: response.data[i].dictLabel, value: response.data[i].dictValue});
        }
        this.belongTeamOptions2 = response.data;
      });
      //执法类别
      this.getDicts("enforce_category").then(response => {
        for (var i = 0; i < response.data.length; i++) {
          this.enforceCategoryOptions.push({text: response.data[i].dictLabel, value: response.data[i].dictValue});
        }
        this.enforceCategoryOptions2 = response.data;
      });
      //民族
      this.getDicts("nationality").then(response => {
        for (var i = 0; i < response.data.length; i++) {
          this.nationOptions.push({text: response.data[i].dictLabel, value: response.data[i].dictValue});
        }
        this.nationOptions2 = response.data;
      });
      this.getDicts("case_node").then(response => {
        this.caseProgressOptions = response.data;
      });
      var schemeQueryParam = {
        applyStatus: "1"
      };
      schemeList(schemeQueryParam).then((response) => {
        for (var i = 0; i < response.rows.length; i++) {
          this.schemeOptions.push({text: response.rows[i].schemeName, value: response.rows[i].id});
        }
      });
      this.getBookList()
      var that = this;
      setTimeout(function () {
        that.getDetail();
      },2000)
    },
    methods: {
      goSubmit(){
        if (typeof this.form.attachement != 'string'){
          this.form.attachement = this.form.attachement.join(',');
        }
        updateCase(this.form).then(response => {
          if(response.code=="200"){
            this.$notify({ type: 'success', message: '保存成功' });
            setTimeout(function(){
              history.back(-1);
            },2000)
            // var progressParam = {
            //   caseId: response.data,
            //   caseStatus:"1",
            //   caseProgress:"1",
            //   caseProgressName:"登记"
            // };
            //
            // addProgress(progressParam).then(response2 => {
            //   let surveyForm = {};
            //   surveyForm.tEnforceCaseHandlerList = this.tEnforceCaseHandlerList;
            //   surveyForm.caseId = response.data;
            //   addSurvey(surveyForm).then(response => {
            //   });
            // });
          }
        });
      },
      keepSubmit(){
        this.form.caseId = this.form.id;
        this.form.caseStatus = "1";
        this.form.caseProgress = "2"; // 勘察
        this.form.caseProgressName = this.selectDictLabel(this.caseProgressOptions, this.form.caseProgress); // 勘察

        if (typeof this.form.attachement != 'string'){
          this.form.attachement = this.form.attachement.join(',');
        }
        var _this = this;
        this.$dialog.confirm({
          message: '提交后案件将进入后续流程并且不能修改,是否确认提交?',
        }).then(function () {
          // 更新案件的节点和状态
          updateCase(_this.form).then(response => {
            _this.$notify({ type: 'success', message: '提交成功' });
            _this.goSubmit();
          });
        });
      },
      getBookList(){
        treeselect().then((response) => {
          if (response.code == 200) {
            this.deptOptions = response.data;
          }
        })
      },
      //选择案件来源
      onConfirmCaseSource(data){
        this.form.caseSource = data.value;
        this.caseSource = data.text;
        this.showCaseSource = false;
      },
      //选择案件属地
      onConfirmDept({ selectedOptions }){
        console.log(selectedOptions[selectedOptions.length-1])
        this.form.deptId = selectedOptions[selectedOptions.length-1].value;
        this.deptName = selectedOptions[selectedOptions.length-1].label;
        this.showDeptId = false
      },
      //选择执行主体
      onConfirmBelongTeam(data){
        this.form.belongTeam = data.value;
        this.belongTeam = data.text;
        this.showBelongTeam = false;
      },
      //选择执法类别
      onConfirmEnforceCategory(data){
        this.form.enforceCategory = data.text;
        this.enforceCategory = data.text;
        this.showEnforceCategory = false;
      },
      //选择民族
      onConfirmNation(data){
        this.form.nation = data.value;
        this.nation = data.text;
        this.showNation = false;
      },
      //选择关联方案
      onConfirmScheme(data){
        this.form.schemeId = data.value;
        this.scheme = data.text;
        this.showScheme = false;
      },
      //选择登记时间
      onConfirmRegisterDate(data){
        this.form.registerDate = this.format(data,'yyyy-MM-dd');
        this.registerDate = data;
        this.showRegisterDate = false;
      },
      //选择出生日期
      onConfirmBirthday(data){
        this.form.birthday = this.format(data,'yyyy-MM-dd');
        this.birthday = data;
        this.showBirthday = false;
      },
      getDetail(){
        getCase(this.$route.query.id).then((response) => {
          this.caseSource = response.data.caseSource == '' ? '' : this.selectDictLabel(this.caseSourceOptions2, response.data.caseSource);
          this.belongTeam = response.data.belongTeam == '' ? '' : this.selectDictLabel(this.belongTeamOptions2, response.data.belongTeam);
          this.nation = response.data.nation == '' ? '' : this.selectDictLabel(this.nationOptions2, response.data.nation);
          console.log(response.data.schemeId)
          console.log(this.schemeOptions)
          this.scheme = response.data.schemeId == null ? '' : this.schemeOptions.filter(function (e) { return e.value == response.data.schemeId; })[0].text;
          this.enforceCategory = response.data.enforceCategory;
          if (response.data.attachement){
            response.data.attachementList = [];
            var attachement = response.data.attachement.split( "," );
            attachement.forEach(responseAttach=>{
              response.data.attachementList.push({
                url:'/api' + responseAttach,
                isImage: true
              });
            })
            // process.env.VUE_APP_BASE_ROUTING_URL + process.env.VUE_APP_BASE_API
          }else{
            response.data.attachement = [];
          }
          getDeptName(response.data.deptId).then(res => {
            this.deptName = res.data.deptName
          });
          this.form = response.data;
        });
        getSurveyByCaseId(this.$route.query.id).then((response) => {
          if(response.data != undefined){
            this.tEnforceCaseHandlerList = response.data.tEnforceCaseHandlerList;
          }
        });
      },
      afterReadEvidenceForm(file){
        let params1 = new FormData();
        params1.append("file", file.file);
        commonUpload(params1).then((r1) => {
          // this.tEnforceSamplingGoodsList[index].attachement.push(r1.fileName);
          this.form.attachement.push(r1.fileName)
        })
      },
      deleteFileEvidenceForm(file,detail) {
        this.form.attachement.splice(detail.index,1);
        this.form.attachementList.splice(detail.index,1);
      },
    },
    watch: {
      $route (to, from ) {
        // 监听路由变化, 实现类似 小程序的 onShow 事件
        if (to.path === '/lawEnforcement/caseAdd') {
          console.log('abc')
          // do anything you want
          if (Cookies.get('enforcer')){
            JSON.parse(Cookies.get('enforcer')).map((res,index)=>{
              this.tEnforceCaseHandlerList.push(res)
            })
          }
        }
      }
    },
  }
</script>

<style scoped lang="scss">
  .app-container {
    padding: 2% 0;
  }
  /deep/ .van-collapse-item__content{
    padding: 0;
  }
  .cf{
    padding: 0 3%;
    margin-top: 20PX;
    margin-bottom: 20PX;
    .van-row{
      background: #F0F3F5;
      .van-col{
        padding: 5PX 0;
        font-size: 12PX!important;
        text-align: center;
        p{
          color: #1D6FE9;
        }
      }
      &:first-child{
        background: transparent;
      }
    }
  }
  .header_main{
    height: 116px;
    background: url('../../../../static/images/lawEnforcement/new/list_head.png') no-repeat;
    background-size: 100% 100%;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    font-size: 36px;
    line-height: 116px;
    text-align: center;
    color: #fff;
    z-index: 999;
    .return_btn{
      width: 24px;
      height: 43.2px;
      background: url('../../../assets/images/sunVillage_info/list_icon_5.png') center center no-repeat;
      background-size: 20px 36px;
      position: absolute;
      left: 38px;
      top: 36px;
    }
    .add_btn{
      width: 56.4px;
      height: 40.8px;
      background: url('../../../assets/images/sunVillage_info/list_icon_9.png') center center no-repeat;
      background-size: 47px 34px;
      position: absolute;
      right: 38px;
      top: 36px;
    }
  }
  /deep/ .van-radio--horizontal{
    margin-left: 0.32rem;
    margin-right: 0;
  }
  .peopleList{
    padding: 3%;
    text-align: center;
    .van-row{
      margin-bottom: 10PX;
      background: rgba(196,218,249,0.3);
      border-radius: 5PX;
      .van-col{
        text-align: center;
        font-size: 14PX;
        color: #333333;
        padding: 5PX 0;
      }
      &:first-child{
        background: none;
        .van-col{
          padding: 0;
          color: #1D6FE9;
        }
      }
    }
    .icon_box{
      display: flex;
      justify-content: space-around;
      /deep/ .van-checkbox{
        justify-content: center;
      }
    }
  }
  .main_title{
    font-size: 0.4rem;
    color: #1D6FE9;
    margin: 0.2rem 6%;
    margin-top: 0;
    position: relative;
  }
  .main_box{
    width: 96%;
    margin: 0 auto;
    border-radius: 10PX;
    box-shadow: 0px 3px 6px 0px rgba(0,0,0,0.16);
    overflow: hidden;
    background-color: #FFF;
    .van-icon{
      vertical-align: middle;
    }
    .custom-title{
      font-size: 17PX;
      color: #333333;
      vertical-align: middle;
      line-height: 1;
      position: relative;
    }
    .tap{
      color: #1D6FE9;
    }
    .bgBlue{
      display: block;
      position: absolute;
      width: 17PX;
      height: 17PX;
      border-radius: 50%;
      background-color: rgba(29,111,233,0.26);
      top: -2PX;
      right: -8PX;
    }
  }
  .submitButton{
    width: 40%;
    margin: 0 auto;
    background-image: linear-gradient(to right, #2E79E9 , #77A6EF);
    text-align: center;
    color: #ffffff;
    height: 70px;
    line-height: 70px;
    border-radius: 8PX;
    margin-top: 25PX;
  }
  .submit_box{
    display: flex;
  }
  .addFamily{
    position: absolute;
    top: -2px;
    right: 0;
    border-radius: 50%;
  }
</style>
