<template>
  <div class="modal-backdrop">
    <div class="modal">
      <div class="modal-header">
        <span>编辑</span>
      </div>
      <div class="modal-body">
        <div class="bodyRow">
          <span>资料名称</span>
          <select v-model="ziliaoId">
            <option
              v-for="(item, index) in dataName"
              :key="index"
              :value="item.id"
            >
              {{ item.informationName }}
            </option>
          </select>
        </div>
        <div class="bodyRow timeWidth">
          <span>发布时间</span
          ><input type="date" placeholder="选择时间" v-model="ziliaoTime" />
        </div>
        <div class="bodyRow">
          <div class="contentCol1">
            <span>车系</span
            ><select v-model="carS" @change="chooseCarSeries">
              <option
                v-for="(item, index) in carSeries"
                :key="index"
                :value="item.typeName"
              >
                {{ item.typeName }}
              </option>
            </select>
          </div>
          <div class="contentCol2">
            <span>车型</span
            ><select v-model="carT">
              <option
                v-for="(item, index) in carType"
                :key="index"
                :value="item.typeName"
              >
                {{ item.typeName }}
              </option>
            </select>
          </div>
        </div>
        <div class="bodyRow">
          <div class="contentCol1">
            <span>发布平台</span>
            <div>{{ selectedPlatform }}</div>
          </div>
          <div class="contentCol2">
            <span>发布板块</span
            ><select name="" id="" v-model="releaseP">
              <option
                v-for="(item, index) in releasePlate"
                :key="index"
                :value="item.platformName"
              >
                {{ item.platformName }}
              </option>
            </select>
          </div>
        </div>
        <div class="bodyRow">
          <span>项目分类</span
          ><select name="" id="">
            <option value="">请选择</option>
          </select>
        </div>
        <div class="bodyRow">
          <div class="contentCol1">
            <span>内容分类1</span
            ><select name="" id="" v-model="contentName1">
              <option
                v-for="(item, index) in content1"
                :key="index"
                :value="item.dictName"
              >
                {{ item.dictName }}
              </option>
            </select>
          </div>
          <div class="contentCol2">
            <span>内容分类2</span
            ><select name="" id="" v-model="contentName2">
              <option
                v-for="(item, index) in content2"
                :key="index"
                :value="item.dictName"
              >
                {{ item.dictName }}
              </option>
            </select>
          </div>
        </div>
        <div class="bodyRow">
          <span>标题</span>
          <input type="text" placeholder="文章标题" v-model="mediaTitle" />
        </div>
        <div class="bodyRow">
          <span>链接</span>
          <input type="text" placeholder="文章发布地址" v-model="mediaUrl" />
        </div>
      </div>
      <div class="modal-footer">
        <!-- <button type="button" class="btn-confirm" @click="confirm">确认</button> -->
        <button type="button" class="btn-confirm" @click="closeSelf(), edit()">
          确认
        </button>
        <button type="button" class="btn-close" @click="closeSelf">取消</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Modal",
  props: {
    LinkData: {
      type: Object,
      default: () => {
        return {};
      },
    },
    selectedPlatform: {
      type: String,
      default: () => {
        return "";
      },
    },
    releasePlate: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      dataName: [],
      carSeries: [],
      carType: [],
      content1: [],
      content2: [],

      carS: "",
      carT: "",
      releaseP: "",
      ziliaoId: "",
      content1Id: "",
      content2Id: "",

      carTypeId: "",
      contentTypeName: "",
      informationId: "",
      mediaTitle: "",
      mediaUrl: "",
      projectTypeName: "",
      publishDate: "",
      publishSourceName: "",

      ziliaoName: "",
      ziliaoTime: "",
      contentName1: "",
      contentName2: "",
    };
  },
  methods: {
    closeSelf() {
      this.$emit("closeme");
      console.log(this.LinkData);
    },

    edit: function () {
      let obj = {};
      //获取资料名
      let idVal = this.ziliaoId;
      obj = this.dataName.find(function (item) {
        return item.id === idVal;
      });
      this.ziliaoName = obj.informationName;
      //获取车系 id
      let nameCarS = this.carS;
      obj = this.carSeries.find(function (item) {
        return item.typeName === nameCarS;
      });
      console.log("chexi ", obj);
      //carTypeId  表示车系 id
      this.carTypeId = obj.id;
      //获取车型名 this.carT
      //获取平台名 this.selectedPlatform,

      //当前月份
      let date = new Date();
      let m = date.getMonth() + 1;
      let y = date.getFullYear();
      let d = y + "/" + m;
      let config = {
        url: "/dealerFeedback",
        data: {
          month: d,
          dealerId: "254",
          dataId: this.ziliaoId,
          informationName: this.ziliaoName,
          releaseTime: this.ziliaoTime,
          carService: this.carS,
          carType: this.carT,
          releasePlate: this.selectedPlatform,
          releaseMedia: this.releaseP,
          draftGenre: this.contentName1,
          contentClass: this.contentName1,
          articleTitle: this.mediaTitle,
          articleLink: this.mediaUrl,
        },
      };
      //  console.log("edit2:", config);
      this.$emit("update", config);
    },
    //资料名称列表
    chooseDataName: function () {
      this.$http({
        url: "/firmsUploadListByAgent",
        //url: "/firmsLinkUpload",
        method: "post",
        data: {},
      })
        .then((res) => {
          if (res.data.code === 200) {
            this.dataName = res.data.data;
            console.log(this.dataName);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //发布版块
    /*  chooseReleasePlate: function () {}, */

    //不分页查询车系车型信息
    chooseCarSeries: function () {
      this.$http({
        url: "/base/carTypeManager/selectCarTypeList",
        method: "post",
        data: {},
      })
        .then((res) => {
          if (res.data.code === 200) {
            this.carSeries = res.data.data;
            this.chooseCarType();
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    chooseCarType: function () {
      //获取车系 id
      let obj = {};
      let nameCarS = this.carS;
      obj = this.carSeries.find(function (item) {
        return item.typeName === nameCarS;
      });
      this.carTypeId = obj.id;

      this.$http({
        url: "/base/carTypeManager/selectCarTypeList",
        method: "post",
        data: {
          parentId: this.carTypeId || -1,
        },
      })
        .then((res) => {
          if (res.data.code === 200) {
            this.carType = res.data.data;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //不分页查询发布平台信息 内容分类1
    //初次调用获得 content ID 用作 parentID
    firstContent1Classify: function () {
      this.$http({
        url: "/sys/dataDict/selectSysDataDictList",
        method: "post",
        data: {
          dictCode: "content1",
        },
      })
        .then((res) => {
          if (res.data.code === 200) {
            this.content1Id = res.data.data[0].id;
            this.secondContent1Classify();
          }
          console.log("contentClassify1", res.data.data[0].id);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    secondContent1Classify: function () {
      this.$http({
        url: "/sys/dataDict/selectSysDataDictList",
        method: "post",
        data: {
          parentId: this.content1Id,
        },
      })
        .then((res) => {
          if (res.data.code === 200) {
            this.content1 = res.data.data;
          }
          console.log("second contentClassify1", res.data.data);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //不分页查询发布平台信息 内容分类2
    firstContent2Classify: function () {
      this.$http({
        url: "/sys/dataDict/selectSysDataDictList",
        method: "post",
        data: {
          dictCode: "content2",
        },
      })
        .then((res) => {
          if (res.data.code === 200) {
            this.content2Id = res.data.data[0].id;
            this.secondContent2Classify();
          }
          console.log("contentClassify2", res.data.data[0].id);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    secondContent2Classify: function () {
      this.$http({
        url: "/sys/dataDict/selectSysDataDictList",
        method: "post",
        data: {
          parentId: this.content2Id,
        },
      })
        .then((res) => {
          if (res.data.code === 200) {
            this.content2 = res.data.data;
          }
          console.log("second contentClassify2", res.data.data);
        })
        .catch((err) => {
          console.log(err);
        });
    },

    initialData: function () {
      this.ziliaoId = this.LinkData.informationId;

      this.mediaTitle = this.LinkData.mediaTitle;
      this.mediaUrl = this.LinkData.mediaUrl;
      this.carS = this.LinkData.carTypeName;

      this.carT = this.LinkData.carPlatformName;
      let time = this.LinkData.publishDate;
      if (time.length > 4) {
        let y = time.slice(0, 4);
        let m = time.slice(5, 7);
        let d = time.slice(8, 10);
        let temp = y + "-" + m + "-" + d;
        this.ziliaoTime = temp;
      }
      //console.log("console.log(this.forceUpdate);", typeof time, m, y);

      //缺少对应列表值 已有资料名在数据中无法查询到
      this.releaseP = this.LinkData.publishSourceName;

      this.contentName1 = this.LinkData.contentTypeName;
      this.contentName2 = this.LinkData.contentTypeName;
    },
    /*  forceUpdata: function () { 
      this.$forceUpdate();
      console.log("console.log(this.forceUpdate);");
    }, */
  },
  mounted() {
    this.chooseDataName();
    this.chooseCarSeries();
    this.firstContent1Classify();
    this.firstContent2Classify();
    //this.chooseReleasePlate();

    this.initialData();
  },
  /*   created() {
    console.log(this.LinkData);
    this.ziliaoId = this.LinkData.id;
    console.log(this.ziliaoId);
  }, */
};
</script>

<style>
.modal-backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: #7d9191;
  display: flex;
  justify-content: center;
  align-items: center;
}
.modal {
  background-color: #eeeeee;
  box-shadow: 2px 2px 20px 1px;
  overflow-x: auto;
  display: flex;
  flex-direction: column;
  border-radius: 0px;
  width: 1030px;
}
.modal-header {
  border-bottom: 1px solid #eee;
  color: #313131;
  justify-content: space-between;
  padding: 15px;
  display: flex;
  border-bottom: 1px solid #e3e3e3;
}
.modal-header span {
  font-size: 16px;
  font-weight: bold;
}
.modal-footer {
  border-top: 1px solid #eee;
  justify-content: center;
  padding: 15px;
  display: flex;
}
.modal-footer button {
  width: 90px;
  margin-left: 0px;
}
.modal-footer button:nth-child(1) {
  margin-right: 80px;
}
.modal-body {
  position: relative;
  padding: 20px 120px 20px 180px;
}
.modal-body span,
select,
input,
div {
  font-size: 14px;
}

.btn-close,
.btn-confirm {
  border-radius: 8px;
  margin-left: 16px;
  width: 56px;
  height: 36px;
  border: none;
  cursor: pointer;
}
.btn-close {
  color: #313131;
  background-color: gray;
}
.btn-confirm {
  color: #fff;
  background-color: #0056a0;
}
.bodyRow {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 4px;
}
.bodyRow span {
  width: 80px;
  margin: 10px;
}
.bodyRow select {
  width: 120px;
}
.bodyRow input {
  width: 480px;
}
.bodyRow select,
input {
  padding: 2px;
}
.contentCol1 {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.contentCol1 span {
  width: 80px;
  margin: 10px;
}
.contentCol1 div {
  width: 120px;
}
.contentCol2 {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  width: 363px;
}
.timeWidth input {
  width: 150px;
}
</style>

