<template>
  <!--新增弹框-->
  <el-dialog
    custom-class="party-dialog"
    title="新建视频分类"
    width="468px"
    :visible.sync="dialogVisible"
    :before-close="close"
    
  >
    <div class="dialog-content">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="140px"
        class="party-form form-inline"
        label-position="top"
      >
        <el-form-item label="版权方名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="版权方有效期" required>
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="value1"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item class="selectH100" label="请选择预设视频分类">
          <el-select
            placeholder="请选择预设视频分类"
            @focus="getAssetTypeData"
            multiple
            v-model="ruleForm.assetTypeIdList"
            @change="getSelectDep"
          >
            <el-checkbox :style="selfstyle" v-model="checkedThing" @change="selectAllThing">全选</el-checkbox>
            <el-option v-for="item in videoContentCat" :label="item.name" :value="item.id" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="ruleForm.remarks"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer btn-group">
      <el-button size="mini" type="primary" @click="save('classForm')">确定</el-button>
      <el-button size="mini" @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible:true,
      ruleForm: {
        assetTypeIdList: [],
        name: "",
        ownerType: "VIDEO_CONTENT"
      },
            ruleForm: {
        assetTypeIdList: [],
        name: "",
        ownerType: "VIDEO_CONTENT"
      },
      videoContentCat: [
        {
          value: "Beijing",
          label: "北京"
        },
        {
          value: "Shanghai",
          label: "上海"
        }
      ],

      checkedThing: false,
      selfstyle: {
        textAlign: "right",
        width: "100%",
        paddingRight: "10px"
      },
      rules: {
        name: [
          { required: true, message: "请输入版权方名称", trigger: "blur" }
          // { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        date1: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change"
          }
        ],
        date2: [
          {
            type: "date",
            required: true,
            message: "请选择时间",
            trigger: "change"
          }
        ],
        type: [
          {
            type: "array",
            required: true,
            message: "请至少选择一个活动性质",
            trigger: "change"
          }
        ],
        resource: [
          { required: true, message: "请选择活动资源", trigger: "change" }
        ],
        desc: [{ required: true, message: "请填写活动形式", trigger: "blur" }]
      }
    };
  },
  methods: {
    // 弹窗保存
    save(formName) {
      this.$refs[formName].validate(valid => {
        this.ruleForm.expireDateEnd = this.value1[1];
        this.ruleForm.expireDateStart = this.value1[0];
        if (valid) {
          this.$https(
            {
              url: "videoContentCat/save",
              method: "post",
              authType: this.backToken
            },
            this.classForm
          )
            .then(res => {
              this.$message({ type: "success", message: "新增分类成功!" });
              this.dialogVisible = false;
            })
            .catch(function(err) {
              this.$message({
                type: "fail",
                message: "新增失败!" + err.response.data.msg
              });
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 新增关闭
    close() {
      this.dialogVisible = false;
      for (let key in this.classForm) {
        this.classForm[key] = null;
      }
      this.$refs["classForm"].resetFields();
    },
     // 获取视频分类列表
    getAssetTypeData() {
      let vm = this;
      vm.$https({
        url: "videoContentCat/getList",
        method: "get",
        authType: this.backToken
      })
        .then(res => {
          let data = res.data.data;
          this.videoContentCat = data;
          // this.value1[0]=data.expireDateEnd
          //this.value1[1]=data.expireDateStart
        })
        .catch(function(err) {
          console.log(err);
        });
    },
    getSelectDep(assetTypeIdList) {
      if (assetTypeIdList.length === this.videoContentCat.length) {
        this.checkedThing = true;
      } else {
        this.checkedThing = false;
      }
    },
    selectAllThing() {
      // debugger
      this.ruleForm.assetTypeIdList = [];
      if (this.checkedThing) {
        this.videoContentCat.map(item => {
          this.ruleForm.assetTypeIdList.push(item.id);
        });
      } else {
        this.ruleForm.assetTypeIdList = [];
      }
    }
  }
};
</script>

<style>
</style>