 <!-- 运营管理 场景时间 -->

<template>
  <div class="app-container">
    <el-button
      type="primary"
      size="mini"
      icon="el-icon-plus"
      @click="getDialog('新增')"
      v-hasPermi="['operation:sceneTime:add']"
      >新增</el-button
    >
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="时间段" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime }} - {{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间段文案" prop="name" align="center" />
      <el-table-column label="时间Icon" width="100px" align="center">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic" style="background: #000" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createtTime" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDialog('查看', scope.row)"
            >查看</el-button
          >
          <el-button
            type="text"
            @click="getDialog('编辑', scope.row)"
            v-hasPermi="['operation:sceneTime:edit']"
            >编辑</el-button
          >
          <el-button
            type="delete"
            @click="getDelete(scope.row, 2)"
            v-hasPermi="['operation:sceneTime:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗 -->
    <el-dialog
      :visible.sync="dialogVisible"
      :title="title"
      width="500px"
      :before-close="cancel"
    >
      <el-form
        :model="dialogForm"
        ref="dialogForm"
        :rules="rules"
        label-width="110px"
        :disabled="disabled"
      >
        <el-form-item label="时间段：" prop="date">
          <el-time-picker
            is-range
            v-model="dialogForm.date"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="HH:mm:ss"
            format="HH:mm:ss"
            placeholder="选择时间范围"
          />
        </el-form-item>
        <el-form-item label="时间段文案：" prop="name">
          <el-input v-model="dialogForm.name" placeholder="请输入时间段文案" />
        </el-form-item>
        <el-form-item label="时间段Icon：" prop="pic">
          <Upload
            listType="picture-card"
            :url="dialogForm.pic"
            @upload="upload"
            style="background: #000"
            :disabled="disabled"
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button v-if="!disabled" type="primary" @click="getSubmit"
          >确定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { change, detail, list, submit } from "@/api/operation/sceneTime";
import { dialogCallBack } from "@/utils/DialogUtil";
export default {
  name:"OperationSceneTime",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 列表
      tableData: [],
      // 弹窗
      dialogVisible: false,
      // 弹窗title
      title: "",
      // 弹窗表单
      dialogForm: {},
      // 校验
      rules: {
        date: [
          {
            required: true,
            message: "请选择时间段",
            trigger: "change",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入时间段文案",
            trigger: "blur",
          },
        ],
        pic: [
          {
            required: true,
            message: "请上传图片",
            trigger: "change",
          },
        ],
      },
      // 只读
      disabled: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 列表
    getList() {
      this.loading = true;
      list().then((res) => {
        if (res.code === 0) {
          this.tableData = res.data;
          this.loading = false;
        }
      });
    },

    // 弹窗
    getDialog(title, row) {
      this.dialogVisible = true;
      this.title = title;
      this.disabled = title === "查看" ? true : false;
      if (row) {
        this.getDetail(row.id);
      }
    },

    // 详情
    getDetail(id) {
      detail(id).then((res) => {
        if (res.code === 0) {
          this.dialogForm = res.data;
          this.$set(this.dialogForm, "date", [
            res.data.startTime,
            res.data.endTime,
          ]);
        }
      });
    },

    // 上传icon
    upload(e) {
      this.dialogForm.pic = e.file;
    },

    // 取消
    cancel() {
      this.dialogVisible = false;
      this.dialogForm = {
        date: ["", ""],
        name: "",
        pic: "",
      };
      this.$refs.dialogForm.resetFields();
    },

    // 确定
    getSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          this.dialogForm.startTime = this.dialogForm.date[0];
          this.dialogForm.endTime = this.dialogForm.date[1];
          delete this.dialogForm.date;
          submit(this.dialogForm).then((res) => {
            if (res.code === 0) {
              this.$message.success("提交成功！");
              this.getList();
              this.cancel();
            }
          });
        } else {
          return false;
        }
      });
    },

    getDelete(row, status) {
      var that = this;
      dialogCallBack(that, function () {
        that
          .$confirm(`是否删除${row.name}?`, "提示：", {
            type: "warning",
          })
          .then(() => {
            change(row.id, status).then((res) => {
              if (res.code === 0) {
                that.$message.success("删除成功！");
                that.getList();
              }
            });
          });
      });
    },
  },
};
</script>
