<template>
  <div>
    <vxe-modal :value="isShow"
               show-footer
               :z-index="zIndex"
               title="抄送"
               show-zoom
               show-confirm-button
               transfer
               resize
               :width="defaultModalSize.width"
               :height="defaultModalSize.height"
               @input="e => $emit('update:isShow',e)">
      <div>
        <el-tag effect="dark">{{ flow.custom_model ? flow.custom_model.name : ''}}（流水号：{{ flow.id }}）抄送</el-tag>

        <el-checkbox-group v-model="form.cc_users">
          <div v-for="(ccUser, index) in config.cc_users" :key="ccUser.id">
            <h4>{{ ccUser.name }}</h4>

            <div>
              <el-checkbox v-for="(user, index1) in ccUser.users"
                           :key="user.id"
                           :disabled="(config.flow && config.flow.ccs) ? (!!config.flow.ccs.find(j => j.user_id === user.id)) : false"
                           :label="user.id">{{ user.name }}{{ user.position ? `(${user.position})` : '' }}</el-checkbox>
            </div>
          </div>
        </el-checkbox-group>
      </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 { preShare, share } 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(),
      config: {
        cc_users: []
      },
      form: {
        cc_users: []
      }
    }
  },
  methods: {
    async getConfig() {
      try {
        const res = await preShare(this.flow.id)
        this.config = res;
        this.form.cc_users = res.exists_users;
      } catch (err) {
        console.error(err)
      }
    },

    async submit() {
      try {
        const res = await share(this.flow.id,this.form)
        this.$message({
          message: res,
          duration: 2000,
          type: 'success'
        })
        this.$emit('update:isShow',false)
      } catch (err) {
        console.error(err)
      }
    }
  },
  computed: {},
  watch: {
    isShow(newVal) {
      if (newVal) {
        this.zIndex = PopupManager.nextZIndex()

        if(this.flow.id) {
          this.getConfig()
        }
      } else {
        this.form.cc_users = []
      }
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-checkbox-group {
  font-size: inherit;
}
</style>
