<template>
  <div class="info">
    <div class="info-header">{{ type === 'Update'?'修改':'新建'}}展板版权方</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="demo-form-inline party-form"
          :inline="true"
          label-position="top"
        >
          <el-form-item label="版权方名称" prop="name" class="w50">
            <el-input oninput="value = value.trim()" v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="版权方有效期" prop="value1">
            <el-date-picker
              value-format="yyyy-MM-dd"
              v-model="ruleForm.value1"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
         
          <el-form-item class="w50" label="请选择预设展板分类" prop="boardCatIdList">
            <el-select
              placeholder="请选择预设展板分类"
              @focus="getAssetTypeData"
              multiple
              v-model="ruleForm.boardCatIdList"
              @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 >
             <br>
            <span style="color:red">若没有相应的展板分类，请点击</span>
            <el-button type="text" @click="addBoardClass">新建展板分类</el-button>
          </el-form-item>
          <el-form-item label="备注" class="w100" prop="remarks">
            <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="rule"
              id="ruleo"
              class="party-form"
            >
              <el-form-item label="展板分类名称" prop="name">
                <el-input oninput="value = value.trim()" v-model="classForm.name"></el-input>
              </el-form-item>
              <el-form-item label="备注" prop="remarks">
                <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: {
        value1:[],
        boardCatIdList: [],
        name: "",
        ownerType: "EXHIBITION_BOARD"
      },
      videoContentCat: [
        {
          value: "Beijing",
          label: "北京"
        },
        {
          value: "Shanghai",
          label: "上海"
        }
      ],

      checkedThing: false,
      selfstyle: {
        textAlign: "right",
        width: "100%",
        paddingRight: "10px"
      },
      rule:{
        name: [
          { required: true, message: "请输入展板分类名称", trigger: "blur" },
          { min: 1, max: 20, message: "请输入1到20个字" },
        ],
         remarks:[
          { min: 1, max: 100, message: "请输入1到100个字" },
        ]
      },
      rules: {
        name: [
          { required: true, message: "请输入版权方名称", trigger: "blur" },
          { min: 1, max: 20, message: "请输入1到20个字" },
        ],
        boardCatIdList:[
          {required: true, message: "请选择展板分类", trigger: "change" }
        ],
        value1:[
          { required: true, message: "请填写版权方有效期", trigger: "change" }
        ],
         remarks:[
          { min: 1, max: 100, message: "请输入1到100个字" },
        ]
      }
    };
  },
  watch:{
    // 'ruleForm.value1'(newVal ,oldVal){
    //   // console.log(newVal,oldVal)
    //   this.ruleForm.expireDateEnd = this.ruleForm.value1[1];
    //   this.ruleForm.expireDateStart = this.ruleForm.value1[0];

    // }
  },
  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.ruleForm = data;

          let arr = []
          arr = [data.expireDateStart, data.expireDateEnd];
          this.ruleForm.value1 = arr
          this.ruleForm = {...this.ruleForm}

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

    // 新建视频分类
    addBoardClass() {
      this.dialogVisible = true;
    },
    // 新建视频分类弹窗保存
    save(formName) {
      this.$refs[formName].validate(valid => {
        this.classForm.expireDateEnd = this.value1[1];
        this.classForm.expireDateStart = this.value1[0];
        if (valid) {
          this.$https(
            {
              url: "exhibitionBoardCat/save",
              method: "post",
              authType: this.backToken
            },
            this.$qs.stringify(this.classForm)
            // this.classForm
          )
            .then(res => {
              console.log(res)
              if(res.data.resultCode === "200"){
                this.$message({ type: "success", message: "新增分类成功!" });
              this.dialogVisible = false;
              }else{
                this.$message({ type: "error", message: res.data.message });
              }
             
            })
            .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.ruleForm.value1[1];
        this.ruleForm.expireDateStart = this.ruleForm.value1[0];
        if (valid) {
          this.$https(
            {
              url: "copyrightOwner/save",
              method: "post",
              authType: this.backToken
            },
            // this.ruleForm
            this.$qs.stringify(this.ruleForm)
          )
            .then(res => {
              if(res.data.resultCode === "200"){
                this.$message({ type: "success", message: "新增展板版权方成功！" });
                history.go(-1);
              }else{
                this.$message({ type: "error", message: res.data.message });
              }
              
              
            })
            .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.ruleForm.value1[1];
        this.ruleForm.expireDateStart = this.ruleForm.value1[0];

        if (valid) {
          this.$https(
            {
              url: "copyrightOwner/update",
              method: "put",
              authType: this.backToken
            },
            this.$qs.stringify(this.ruleForm)
            // this.ruleForm
          )
            .then(res => {
               if(res.data.resultCode === "200"){
                this.$message({ type: "success", message: "修改展板版权方成功！" });
                history.go(-1);
              }else{
                this.$message({ type: "error", message: res.data.message });
              }
            })
            .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: "exhibitionBoardCat/getList",
        method: "post",
        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(boardCatIdList) {
      if (boardCatIdList.length === this.videoContentCat.length) {
        this.checkedThing = true;
      } else {
        this.checkedThing = false;
      }
    },
    selectAllThing() {
      // debugger
      this.ruleForm.boardCatIdList = [];
      if (this.checkedThing) {
        this.videoContentCat.map(item => {
          this.ruleForm.boardCatIdList.push(item.id);
        });
      } else {
        this.ruleForm.boardCatIdList = [];
      }
    }
  }
};
</script>

<style lang="less">
@import "../../../../style/dialog";
</style>