<template>
  <div class="app-container">
    <van-nav-bar
      title="修改方案信息"
      fixed
      placeholder
      left-arrow
      @click-left="onClickLeft"
    />
    <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 name="radio" label="是否公开" input-align="right">
        <template #input>
          <van-radio-group v-model="form.isOpen" direction="horizontal">
            <van-radio name="Y">是</van-radio>
            <van-radio name="N">否</van-radio>
          </van-radio-group>
        </template>
      </van-field>
      <van-field label="方案名称" v-model="form.schemeName" input-align="right" label-width="auto" placeholder="请输入方案名称"/>
      <van-field
        readonly
        clickable
        name="picker"
        v-model="form.checkStartDate"
        label="检查开始日期"
        placeholder="点击选择开始日期"
        @click="showPicker = true"
        input-align="right"
        right-icon="calendar-o"
      />
      <van-popup v-model="showPicker" position="bottom">
        <van-datetime-picker
          v-model="showPicker"
          type="date"
          title="选择年月日"
          :min-date="minDate"
          :max-date="maxDate"
        />
      </van-popup>
      <van-field
        readonly
        clickable
        name="picker"
        v-model="form.checkEndDate"
        label="检查结束日期"
        placeholder="点击选择结束日期"
        @click="showPicker = true"
        input-align="right"
        right-icon="calendar-o"
      />
      <van-popup v-model="showPicker" position="bottom">
        <van-datetime-picker
          v-model="showPicker"
          type="date"
          title="选择年月日"
          :min-date="minDate"
          :max-date="maxDate"
        />
      </van-popup>
      <van-field label="检查方式" v-model="form.checkMethod" input-align="right" label-width="auto" placeholder="请输入检查方式"/>
      <van-field label="检查部门" v-model="form.checkDepartment" input-align="right" label-width="auto" placeholder="请输入检查部门"/>
      <van-field label="检查事项" v-model="form.checkList" input-align="right" label-width="auto" placeholder="请输入检查事项"/>
      <van-field label="检查类型" v-model="form.checkType" input-align="right" label-width="auto" placeholder="请输入检查类型"/>
      <van-field label="检察方案描述" input-align="right" label-width="auto" readonly/>
      <van-field v-model="form.checkContent" type="textarea" autosize placeholder="请输入检察方案描述"/>
    </div>

    <div class="main_box" style="margin-top: 10px;">
      <van-cell>
        <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>
      </van-cell>
      <div class="peopleList">
        <van-row>
          <van-col :span="4"><van-button @click="handleAddTEnforceSchemeHandler" icon="plus" type="primary" size="mini" style="display: block;margin: 0 auto;"/></van-col>
          <van-col :span="4">序号</van-col>
          <van-col :span="8">执行人员</van-col>
          <van-col :span="8">执法证号</van-col>
        </van-row>

        <van-row v-for="(item,index) in form.tEnforceSchemeHandlerList" :key="index" >
          <van-col :span="4"><van-button @click="handleDeleteTEnforceSchemeHandler(index)" icon="minus" type="danger" size="mini"/></van-col>
          <van-col :span="4"><van-field :value="index+1" readonly style="border: none"/></van-col>
          <van-col :span="8">
            <van-popover
              v-model="item.showPopover"
              trigger="click"
              :actions="item.enforcers"
              placement="top"
              @select="onSelect($event,index)"
            >
              <template #reference>
                <van-field v-model="item.name" readonly placeholder="人员姓名"/>
              </template>
            </van-popover>
          </van-col>
          <van-col :span="8"><van-field v-model="item.enforceNum" placeholder="执法证号"/></van-col>
        </van-row>
      </div>
    </div>

    <van-button type="default" class="loginOut" @click="submit">保存</van-button>

  </div>
</template>

<script>
  import { getScheme , updateScheme , getEnforcerList } from "@/api/lawEnforcement/index";
  export default {
    name: "programmeDetail",
    data() {
      return {
        showBankType:false,
        showPayeeType:false,
        showPicker:false,
        form:{},

        bankType:'',
        payeeType:'',
        value:'',

        showPopover:false,
        enforcers:[],
        actions: [{ text: '选项一' }, { text: '选项二' }, { text: '选项三' }],

        bankTypeOptions:[],
        payeeTypeOptions:[],
        minDate: new Date(2020, 0, 1),
        maxDate: new Date(2025, 10, 1),
        currentDate: new Date(2021, 0, 17),
      };
    },
    created() {

      // 执行人员列表查询
      getEnforcerList().then(response => {
        const data = response.enforcerList;
        for (let i = 0; i < data.length; i++) {
          const res = {
            enforcerId: data[i].id,
            text: data[i].name,
          };
          this.enforcers.push(res);
        }
      });

      this.getDictionaries()

    },
    methods: {
      onSelect(e,index) {
        this.form.tEnforceSchemeHandlerList[index].enforcerId = e.enforcerId;
        this.form.tEnforceSchemeHandlerList[index].name = e.text;
      },
      getDictionaries(){
        getScheme(this.$route.query.id).then((response) => {

          for( var i = 0 ; i < response.data.tEnforceSchemeHandlerList.length ; i++ ){

            response.data.tEnforceSchemeHandlerList[i].showPopover = false ;

            response.data.tEnforceSchemeHandlerList[i].enforcers = this.enforcers ;

          }

          this.form = response.data;

        });
      },
      /** 方案关联执法人员添加按钮操作 */
      handleAddTEnforceSchemeHandler() {
        var formLength = this.form.tEnforceSchemeHandlerList.length-1;
        var list = this.form.tEnforceSchemeHandlerList;
        if (list[formLength].enforcerId == '' || list[formLength].name == '' || list[formLength].enforceNum == '' ){
          this.$notify({ type: 'danger' , message: '请勿添加多条空数据，请正确填写后重试！' });
          return;
        }
        let obj = {};
        obj.enforcerId = "";
        obj.name = "";
        obj.enforceNum = "";
        obj.showPopover = false;
        obj.enforcers = this.enforcers;
        this.form.tEnforceSchemeHandlerList.push(obj);
      },
      /** 方案关联执法人员删除按钮操作 */
      handleDeleteTEnforceSchemeHandler(index) {
        this.form.tEnforceSchemeHandlerList.splice(index, 1);
      },
      submit(){
        updateScheme(this.form).then(response => {
          if (response.code == 200){
            this.$notify({ type: 'success' , message: '修改成功' });
          }
        });
      },
    },
  }
</script>

<style scoped lang="scss">
  .app-container {
    padding: 2% 0;
  }
  /deep/ .van-radio--horizontal{
    margin-left: 0.32rem;
    margin-right: 0;
  }
  .peopleList{
    padding-right: 3%;
    margin-top: 3%;
    .van-row{
      margin-bottom: 10PX;
      .van-col{
        text-align: center;
        font-size: 14PX;
        padding: 0 5PX;
        &:first-child{
          padding: 0.266667rem 0.426667rem;
          .van-field{
            border: none;
          }
        }
        .van-field{
          border: 1px solid #eee;
          /deep/ .van-field__control{
            text-align: center;
          }
        }
      }
      &:first-child{
        .van-col{
          color: #1D6FE9;
          line-height: 0.64rem;
          padding: 0;
        }
      }
    }
  }
  .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: 6px;
    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: 80%;
    margin: 0 auto;
    background-color: #1D6FE9;
  }

  .addFamily{
    position: absolute;
    top: -2px;
    right: 0;
    border-radius: 50%;
  }
  .loginOut{
    width: 88%;
    margin: 10PX 6%;
    border-radius: 8Px;
    box-shadow: 0PX 5PX 5PX #cccccc;
    border: none;
    background-color: #1D6FE9;
    color: #FFF;
  }
</style>
