/*分配资源*/
<template>
  <div>
    <transition name="marsk-fade">
      <div v-show="deployConfig.isShow" class="marsk" />
    </transition>
    <transition name="slide-fade">
      <div v-show="deployConfig.isShow" class="action-limit">
        <div class="title">
          <p>分配资源</p>
          <div class="operation">
            <i class="el-icon-circle-close" @click="close" />
          </div>
        </div>
        <el-form
          ref="applyForm"
          :model="applyForm"
          :rules="rules"
          label-width="120px"
          class="apply"
          label-position="left"
        >
          <p class="tips">
            <span>{{ deployConfig.userName }}</span
            >申请使用<span>{{ deployConfig.resCount }}</span
            >台<span>{{ getType(deployConfig.resType) }}</span
            >资源<span>{{ deployConfig.resApplyPeriod }}</span
            >小时，用于<span>{{ deployConfig.projectName }}</span
            >项目，请您分配资源!
          </p>
          <el-form-item label="是否分配资源：">
            <el-radio-group
              v-model="applyForm.applyResState"
              @change="getRadio"
            >
              <el-radio label="2">分配</el-radio>
              <el-radio label="3">不分配</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item
            label="不分配说明："
            prop="rejectInfo"
            v-if="applyForm.applyResState == 3"
          >
            <el-input
              type="textarea"
              size="mini"
              :autosize="{ minRows: 3, maxRows: 5 }"
              resize="none"
              v-model="applyForm.rejectInfo"
              placeholder="请填写不分配资源说明"
            ></el-input>
          </el-form-item>
          <el-transfer
            v-if="applyForm.applyResState == 2"
            v-model="resIds"
            :data="deployConfig.resList"
            :titles="['可分配资源', '预分配资源']"
          >
            <span slot-scope="{ option }">
              <el-tooltip placement="top" content="Top center">
                <div slot="content">
                  <p>
                    截止时间：<span>{{
                      formatTime(option.label.split(',')[2])
                    }}</span>
                  </p>
                  <p>
                    规&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;格：<span>{{
                      option.label.split(',')[1]
                    }}</span>
                  </p>
                </div>
                <span>{{ option.label.split(',')[0] }}</span>
              </el-tooltip>
            </span>
          </el-transfer>
        </el-form>

        <div class="btn-group">
          <el-button
            type="danger"
            size="small"
            class="next-btn"
            @click.native.prevent="sure"
            >{{
              applyForm.applyResState == 2 ? '分配资源' : '拒绝分配'
            }}</el-button
          >
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import { formatType, parseTime } from '@/utils/index'
import { request } from '@/utils/request'
export default {
  name: 'Deploy',
  props: {
    deployConfig: {
      isShow: {
        type: Boolean,
        default: () => false,
      },
      resApplyId: {
        type: String,
        default: () => '',
      },
      resList: {
        type: Array,
        default: () => [],
      },
      userName: {
        type: String,
        default: () => '',
      },
      projectName: {
        type: String,
        default: () => '',
      },
      resCount: {
        type: String,
        default: () => '',
      },
      resType: {
        type: String,
        default: () => '',
      },
      resApplyPeriod: {
        type: String,
        default: () => '',
      },
      resUseInfo: {
        type: String,
        default: () => '',
      },
    },
  },
  data() {
    return {
      applyForm: {
        applyResState: '2',
        rejectInfo: '',
      },
      //rules前端验证
      rules: {
        rejectInfo: [
          {
            required: true,
            message: '请填写不同意分配资源说明',
            trigger: 'blur',
          },
        ],
      },
      resIds: [],
    }
  },
  methods: {
    // 点击关闭
    close() {
      this.applyForm = {
        applyResState: '2',
        rejectInfo: '',
      }
      this.resIds = []
      this.$emit('putDeploy', false)
    },
    // 点击确定
    sure() {
      this.$refs.applyForm.validate((valid) => {
        if (valid) {
          if (
            this.applyForm.applyResState == 2 &&
            this.resIds.length != this.deployConfig.resCount
          )
            return this.$message.warning('分配资源数量不满足项目要求！')
          const params = {
            transCode: 'C00030',
            applyResId: this.deployConfig.resApplyId,
            applyResState: this.applyForm.applyResState,
            rejectInfo: this.applyForm.rejectInfo,
            resIds: this.resIds.toString(),
          }
          request(params)
            .then((res) => {
              if (this.applyForm.applyResState == 2) {
                this.$message.success('分配资源成功!')
              } else if (this.applyForm.applyResState == 3) {
                this.$message.success('拒绝分配成功!')
              }
              this.applyForm = {
                applyResState: '2',
                rejectInfo: '',
              }
              this.resIds = []
              this.$emit('putDeploy', this.deployConfig)
            })
            .catch((err) => {})
        } else {
          return false
        }
      })
    },
    // 获取选中项
    getRadio(value) {
      if (value == 2) {
        this.applyForm.rejectInfo = ''
      } else if (value == 3) {
        this.resIds = []
      } else {
        this.resIds = []
        this.applyForm.rejectInfo = ''
      }
    },
    // 获取类型
    getType(type) {
      return formatType(type)
    },
    formatTime(time) {
      return parseTime(time)
    },
  },
}
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';
@import '@/styles/mixin.scss';

.marsk-fade-enter-active,
.marsk-fade-leave-active {
  transition: all 0.5s;
}
.marsk-fade-enter,
.marsk-fade-leave-to {
  opacity: 0;
}
.marsk {
  @include position(fixed, 0, 0, 0, 0, 1000);
  background: rgba(4, 112, 204, 0.05);
}

.slide-fade-enter-active {
  transition: all 0.3s ease;
}
.slide-fade-leave-active {
  transition: all 0.3s cubic-bezier(1, 0.5, 0.8, 1);
}
.slide-fade-enter,
.slide-fade-leave-to {
  transform: translateY(7.48rem);
}
.action-limit {
  @include position(absolute, 50%, auto, 55%, auto, 1001);
  @include w_h(665px);
  padding: 30px 40px;
  transform: translate(-50%, -50%);
  background: $color_f2;
  box-shadow: 0 0 7px 7px rgba(0, 0, 0, 0.04);
  .title {
    @include flex();
    font-size: 18px;
    font-weight: 700;
    color: $color_3;
    .operation {
      font-size: 24px;
      color: $color_9;
      &:hover {
        cursor: pointer;
        color: $color_b;
      }
    }
  }
  .tips {
    font-size: 12px;
    color: $color_9;
    span {
      font-size: 14px;
      color: $color_on;
    }
  }
  .apply {
    @include w_h(100%, 370px);
    margin: 30px auto;
  }
  .btn-group {
    @include w_h();
    margin: 30px auto;
    .next-btn {
      @include w_h();
    }
  }
}
</style>

<style lang="scss">
@import '@/styles/variables.scss';
@import '@/styles/mixin.scss';
@supports (-webkit-mask: none) and (not (cater-color: $color_9)) {
  .el-input input {
    font-size: 12px;
    color: $color_6;
  }
}
.action-limit {
  border-radius: 4px;
  .el-input {
    &__inner {
      padding: 0 5px;
    }
    &__inner:focus {
      border-color: $color_9;
    }
    &--suffix {
      line-height: 28px;
    }
    &--suffix .el-input__inner {
      padding-right: 20px;
    }
  }
  .el-transfer {
    font-size: 12px;
    &-panel {
      border: 1px solid $color_d;
      width: 271px;
      .el-checkbox__label {
        font-size: 12px;
        overflow: unset;
      }
      .el-transfer-panel__header {
        .el-checkbox {
          &__label {
            font-size: 14px;
            color: #303133;
            font-weight: 400;
          }
        }
      }
    }
    &__buttons {
      padding: 0 8px;
    }
    .el-button {
      margin: 5px auto;
      display: block;
      padding: 10px 5px;
    }
  }
}
</style>
