<template>
  <view>
    <cu-custom bgColor="bg-gradual-blueness" :isBack="true">
      <block slot="backText">返回</block>
      <block slot="content">人员入场申请</block>
    </cu-custom>
    <view class="form">
      <view class="wui-form-list">
        <view class="cu-form-group">
          <view class="title">来源单位：</view>
          <input :disabled="forbidEdit" v-model="form.SOURCE_UNIT" placeholder="请输入编号"></input>
        </view>
        <view class="cu-form-group">
          <view class="title">入场人数：</view>
          <input :disabled="forbidEdit" v-model="form.ADMISSION_NUMBER" placeholder="请输入编号"></input>
        </view>
        <view class="cu-form-group">
          <view class="title">申请用时：</view>
          <input :disabled="forbidEdit" v-model="form.APPLICATION_TIME" placeholder="请输入编号"></input>
        </view>
        <view class="cu-form-group">
          <view class="title">审核人：</view>
          <input :disabled="forbidEdit" v-model="form.EXAMINE_PEOPLE"></input>
        </view>
        <view class="cu-form-group">
          <view class="title">审批人：</view>
          <input :disabled="forbidEdit" v-model="form.APPROVE_PEOPLE"></input>
        </view>
        <view class="cu-form-group">
          <view class="title">确认人：</view>
          <input :disabled="forbidEdit" v-model="form.CONFIRM_PEOPLE"></input>
        </view>
        <view class="cu-form-group">
          <view class="title">状态：</view>
          <view>
          {{translate(form.STATE)}}
          </view>
        </view>
        <view class="cu-form-group">
          <view class="title">入场事由：</view>
          <input :disabled="forbidEdit" v-model="form.SUBJECT_MATTER"></input>
        </view>
        <view v-if="form.REJECTOPINION && form.STATE === '6'"  class="cu-form-group">
          <view class="title">打回原因：</view>
          <input :disabled="forbidEdit" v-model="form.REJECTOPINION"></input>
        </view>
      </view>

      <view class="wui-form-list" style="padding-bottom: 10upx;">
        <view class="cu-form-group">
          <view class="title">来访人员列表：</view>
        </view>
        <view v-for="(item,index) of list" :key="item.OUTSIDERS_DETAILS_ID" :data-id="item.OUTSIDERS_DETAILS_ID" class="add_pard_item">
          <view class="cu-form-group">
            <text>姓名:{{item.NAME}}</text>
          </view>
          <view class="cu-form-group">
            <text>手机号:{{item.PHONO_NUMBER}}</text>
          </view>
        </view>
      </view>
    </view>
    <view class="cu-bar btn-group" style="margin-top: 30upx;">
      <button v-if="!forbidEdit" class="cu-btn bg-blue margin-tb-sm lg" @click="$noMultipleClicks(goSubmit)">提交
      </button>
      <button v-if="forbidEdit" class="cu-btn bg-green margin-tb-sm lg" @click="$noMultipleClicks(goback)">返回</button>
    </view>
    <view class="padding flex flex-direction"></view>
  </view>
</template>

<script>
import {
  basePath,
  corpinfoId,
  loginUser,
  loginSession,
  formatDate,
  baseImgPath
} from '@/common/tool.js';
import tkiTree from "@/components/select-tree/select-tree.vue"
import ruiDatePicker from '@/components/rattenking-dtpicker/rattenking-dtpicker.vue';
import sign from '@/components/sign/sign.vue';

export default {
  components: {
    tkiTree,
    ruiDatePicker,
    sign
  },
  data() {
    return {
      noClick: true,
      BLINDBOARD_ID: '',
      msg:'add',
      list: [],
      form: {
        SOURCE_UNIT: '',
        LICENSE_PLATE: '',
        MODEL: '',
        NUMBER_PEOPLE_CAR: '',
      },
      statusList: [
        {
          id: '0',
          name: '未提交'
        },
        {
          id: '1',
          name: '待审核'
        },
        {
          id: '2',
          name: '待审批'
        },
        {
          id: '3',
          name: '待确认'
        },
        {
          id: '4',
          name: '进场'
        },
        {
          id: '5',
          name: '出场'
        },
        {
          id: '6',
          name: '已打回'
        },
        {
          id: '7',
          name: '待归档'
        },
        {
          id: '8',
          name: '已归档'
        },
      ],
      rules: [
        {name: 'SOURCE_UNIT', message: '请输入来源单位'},
        {name: 'LICENSE_PLATE', message: '请输入车牌号'},
        {name: 'STARTTIME', message: '请选择开始时间'},
        {name: 'ENDTIME', message: '请选择结束时间'},
        {name: 'MODEL', message: '请输入车型'},
        {name: 'NUMBER_PEOPLE_CAR', message: '请输入人员数'},

      ],
      treeNode: [],
      forbidEdit: false,
      todayDate: formatDate(new Date(), 'yyyy-MM-dd hh:mm'),
      modalShow: false,
      imgList: [],
      workUnitManagerList: [],
      branchApproverList: [],
    }
  },
  onLoad(event) {
    if (event.id) {
      console.log(event.id)
      this.OUTSIDERS_ID = event.id
      this.forbidEdit = true
      this.getData();
    }
  },
  methods: {
    getData() {
      var _this = this;
      uni.showLoading({
        title: '请稍候'
      })
      uni.request({
        url: basePath + '/app/outsiders/goEdit',
        method: 'POST',
        header: {
          'Content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          OUTSIDERS_ID: this.OUTSIDERS_ID,
          CORPINFO_ID :corpinfoId,
        },
        success: (res) => {
          if ("success" === res.data.result) {
            this.form = res.data.pd;
            this.list = res.data.pd.userList;
            console.log(this.form)
            uni.hideLoading();
          } else if ("exception" === res.data.result) {
            uni.showToast({
              title: '错误',
              duration: 2000
            });
          }
        }
      });
    },
    goSubmit() {
      var _this = this;
      let required = true
      uni.showLoading({
        title: '请稍候'
      })
      this.rules.map(({name,message})  => {
        if (!this.form[name]) {
          uni.showToast({
            icon: 'none',
            title: message,
            duration: 1500
          });
          required = false
        }
      })
      if (!required) {
        return
      }
      const formData={}
      Object.keys(this.form).map(key => {
        formData[key]=this.form[key]
      })
      formData.CORPINFO_ID=corpinfoId
      formData.loginUserId=loginUser.USER_ID
      formData.loginUserName=loginUser.USERNAME
      formData.DEPARTMENT_ID=loginUser.DEPARTMENT_ID,
      uni.request({
        url: basePath + "/app/foreigncar/"+_this.msg,
        method: 'POST',
        dataType: 'json',
        header:{
          'Content-type':'application/x-www-form-urlencoded'
        },
        data: formData,
        success: (res) => {
          uni.showToast({
            icon:'none',
            title: '保存成功',
            duration: 2000
          });
          _this.goback()
        },
        fail: (err) => {
          uni.hideLoading();
          uni.showModal({
            content: err.errMsg,
            showCancel: false
          });
        }
      })
    },
    changeJobStartTime(e) {
      this.form.STARTTIME = e
    },
    changeJobEndTime(e) {
      this.form.ENDTIME = e
    },
    zgtreeCancel(e) {
      this.isUps = false;
    },
    translate(id) {
      for (var i = 0; i < this.statusList.length; i++) {
        if (this.statusList[i].id == id) return this.statusList[i].name
      }
    },
    getUserList(list, DEPARTMENT_ID) {
      //发送 post 请求
      var _this = this;
      uni.request({
        method: 'POST',
        dataType: 'json',
        header: {
          'Content-type': 'application/x-www-form-urlencoded'
        },
        url: basePath + '/app/sys/listUser',
        data: {
          DEPARTMENT_ID, NOMAIN:'1',
          tm: new Date().getTime()
        },
        success: function (res) {
          if ("success" == res.data.result) {
            _this[list] = res.data.userList;
          } else {
            uni.showToast({
              title: res.data.message,
              duration: 2000
            });
          }
        }
      })
    },
    chooseLocation() {
      var _this = this
      uni.navigateTo({
        url: '/pages/map/map',
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          acceptDataFromOpenedPage: function (e) {
            _this.form.LONGITUDE = e.data.longitue;
            _this.form.LATITUDE = e.data.latitude;
          }
        },
      })
    },
    ChooseImage() {
      var _this = this;
      uni.chooseImage({
        count: 4, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['camera', 'album'], //从相册选择
        success: (res) => {
          if (this.form.imgList.length != 0) {
            this.form.imgList = this.form.imgList.concat(res.tempFilePaths)
          } else {
            this.form.imgList = res.tempFilePaths
          }
        }
      });
    },
    ViewImage(e) {
      uni.previewImage({
        urls: this.form.imgList,
        current: e.currentTarget.dataset.url
      });
    },
    DelImg(e) {
      uni.showModal({
        title: '双控平台',
        content: '确定要删除这张图片吗？',
        cancelColor: "#000000",
        cancelText: '取消',
        confirmText: '确定',
        success: res => {
          if (res.confirm) {
            this.imgList.splice(e.currentTarget.dataset.index, 1)
          }
        }
      })
    },
    subCanvas (e) {
      this.imgList.splice(0, this.imgList.length);
      this.imgList.push(e);
      this.modalShow = false;
    },
    ViewImage1 (e) {
      let files = [];
      for (var i = 0; i < this.imgList.length; i++) {
        files.push(this.imgList[i].filePath)
      }
      uni.previewImage({
        urls: files,
        current: e.currentTarget.dataset.url
      });
    },
    DelImg1(e) {
      uni.showModal({
        title: '双控平台',
        content: '确定要删除这张图片吗？',
        cancelColor: "#000000",
        cancelText: '取消',
        confirmText: '确定',
        success: res => {
          if (res.confirm) {
            this.form.imgList.splice(e.currentTarget.dataset.index, 1)
          }
        }
      })
    },
    goback() {
      uni.navigateBack({
        delta: 1
      });
      uni.hideLoading();
    },
  }
}
</script>

<style>
.right_icon {
  position: relative;
  padding-right: 36upx;
  width: 200upx;
  text-align: right;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.right_icon ::after {
  font-family: cuIcon;
  display: block;
  content: "\e6a3";
  position: absolute;
  font-size: 17px;
  color: #8799a3;
  line-height: 50px;
  width: 30px;
  text-align: center;
  top: -28upx;
  bottom: 0;
  right: -22upx;
  margin: auto;
}

.modal_content {
  display: flex;
  text-align: left;
}

.modal_left {
  flex-basis: 50%;
  padding: 50upx 20upx;
  border-right: 1px solid #000000;
}

.modal_right {
  flex-basis: 50%;
  padding: 50upx;
}

.modal_left_title, .modal_right_title {
  font-size: 16px;
  color: #000000;
  font-weight: 700;
}

.checkbox-item {
  display: flex;
  align-items: center;
  padding: 0 30upx;
  min-height: 100upx;
  background-color: #ffffff;
  border-bottom: 1upx solid #ddd;
}

.checkbox-item:last-child {
  border-bottom: none;
}
</style>
