<template>
  <div class="page-container add-chart-page">
    <div class="page-top-title-box">
      <p class="page-top-title-name-p">{{form.id?'编辑':'新增'}}</p>
      <p class="page-top-title-out-p" @click="backPage">返回</p>
      <p class="page-top-title-submit-p" @click="submitForm">保存</p>
    </div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="标题：" prop="imgName">
        <el-input v-model="form.imgName" placeholder="请输入标题" maxLength="20" style="width:400px;"/>
      </el-form-item>
      <el-form-item label="类型：" prop="imgCategory">
        <el-select v-model="form.imgCategory" placeholder="请选择类型" style="width:400px;">
          <el-option label="轮播图" :value="1"></el-option>
          <el-option label="文化图" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="范围：" prop="imgRange">
        <el-select v-model="form.imgRange" @change="imgRangeChange()" placeholder="请选择范围" style="width:400px;">
          <el-option
            v-for="dict in deptOptions"
            :key="dict.deptId"
            :label="dict.deptName"
            :value="dict.deptId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序：" prop="imgOrder">
        <el-input v-model="form.imgOrder" placeholder="请输入排序" maxLength="5" style="width:400px;"/>
      </el-form-item>
      <el-form-item label="有效时间：" prop="validBeginTime">
        <el-date-picker
          :clearable="false"
          v-model="dateRange"
          size="small"
          style="width: 400px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="内容：" prop="imgUrl">
        <el-upload
          class="certificate-avatar-uploader"
          :action="uploadImgUrl"
          :show-file-list="false"
          accept="image/jpeg,image/gif,image/png"
          :on-success="handleAvatarSuccess"
          :headers="headers"
          :before-upload="beforeAvatarUpload">
          <p v-if="!form.imgUrl" style="background:#0183FA;color:#fff;font-weight:500;font-size:14px;text-align: center;line-height:40px;width:200px;border-radius:4px; "> + 上传图片</p>
          <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar" style="height:80px;width:80px;">
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { getToken } from "@/utils/auth";
  import { systemRotationChartAdd, systemRotationChartEdit } from '@/api/systemManagement/index'
  import { getDeptDropList } from '@/api/commonality/permission'
  export default {
    name: "addPage",
    props:{
      propsData:{},
    },
    data(){
      return{
        uploadImgUrl: this.uploadUrl(), // 上传的图片服务器地址
        headers: {
          Authorization:getToken(),
        },
        form:{

        },
        rules:{
          imgName:[
            {required: true, message: '请输入轮播图标题', trigger: 'blur'},
            { required: true, message: "请输入轮播图标题", validator: this.spaceJudgment, trigger: "blur" }
          ],
          imgCategory:[
            {required: true, message: '请选择轮播图类型', trigger: 'blur'}
          ],
          imgRange:[
            {required: true, message: '请选择轮播图展示范围', trigger: 'blur'}
          ],
          imgOrder:[
            {required: true, message: '请输入轮播图排序序号', trigger: 'blur'},
            { required: true, message: "请输入轮播图排序序号", validator: this.spaceJudgment, trigger: "blur" },
            { required: true, message: "只能输入数字", validator: this.isNum, trigger: "blur" },
          ],
          imgUrl:[
            {required: true, message: '请选择轮播图内容', trigger: 'blur'}
          ],
        },
        dateRange:[],
        //范围
        deptOptions:[],
      }
    },
    created() {

    },
    mounted(){
      this.getDeptList();
      if(this.propsData.id){
        let obj = {
          id : this.propsData.id,
          imgName : this.propsData.imgName,
          imgCategory : this.propsData.imgCategory,
          imgRange : this.propsData.imgRange,
          imgOrder : this.propsData.imgOrder,
          validBeginTime : this.propsData.validBeginTime+'',
          validEndTime : this.propsData.validEndTime+'',
          imgUrl : this.propsData.imgUrl,
        }
        this.dateRange=[this.propsData.validBeginTime,this.propsData.validEndTime]
        this.$set(this,'form',obj);
      }
    },
    methods: {
      /** 查询学院列表 */
      imgRangeChange(){
        let self=this;
        this.deptOptions.forEach(function(item) {
          if (self.form.imgRange==item.deptId){
            self.$set(self.form, 'imgRangeName', item.deptName)
          }
        })
      },
      //查询学院列表
      getDeptList(){
        getDeptDropList({deptName:"",level:2,deptType:1}).then(response => {
          this.$set(this, 'deptOptions', response.data)
        });
      },
      // 返回按钮
      backPage(){
        this.$parent.tableButton('out');
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let obj = JSON.parse(JSON.stringify(this.form))
            if(this.dateRange[0]){
              obj.validBeginTime = this.dateRange[0]+'T00:00:00'
              obj.validEndTime = this.dateRange[1]+'T23:59:59'
            }else{
              obj.validBeginTime = "";
              obj.validEndTime = "";
            }
            if (this.form.id != null) {
              systemRotationChartEdit(obj).then( response => {
                this.msgSuccess("修改成功");
                this.$parent.buttonClick(1);
              });
            } else {
              systemRotationChartAdd(obj).then( response => {
                this.msgSuccess("新增成功");
                this.$parent.buttonClick(1);
              });
            }
          }
        });
      },
      //上传
      handleAvatarSuccess(res, file) {
        this.$set(this.form,'imgUrl',res.data.url);
        this.$forceUpdate()
      },
      beforeAvatarUpload(file) {
        let type = false;
        console.log('file',file);
        if (file.type == 'image/png' || file.type == 'image/jpeg' || file.type == 'image/gif') {
          type = true;
        }else{
          this.$message.error('只能上传png/jpeg/gif格式图片');
          type = false;
        }
        return type;
      },
    },
  }
</script>

<style scoped lang="scss">
  .add-chart-page{
    flex:1;
    display: flex!important;
    flex-direction: column;
    *{
      margin:0;
    }
    .top-title-box{
      display: flex;
      border-bottom:1px solid #dedede;
      p:nth-child(1){
        flex:1;
        margin-left:20px;
        color:#0045af;
        line-height:80px;
        font-size:18px;
      }
      p:nth-child(2){
        margin:20px;
      }
    }
    .bottom-button-p{
      display: block;
      margin:40px auto;
    }
    .el-form-item{
      margin-top:30px;
    }
  }
</style>
