<template>
  <el-dialog title="审核业务" :close-on-click-modal="true" v-if="visible" :visible.sync="visible" :before-close="handleDialogClose">
    <el-form :model="model" ref="form1" label-width="120px">
      <el-row :gutter="26">
        <el-col :span="16" v-if="isLast">
          <el-form-item label="审核角色" prop="data1" class="is-required">
            <el-select filterable v-model="model.data1" :multiple="ismultiple" @remove-tag="removeTag" placeholder="请选择..." @change="role">
              <el-option v-for="item in options1" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="16" v-if="isLast">
          <el-form-item label="审核人" verify prop="nextDealMan" class="is-required">
            <el-select filterable v-model="model.nextDealMan" :multiple="ismultiple" placeholder="请选择...">
              <el-option v-for="item in options2" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="16" v-if="type==2">
          <el-form-item label="审核意见" prop="content" verify :maxLength="100" class="is-required">
            <el-input type="textarea" v-model="model.content"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="16" v-if="type==2">
          <el-form-item label="常用意见" prop="nextDealMan1">
            <el-select filterable v-model="model.nextDealMan1" @change="changeContent" placeholder="请选择...">
              <el-option v-for="item in options3" :key="item.adviceId" :label="item.adviceInfo" :value="item.adviceId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="16" v-if="isLast || type === 2">
          <el-form-item>
            <el-checkbox v-model="checked" @change="changeIsQRCodeTrack">同时短信通知</el-checkbox>
          </el-form-item>
        </el-col>
          <el-col :span="16" v-if="!isLast && checked">
              <el-form-item label="短信接收人名单">
                  <div class="textarea" :disabled="false">
                      <el-tag :key="tag.id" style="margin-left:6px;" v-for="tag in messageReceiveList">
                          {{tag.name}}
                      </el-tag>
                  </div>
              </el-form-item>
          </el-col>
      </el-row>

    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button type="primary" v-if="type==2" @click="addSer()">添加到常用意见</el-button>
      <el-button v-if="type==1||type==3" @click="visible = false">关闭</el-button>

      <el-button v-if="type==2" @click="dataFormSubmit('no')">不同意</el-button>
      <el-button type="primary" @click="dataFormSubmit('yes')">{{btn}}</el-button>

    </span>
  </el-dialog>
</template>


<script>
export default {
  data() {
    return {
      checked: false,
      messageReceiveList: [],
      formAction: 0, //0 add,//1,edit
      visible: false,
      title: '添加',
      disabled: false,
      btn: '提交',
      ismultiple: false,
      type: 1,
      aproveVisible: false,
      isLast: true,
      taskId: '',
      model: {
        content: '',
        nextDealMan: -1,
        isAccount: 1,
        isQRCodeTrack:'',
      },

      options1: [],
      options2: [],
      options3: [],
      value5: [],
    }
  },
  mounted() {
    // this.model.data2 = this.$store.getters.getLogin("username");
    // this.model.data4 = this.$store.getters.getLogin("userId")
  },
  activated() {},
  methods: {
    init(type, procdefkey, procinstid, resolution, taskId) {
      //type1  申请  2 中间环节  3.驳回
      console.log(arguments)
      this.type = type
      console.log(99, this.type)
      this.model = {
        content: '',
        nextDealMan: -1,
        isAccount: 1,
      }
      this.taskId = taskId
      this.options1 = []
      this.options2 = []
      this.options3 = []
      this.value5 = []
      if (type == 1 || resolution != undefined || type == 3) {
        this.btn = '提交'
      } else {
        this.btn = '同意'
      }
      this.procdefkey = procdefkey

      this.procinstid = procinstid
      this.resolution = resolution
      this.visible = true
      if (this.$refs.form1) {
        this.$refs.form1.resetFields()
      }
      this.doSelect()
      this.doserSelect()
      if (procinstid !== null && procinstid !== undefined) {
        this.doSelectMessageReceiveList()
      }
    },

    dataFormSubmit(type) {
      console.log(120, type)
      if ((this.isLast && !this.model.data1) || (this.model.data1 && this.model.nextDealMan==-1)) return this.$message.error('审核角色或审核人不能为空！')
      let self = this
      // console.log(122,this.ismultiple)
      var model = JSON.parse(JSON.stringify(this.model))
      if (this.ismultiple) {
        model.nextDealManList = model.nextDealMan.join(',')
        model.nextDealMan = -1
      }
      console.log(128, model)
      if (type == 'no') {
        if (!model.content) {
          this.$message('请填写驳回原因')
          return false
        }
        model.approve = type
        self.$parent.dataFormSubmit(model)
        self.visible = false
      } else {
        self.$refs['form1'].validate((valid) => {
          if (valid) {
            model.approve = type
            if(!self.isLast) {
              model.nextDealMan = -1
            }
            self.$parent.dataFormSubmit(model)

            self.visible = false
          } else {
            return false
          }
        })
      }
    },
    handleDialogClose() {
      // console.log(self.$parent)

      this.$parent.visible = false
      this.visible = false
    },
    doserSelect() {
      let self = this
      this.common.selectInit(this.$url.findAdvice.getList, success, { pageNum: 1, pageSize: 20 }, 'options3')
      function success(data, str) {
        self[str] = data.rows
      }
    },
    doSelect() {
      let self = this
      let obj = {}

      if (this.type == 1) {
        obj = { procdefkey: this.procdefkey }
      } else {
        obj = { procinstid: this.procinstid, approve: 'yes' }
        if (this.resolution || this.resolution == 0) {
          obj.resolution = this.resolution
        }
        if (this.taskId) {
          obj.taskId = this.taskId
        }
      }
      console.log(186, obj)
      this.common.selectInit(this.$url.activeti.getRole, success, obj, 'options1', error)
      function error(data) {
        // self.$parent.aproveVisible = false
        // self.$message.error('错误，请重新审批！')
      }

      function success(data, str) {
        //  multiple
        console.log(190, data)
        if (data) {
          if (data.tasktype != 4) {
            self.isLast = false
          } else {
            // data.activitybehavior = 'SequentialMultiInstanceBehavior'

            if (data.activitybehavior == 'SequentialMultiInstanceBehavior' || data.activitybehavior == 'ParallelMultiInstanceBehavior') {
              self.ismultiple = true
              // console.log(199);
              // console.log(200,data.rows[0].id)
              self.model.data1 = [data.rows[0].id]

              self.role([data.rows[0].id])
            } else {
              // console.log(200,data.rows[0].id)
              // 如果返回审核角色和审核人 没有  加这层判断 解决前端报id为undefined的问题
              if (data.rows.length != 0) {
                self.ismultiple = false
                self.model.data1 = data.rows[0].id

                self.role(data.rows[0].id)
              }
              // self.ismultiple = false;
              // self.model.data1 = data.rows[0].id;

              // self.role(data.rows[0].id)
            }
            self[str] = data.rows
            self.isLast = true
          }
        } else {
          self.isLast = false
          self.ismultiple = false
        }
      }
    },
    role(row) {
      var str = ''

      if (typeof row == 'object') {
        if (row.length == 0) {
          return false
        }
        str = row.join(',')
      } else {
        if (!row) {
          return false
        }
        str = String(row)
      }
      let self = this
      let obj = {
        roleId: str,
        excludeMeWhenSelectNextHandler: self.common.excludeMeWhenSelectNextHandler,
        excludeFormerHanlder: self.common.excludeMeWhenSelectNextHandler,
        procinstid: self.procinstid,
      }
      this.common.selectInit(this.$url.activeti.getUsersByRoleIds, success, obj, 'options2')

      function success(data, str) {
        if (data.length != 0) {
          if (self.ismultiple) {
            self.nextDeal(data[0].id)
          } else {
            self.model.nextDealMan = data[0].id
          }
          self[str] = data
        } else {
          self.model.nextDealMan = []
          self.$message('该角色下审核人为空')
        }

        // if( self.ismultiple){
        //   self.nextDeal(data[0].id);
        // }else{

        //   self.model.nextDealMan  = data[0].id;
        // }
        // self[str] = data;
      }
    },
    removeTag() {
      this.model.nextDealMan = -1
    },
    nextDeal(id) {
      if (typeof this.model.nextDealMan != 'object') {
        this.model.nextDealMan = []
      }
      var tag = this.model.nextDealMan.find((i) => {
        return i == id
      })
      if (!tag) {
        this.model.nextDealMan.push(id)
      }
    },
    addSer() {
      let self = this
      if (!this.model.content) {
        this.$message('审核意见为空')
        return false
      }
      let urls = null

      urls = this.$url.findAdvice.add

      var obj = {
        url: urls,
        data: {
          adviceInfo: this.model.content,
        },
      }
      this.common.httpPost(obj, success)
      function success(data) {
        self.$message.success('已添加')
        self.doserSelect()
      }
    },
    changeContent(value) {
      let obj = this.options3.find((element) => {
        return element.adviceId == value
      })
      this.model.content = obj.adviceInfo
    },
    changeIsQRCodeTrack(checked) {
          if(checked){
              this.model.isQRCodeTrack = 1;
          } else {
              this.model.isQRCodeTrack = 0;
          }
     },
      doSelectMessageReceiveList(){
          let self = this;
          var obj = {
              url: self.$url.activeti.getMessageReceiveList,
              data: {
                  procinstid: self.procinstid,
              },
          }
          self.common.httpPost(obj, success)
          function success(data) {
              console.log("获取短信接收人",data);
              self.messageReceiveList = data.list;
          }
      },
  },
}
</script>
<style>
.el-textarea__inner {
  height: 100px;
}
.textarea {
    padding: 4px;
    min-height: 100px;
    width: 100%;
    color: #606266;
    background-color: #fff;
    background-image: none;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    box-sizing: border-box;
}
</style>
