/*申请资源*/
<template>
  <div>
    <transition name="marsk-fade">
      <div v-show="applyConfig.isShow" class="marsk" />
    </transition>
    <transition name="slide-fade">
      <div v-show="applyConfig.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"
        >
          <el-form-item label="选择资源类型" prop="resType">
            <el-select
              v-model="applyForm.resType"
              size="mini"
              placeholder="请选择"
              @change="getDeploy"
            >
              <el-option
                v-for="item in typeList"
                :key="item.value"
                :value="item.value"
                :label="item.label"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="申请资源数量"
            prop="resCount"
            v-if="applyConfig.userType == 2"
          >
            <el-input
              size="mini"
              type="number"
              v-model="applyForm.resCount"
              placeholder="请填写资源数量"
              ><span slot="suffix" class="solt">台</span></el-input
            >
          </el-form-item>
          <el-form-item label="资源使用时长" prop="resUsePeriod">
            <el-input
              size="mini"
              type="number"
              v-model="applyForm.resUsePeriod"
              placeholder="请填写资源时长"
              ><span slot="suffix" class="solt">小时</span></el-input
            >
          </el-form-item>
          <el-form-item label="资源使用说明" v-if="applyConfig.userType == 2">
            <el-input
              type="textarea"
              size="mini"
              :autosize="{ minRows: 3, maxRows: 5 }"
              resize="none"
              v-model="applyForm.resUseInfo"
              placeholder="请填写资源使用说明"
            ></el-input>
          </el-form-item>
          <el-transfer
            v-if="applyConfig.userType != 2"
            v-model="resIds"
            :data="applyConfig.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"
            >提交申请</el-button
          >
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import { request } from '@/utils/request'
import { parseTime } from '@/utils/index'
export default {
  name: 'ApplyResource',
  props: {
    applyConfig: {
      isShow: {
        type: Boolean,
        default: () => false,
      },
      projectId: {
        type: String,
        default: () => '',
      },
      userType: {
        type: String,
        default: () => '',
      },
      resList: {
        type: Array,
        default: () => [],
      },
    },
  },
  data() {
    const validateCount = (rule, value, callback) => {
      if (value) {
        if (
          !/(^[1-9]\d*$)/.test(value) ||
          value.toString().indexOf('.') != -1
        ) {
          callback(new Error('资源数量只能是正整数!'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请输入资源数量！'))
      }
    }
    const validateTime = (rule, value, callback) => {
      if (value) {
        if (
          !/(^[1-9]\d*$)/.test(value) ||
          value.toString().indexOf('.') != -1
        ) {
          callback(new Error('资源时长只能是正整数!'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请输入资源时长！'))
      }
    }
    return {
      applyForm: {
        resType: '0',
        resCount: '1',
        resUsePeriod: '24',
        resUseInfo: '',
      },
      //rules前端验证
      rules: {
        resType: [
          { required: true, message: '请选择资源类型', trigger: 'change' },
        ],
        resCount: [
          { required: true, trigger: 'blur', validator: validateCount },
        ],
        resUsePeriod: [
          { required: true, trigger: 'blur', validator: validateTime },
        ],
        productDesc: [
          { required: true, message: '请输入作品说明', trigger: 'blur' },
        ],
      },
      typeList: [
        {
          value: '0',
          label: '通用',
        },
      ],
      resIds: [],
    }
  },
  methods: {
    // 点击关闭
    close() {
      this.applyForm = {
        resType: '0',
        resCount: '1',
        resUsePeriod: '24',
        resUseInfo: '',
      }
      this.resIds = []
      this.$emit('putApply', false)
    },
    //获取可分配资源
    getDeploy(type) {
      // 获取资源列表
      const params = {
        transCode: 'C00024',
        state: '1', // 资源状态(1-可分配 2-已分配 3-已到期 4-已删除）
        type: type, // 资源类型(0-通用）
        name: '', // 资源名称
        page: 1,
        count: -1,
      }
      request(params)
        .then((res) => {
          let resList = []
          res.rows.map((item, index) => {
            resList.push({
              key: item.resId,
              label: `${item.name},${item.capacity},${item.endtime}`,
            })
          })

          this.applyConfig = {
            isShow: true,
            projectId: this.projectId,
            userType: this.userType,
            resList: resList,
          }
        })
        .catch((err) => {})
    },
    // 点击确定
    sure() {
      this.$refs.applyForm.validate((valid) => {
        if (valid) {
          const params = {
            transCode: 'C00025',
            projectId: this.applyConfig.projectId,
            resType: this.applyForm.resType,
            resCount:
              this.applyConfig.userType != 2
                ? this.resIds.length.toString()
                : this.applyForm.resCount,
            resUsePeriod: this.applyForm.resUsePeriod,
            resUseInfo: this.applyForm.resUseInfo,
          }
          if (this.applyConfig.userType != 2) {
            params.resIds = this.resIds.toString()
          }
          request(params)
            .then((res) => {
              this.$message.success(
                `已申请使用 ${
                  this.applyConfig.userType != 2
                    ? this.resIds.length
                    : this.applyForm.resCount
                } 台资源 ${this.applyForm.resUsePeriod} 小时，请等待审核!`
              )
              this.applyForm = {
                resType: '0',
                resCount: '1',
                resUsePeriod: '24',
                resUseInfo: '',
              }
              this.resIds = []
              this.$emit('putApply', this.applyConfig)
            })
            .catch((err) => {})
        } else {
          return false
        }
      })
    },
    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_f;
  border-radius: 4px;
  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;
      }
    }
  }
  .num-list {
    @include w_h();
    @include flex(flex-start);
    flex-wrap: wrap;
    margin: 30px auto 10px;
  }
  .desc {
    @include flex();
    font-size: 12px;
    color: $color_6;
  }
  .apply {
    @include w_h(100%, 410px);
    margin: 30px auto;
    .el-select {
      @include w_h();
    }
  }
  .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 {
  .el-input {
    &__inner {
      padding: 0 15px;
    }
    &__inner:focus {
      border-color: $color_9;
    }
    &--suffix {
      line-height: 28px;
    }
    &--suffix .el-input__inner {
      padding-right: 30px;
    }
  }

  .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>
