<template>
  <div class="info">
    <div class="info-header">新建版权方</div>
    <div class="info-container">
      <div class="info-wrapper">
          <div class="pageTips">
              <strong>页面说明：</strong>
              可新建版权方信息。“*”为必填项。预设视频分类中没有合适的选项，可点击“新建视频分类”在弹框中新建分类
           </div>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="140px"
          class="party-form form-inline"
        >
          <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="addTip">
            <span>若没有相应的视频分类，请点击</span>
            <el-button type="text" @click="addVideoClass">新建视频分类</el-button>
          </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>
        <!--新增弹框-->
        <el-dialog
          custom-class="party-dialog"
          title="新建视频分类"
          width="468px"
          :visible.sync="dialogVisible"
          :before-close="close"
        >
          <div class="dialog-content">
            <el-form
              :model="classForm"
              ref="classForm"
              label-width="80px"
              label-position="top"
              :rules="rules"
              id="ruleo"
              class="party-form"
            >
              <el-form-item label="视频分类名称" prop="name">
                <el-input v-model="classForm.name"></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input type="textarea" v-model="classForm.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>
      </div>
    </div>
    <div class="info-footer">
      <div class="btn-group">
        <div class="btn-group">
          <el-button
            type="primary"
            v-show="type === 'add'"
            class="btn_form_search"
            @click="submitForm('ruleForm')"
          >确定</el-button>
          <el-button
            type="primary"
            v-show="type === 'Update'"
            class="btn_form_search"
            @click="updateForm('ruleForm')"
          >确定</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      type: this.$route.query.type,
      dialogVisible: false,
      classForm: {},
      formLabelWidth: "100px",
      form: {
        name: ""
      },
      value1: [],
      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" }]
      }
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      if (this.$route.query.type === "Update") {
        this.getInfo();
        this.getAssetTypeData()
      }
    },
    //  获取版权方详情
    getInfo() {
      let vm = this;
      vm.$https({
        url: "copyrightOwner/get/" + this.$route.query.id,
        method: "get",
        authType: this.backToken
      })
        .then(res => {
          let data = res.data.data;
          // this.videoContentCat = data
          this.ruleForm = data;
          this.value1 = [data.expireDateStart, data.expireDateEnd];

          // this.ruleForm.assetTypeIdList[0] = data.expireDateStart
          // this.ruleForm.assetTypeIdList[1] = data.expireDateEnd
        })
        .catch(function(err) {
          console.log(err);
        });
    },

    // 新建视频分类
    addVideoClass() {
      this.dialogVisible = true;
    },
    // 弹窗保存
    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",
              // headers: {
              //   'Content-Type': 'application/json'
              // },
              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();
    },
    // 新增
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        this.ruleForm.expireDateEnd = this.value1[1];
        this.ruleForm.expireDateStart = this.value1[0];
        if (valid) {
          this.$https(
            {
              url: "copyrightOwner/save",
              method: "post",
              authType: this.backToken
            },
            // this.ruleForm
            this.$qs.stringify(this.ruleForm)
          )
            .then(res => {
              this.$message({ type: "success", message: "新增成功!" });
              history.go(-1);
            })
            .catch(function(err) {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 修改版权方
    updateForm(formName) {
      this.$refs[formName].validate(valid => {
        this.ruleForm.id = this.$route.query.id;
        this.ruleForm.expireDateEnd = this.value1[1];
        this.ruleForm.expireDateStart = this.value1[0];

        if (valid) {
          this.$https(
            {
              url: "copyrightOwner/update",
              method: "put",
              authType: this.backToken
            },
            this.$qs.stringify(this.ruleForm)
            // this.ruleForm
          )
            .then(res => {
              this.$message({ type: "success", message: "修改成功!" });
              history.go(-1);
            })
            .catch(function(err) {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      history.go(-1);
    },
    // 获取视频分类列表
    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 lang="less">
@import "../../../../style/dialog";
</style>