<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="朋友圈类型" prop="momentKind">
        <el-input
          v-model="queryParams.momentKind"
          placeholder="请输入朋友圈类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="朋友圈标题" prop="momentTitle">
        <el-input
          v-model="queryParams.momentTitle"
          placeholder="请输入朋友圈标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['moment:moment:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['moment:moment:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['moment:moment:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['moment:moment:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          size="mini"
          @click="shelves"
          :disabled="multiple"
          v-hasPermi="['moment:moment:shelves']"
        >上架
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          @click="offShelves"
          :disabled="multiple"
          v-hasPermi="['moment:moment:offShelves']"
        >下架
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="momentList" @sort-change="handleSortChange"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="标题" prop="momentTitle" width="150" sortable='custom'/>
      <el-table-column label="分类" width="80" prop="momentType" sortable='custom'>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_classify" :value="scope.row.momentType"/>
        </template>
      </el-table-column>
      <el-table-column label="类型" width="100" prop="momentKind" sortable='custom'>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.moment_kind" :value="scope.row.momentKind"/>
        </template>
      </el-table-column>
      <el-table-column label="朋友圈正文"  prop="momentContent" sortable='custom' show-overflow-tooltip/>
      <el-table-column label="备注" prop="remark" sortable='custom' show-overflow-tooltip/>
      <el-table-column label="状态" width="80" prop="status" sortable='custom'>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleRead(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['moment:moment:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['moment:moment:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      hide-on-single-page
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品朋友圈对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="朋友圈分类" prop="momentType">
              <el-select  style="width: 100%" v-model="form.momentType" placeholder="选择朋友圈分类" :disabled="readonly">
                <el-option
                  v-for="dict in dict.type.product_classify"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="朋友圈类型" prop="momentKind">
              <el-select  style="width: 100%" v-model="form.momentKind" placeholder="选择朋友圈类型" :disabled="readonly">
                <el-option
                  v-for="dict in dict.type.moment_kind"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="朋友圈标题" prop="momentTitle">
              <el-input v-model="form.momentTitle" placeholder="请输入朋友圈标题" :disabled="readonly"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select style="width: 100%" v-model="form.status" placeholder="请选择状态" :disabled="readonly">
                <el-option
                  v-for="dict in dict.type.product_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="朋友圈正文">
              <el-input v-model="form.momentContent" type="textarea" :min-height="192"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="readonly"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="上传" prop="field">
          <el-form :model="form">
            <PictureUpload :biz-id="form.id" :readonly="readonly"></PictureUpload>
          </el-form>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!readonly">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {pageListMoment, getMoment, delMoment, addMoment, updateMoment,changeStatusByMoment} from "@/api/moment/moment";
import {addPoster, updatePoster} from "@/api/poster/poster";
import {getArticle} from "@/api/article/article";

export default {
  name: "Moment",
  dicts: ["product_status", "product_classify", "moment_kind"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中名字
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品朋友圈表格数据
      momentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      readonly:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        momentType: null,
        momentKind: null,
        momentTitle: null,
        momentContent: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        momentType: [
          {required: true, message: "朋友圈分类不能为空", trigger: "change"}
        ],
        momentKind: [
          {required: true, message: "朋友圈类型不能为空", trigger: "blur"}
        ],
        momentTitle: [
          {required: true, message: "朋友圈标题不能为空", trigger: "blur"}
        ],
        momentContent: [
          {required: true, message: "朋友圈正文不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "状态不能为空", trigger: "change"}
        ],
        // remark: [
        //   {required: true, message: "备注不能为空", trigger: "blur"}
        // ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    offShelves(){
      const ids = this.ids;
      let param = {
        ids: ids,
        status: "0"
      }
      this.$modal.confirm('是否确认下架数据？').then(function () {
        return changeStatusByMoment(param)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("下架成功");
      }).catch(() => {
      });
    },
    shelves(){
      const ids = this.ids;
      let param = {
        ids: ids,
        status: "1"
      }
      changeStatusByMoment(param).then(response => {
        this.getList();
        this.$modal.msgSuccess("修改成功");
      });
    },
    /** 查询产品朋友圈列表 */
    getList() {
      this.loading = true;
      pageListMoment(this.queryParams).then(response => {
        this.momentList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(e => {
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        momentType: null,
        momentKind: null,
        momentTitle: null,
        momentContent: null,
        status: "0",
        remark: null,
      };
      this.resetForm("form");
    },
    //排序
    handleSortChange(col) {
      this.$sortBy(col, this.queryParams);
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.names = selection.map(item => item.momentTitle);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.readonly = false;
      this.title = "添加产品朋友圈";
      const { v4: uuidv4 } = require('uuid');
      const uniqueID = uuidv4().replace(/-/g, '');
      this.$set(this.form, 'id', uniqueID);
    },
    handleRead(row) {
      this.reset();
      const id = row.id || this.ids;
      getMoment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.readonly = true;
        this.title = "查看产品朋友圈";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMoment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.readonly = false;
        this.title = "修改产品朋友圈";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // if (this.form.id != null) {
          //   updateMoment(this.form).then(response => {
          //     this.$modal.msgSuccess("修改成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
          //   addMoment(this.form).then(response => {
          //     this.$modal.msgSuccess("新增成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // }
          if (this.title.indexOf('修改') !== -1) {
            updateMoment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMoment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.id || this.names;
      this.$modal.confirm('是否确认删除朋友圈标题为"' + names + '"的数据项？').then(function () {
        return delMoment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('moment/moment/export', {
        ...this.queryParams
      }, `产品朋友圈_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
