<template>
  <div>
    <vxe-modal :value="isShow"
               show-footer
               :z-index="zIndex"
               title="转办"
               show-zoom
               transfer
               resize
               esc-closable
               :fullscreen="$store.getters.device === 'mobile'"
               show-confirm-button
               :width="defaultModalSize.width"
               :height="defaultModalSize.height"
               @input="e => $emit('update:isShow',e)">
      <div>
        <div>
          <h4>本部门</h4>
          <el-radio-group v-model="form.user_id">
            <el-radio v-for="(user, index) in config.users" :key="user.id" :label="user.id">{{ user.name }}</el-radio>
          </el-radio-group>
        </div>

        <div v-for="(group, index) in config.groups" :key="group.id">
          <h4>{{ group.name }}</h4>
          <el-radio-group v-model="form.user_id">
            <el-radio v-for="(user, index1) in group.users" :key="user.id" :label="user.id">{{ user.name }}</el-radio>
          </el-radio-group>
        </div>

        <el-divider></el-divider>

        <div>
          <h4>转办意见</h4>
          <el-input v-model="form.forwarded_comment" type="textarea" :autosize="{ minRows: 2 }"></el-input>
        </div>
      </div>

      <template #footer>
        <div style="margin-top: 20px;display: flex;justify-content: center;">
          <el-button type="primary" size="small" @click="submit">确认转办 <i class="el-icon-arrow-right"></i> </el-button>
        </div>
      </template>
    </vxe-modal>
  </div>
</template>

<script>
import { preForward, forward } from '@/api/flow'
import { PopupManager } from 'element-ui/lib/utils/popup'
import {defaultModalSize} from "@/settings";

export default {
  props: {
    isShow: {
      type: Boolean,
      default: false,
      required: true
    },
    flow: Object
  },
  data() {
    return {
      defaultModalSize,
      zIndex: PopupManager.nextZIndex(),
      form: {
        id: "",
        user_id: "",
        forwarded_comment: "",
      },
      config: {
        users: [],
        groups: []
      }
    }
  },
  methods: {
    async getConfig() {
      try {
        const res = await preForward({
          id: this.flow.id
        })
        this.config = res;
      } catch (err) {
        console.error(err)
      }
    },

    async submit() {
      try {
        this.form.id = this.flow.id;
        const res = await forward(this.form)
        this.$message({
          message: res,
          duration: 2000,
          type: 'success'
        })
        setTimeout(() => {
          this.$router.go(-1)
        },2000)
      } catch (err) {
        console.error(err)
      }
    }
  },
  computed: {},
  watch: {
    isShow(newVal) {
      if (newVal) {
        this.zIndex = PopupManager.nextZIndex()

        if(this.flow.id) {
          this.getConfig()
        }
      }
    }
  }
}
</script>

<style scoped lang="scss">
</style>
