<template>
  <div class="app-container">
    <div class="page-nav">
      <el-form :inline="true">
        <!-- <el-form-item label="查询日期">
          <el-date-picker
            v-model="queryDate"
            :picker-options="pickerOptions"
            type="daterange"
            align="center"
            unlink-panels
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item> -->

        <el-form-item label="请选择产品">
          <el-select v-model="condition.pkg" clearable placeholder="请选择产品">
            <el-option
              v-for="item of pkgOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="请选择渠道">
          <el-select
            v-model="condition.channelId"
            clearable
            placeholder="请选择渠道"
          >
            <el-option
              v-for="item of agentList"
              :key="item.id"
              :label="item.agentName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="投放状态">
          <el-select
            v-model="condition.putinStatus"
            clearable
            placeholder="请选择投放状态"
          >
            <el-option
              v-for="item of stateList"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="statusManage()">查询</el-button>
        </el-form-item>
      </el-form>

      <el-button type="success" style="margin-bottom: 20px" @click="addAgent()"
        >添加</el-button
      >
    </div>
    <el-table
      :data="putList"
      v-loading="listLoading"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="产品" prop="appName" align="center" />
      <el-table-column label="渠道" prop="agentName" align="center" />
      <el-table-column
        label="媒体平台"
        align="center"
        :formatter="platformIdFormat"
      />
      <el-table-column
        label="投放状态"
        align="center"
        :formatter="stateFormat"
      />
      <el-table-column label="起投时间" prop="startPutinTime" align="center" />
      <el-table-column label="备注" prop="remarks" align="center" />
      <el-table-column fixed="right" label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button type="primary" @click="clickEditor(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" @click="clickDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="is_clickEditor">
      <el-form>
        <el-form-item label="产品" :label-width="formLabelWidth">
          <el-select v-model="item.pkg" placeholder="请选择产品">
            <el-option
              v-for="item of pkgOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="渠道" :label-width="formLabelWidth">
          <el-select v-model="item.channelId" placeholder="请选择渠道">
            <el-option
              v-for="item of agentList"
              :key="item.id"
              :label="item.agentName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="媒体平台" :label-width="formLabelWidth">
          <el-select v-model="item.platformId" placeholder="请选择媒体平台">
            <el-option
              v-for="item of platformList"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="投放状态" :label-width="formLabelWidth">
          <el-select v-model="item.putinStatus" placeholder="请选择投放状态">
            <el-option
              v-for="item of stateList"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="起投时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="item.startPutinTime"
            type="datetime"
            placeholder="选择起投时间"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 3 }"
            placeholder="请输入备注"
            v-model="item.remarks"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="is_clickEditor = false">取 消</el-button>
        <el-button type="primary" @click="clickEditorOk()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
.page-nav {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>

<script>
import { statusManage } from "@/api/putManagement";
import { ceanEngineUpdate } from "@/api/putManagement";
import { putonAgent } from "@/api/agent";
const moment = require("moment");

export default {
  name: "Management",
  data() {
    return {
      listLoading: false, // 加载动画
      formLabelWidth: "120px", // 统一设置列的宽度
      is_clickEditor: false, // 控制编辑弹窗显示隐藏
      EditorType: false, // 判断是添加操作，还是编辑操作
      queryDate: [],
      pkgOption: JSON.parse(window.localStorage.getItem("pkgOption")),
      condition: {
        // startDate: "", // 起始日期
        // endDate: "", // 截止日期
        pkg: "", // 产品
        channelId: "", // 渠道
        putinStatus: "" // 投放状态
      },
      agentList: [], // 渠道列表
      putList: [
        {
          appName: "appName", // 产品
          agentName: "agentName", // 渠道
          putinStatus: 1, // 投放状态
          startPutinTime: "startPutinTime", // 起投时间
          remarks: "remarks" // 备注
        }
      ], // 投放列表

      platformList: [
        { value: 1, label: "头条" },
        { value: 2, label: "广点通" },
        { value: 3, label: "快手" },
        { value: 4, label: "微博" },
        { value: 5, label: "TikTok" },
        { value: 6, label: "网易" },
        { value: 7, label: "爱奇艺" }
      ],
      stateList: [
        { value: 1, label: "开户中" },
        { value: 2, label: "准备素材" },
        { value: 3, label: "开始投放" },
        { value: 4, label: "稳定投放" },
        { value: 5, label: "停投" }
      ], // 投放状态
      item: {
        pkg: "", // 产品
        channelId: "", //渠道
        platformId: "", // 媒体平台
        putinStatus: "1", // 投放状态
        startPutinTime: "", // 起投时间
        remarks: "" // 备注
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              let end = new Date();
              let start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              start = moment(start).format("YYYY-MM-DD");
              end = moment(end).format("YYYY-MM-DD");
              console.log("最近一周", start, end);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              let end = new Date();
              let start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              start = moment(start).format("YYYY-MM-DD");
              end = moment(end).format("YYYY-MM-DD");
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              let end = new Date();
              let start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              start = moment(start).format("YYYY-MM-DD");
              end = moment(end).format("YYYY-MM-DD");
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      }
    };
  },

  created() {
    // let beforeDay = moment()
    //   .subtract(0, "days")
    //   .format("YYYY-MM-DD");
    // this.condition.startDate = beforeDay;
    // this.condition.endDate = moment().format("YYYY-MM-DD");
    // this.queryDate.push(beforeDay, moment().format("YYYY-MM-DD"));
    this.putonAgent();
    this.statusManage(); // 获取渠道列表
  },

  methods: {
    //获取渠道列表
    putonAgent: function() {
      putonAgent("get", {}).then(res => {
        this.listLoading = false;
        if (res.status === 200) {
          this.agentList = res.result.data;
        } else {
          // alert(res.msg);
        }
      });
    },

    // 返回投放状态
    stateFormat: function(row, column) {
      switch (row.putinStatus) {
        case 1:
          return "开户中";
          break;
        case 2:
          return "准备素材";
          break;
        case 3:
          return "开始投放";
          break;
        case 4:
          return "稳定投放";
          break;
        case 5:
          return "停投";
          break;
      }
    },

    // 返回媒体平台名字
    platformIdFormat: function(row, column) {
      switch (row.platformId) {
        case 1:
          return "头条";
          break;
        case 2:
          return "广点通";
          break;
        case 3:
          return "快手";
          break;
        case 4:
          return "微博";
          break;
        case 5:
          return "TikTok";
          break;
        case 6:
          return "网易";
          break;
        case 7:
          return "爱奇艺";
          break;
      }
    },

    // 获取渠道列表
    statusManage: function() {
      // this.condition.startDate = this.queryDate[0];
      // this.condition.endDate = this.queryDate[1];

      statusManage("get", {
        ...this.condition
      }).then(res => {
        this.listLoading = false;
        if (res.status === 200) {
          this.putList = res.result.data;
          for (let i = 0; i < this.putList.length; i++) {
            this.putList[i].difference = (
              this.putList[i].cost - this.putList[i].yesCost
            ).toFixed(2); // 差值
            if (!this.putList[i].yesCost) {
              this.putList[i].yesCost = 0;
            }
          }
        } else {
          // alert(res.msg);
        }
      });
    },

    // 添加
    addAgent: function() {
      this.EditorType = true;
      this.is_clickEditor = true;
      this.item = {
        pkg: "", // 产品
        channelId: "", //渠道
        platformId: "", // 媒体平台
        putinStatus: 1, // 投放状态
        startPutinTime: "", // 起投时间
        remarks: "" // 备注
      };
    },

    //编辑
    clickEditor: function(e) {
      this.EditorType = false;
      console.log("编辑===", e);
      this.is_clickEditor = true;
      this.item = { ...e };
    },

    //删除
    clickDelete: function(e) {
      console.log("删除===", e);
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          statusManage("delete", {
            id: e.id
          }).then(res => {
            this.listLoading = false;
            if (res.status === 200) {
              this.statusManage();
              this.$message({
                message: "删除成功",
                type: "success"
              });
            } else {
              // alert(res.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            message: "已取消删除",
            type: "info"
          });
        });
    },

    // 编辑后点击确定
    clickEditorOk: function() {
      if (!this.item.pkg) {
        this.$message({
          message: "请选择产品",
          type: "warning"
        });
        return;
      }

      if (!this.item.channelId) {
        this.$message({
          message: "请选择渠道",
          type: "warning"
        });
        return;
      }

      if (!this.item.platformId) {
        this.$message({
          message: "请选择媒体平台",
          type: "warning"
        });
        return;
      }

      let startPutinTime = this.item.startPutinTime;
      if (startPutinTime) {
        startPutinTime = moment(startPutinTime).format("YYYY-MM-DD HH:MM:SS");
      }

      let id = this.EditorType ? "" : this.item.id;

      statusManage("post", {
        id: id,
        pkg: this.item.pkg,
        channelId: this.item.channelId,
        platformId: this.item.platformId,
        putinStatus: this.item.putinStatus,
        startPutinTime: startPutinTime,
        remarks: this.item.remarks
      }).then(res => {
        this.listLoading = false;
        this.is_clickEditor = false;
        if (res.status === 200) {
          this.statusManage(); // 获取渠道列表

          this.$message({
            message: this.EditorType ? "添加成功" : "编辑成功",
            type: "success"
          });
        } else {
          // alert(res.msg);
        }
      });
    }
  }
};
</script>
