<template>
  <div>
    <vxe-modal :value="isShow"
               show-footer
               :z-index="zIndex"
               title="退回"
               show-zoom
               resize
               transfer
               :fullscreen="$store.getters.device === 'mobile'"
               show-confirm-button
               :width="defaultModalSize.sWidth"
               :height="defaultModalSize.sHeight"
               @input="e => $emit('update:isShow',e)">
      <div>

        <h4>请选择退回步骤：</h4>
        <el-radio-group v-model="form.flow_log_id">
          <el-radio v-for="flowLog in config.flowLogs" :key="flowLog.id" :label="flowLog.id">
            <span style="font-weight: 600;font-size: 15px;">{{ flowLog.node ? flowLog.node.name : "" }}</span>
            <div class="flow-label">
              主办：{{ flowLog.user ? flowLog.user.name : '' }}
            </div>
            <div class="flow-label">
              办结：{{ $moment(flowLog.created_at).format('YYYY年MM月DD日 HH时mm分ss秒') }}
            </div>
          </el-radio>
        </el-radio-group>

        <h4>请输入退回原因：</h4>
        <el-input v-model="form.reason" type="textarea" :autosize="{ minRows: 2 }"></el-input>
      </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 { flowLogs, rollback } 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: "",
        flow_log_id: "",
        reason: "",
      },
      config: {
        flowLogs: [],
      }
    }
  },
  methods: {
    async getConfig() {
      try {
        const res = await flowLogs({
          id: this.flow.id
        })
        this.config = res;
      } catch (err) {
        console.error(err)
      }
    },

    async submit() {
      if(!this.form.flow_log_id) {
        this.$message.warning("请选择退回步骤！")
        return
      }
      try {
        this.form.id = this.flow.id;
        const res = await rollback(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">
.flow-label {
  line-height: 1.5;
}
</style>
