<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="产品id" prop="productId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.productId"-->
      <!--          placeholder="请输入产品id"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="产品名称" prop="productId">
        <el-select style="width: 100%;" filterable v-model="queryParams.productId" placeholder="请选择产品名称" clearable>
          <el-option v-for="info in lsInfo" :key="info.id" :label="info.productName" @keyup.enter.native="handleQuery"
                     :value="info.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="海报分类" prop="posterType">
        <el-select v-model="queryParams.posterType" placeholder="选择海报分类" clearable @keyup.enter.native="handleQuery">
          <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-form-item label="海报名称" prop="posterName">
        <el-input
          v-model="queryParams.posterName"
          placeholder="请输入海报名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="海报标签" prop="posterTag">
        <el-select v-model="queryParams.posterTag" placeholder="选择海报标签" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="dict in dict.type.poster_tag" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="选择状态" clearable @keyup.enter.native="handleQuery">
          <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-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="['poster:poster: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="['poster:poster: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="['poster:poster: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="['poster:poster:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          size="mini"
          @click="shelves"
          :disabled="multiple"
          v-hasPermi="['poster:poster:shelves']"
        >上架
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          @click="offShelves"
          :disabled="multiple"
          v-hasPermi="['poster:poster:offShelves']"
        >下架
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="posterList" @sort-change="handleSortChange"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="排序" width="80"  prop="serialNumber" sortable='custom'/>
      <el-table-column label="海报名称"  width="120" prop="posterName" sortable='custom'/>
      <el-table-column label="分类"  prop="posterType" width="80" sortable='custom'>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_classify" :value="scope.row.posterType"/>
        </template>
      </el-table-column>
      <el-table-column label="产品名称"  prop="productName" width="120" sortable='custom'/>
      <!--      <el-table-column label="海报分类" align="center" prop="posterType" sortable='custom'/>-->
      <el-table-column label="海报标签"  min-width="100" prop="posterTag" sortable='custom'>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.poster_tag" :value="scope.row.posterTag"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="海报图片" align="center" prop="posterFileId" sortable='custom'/>-->
      <el-table-column label="状态"  prop="status" width="80" 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="备注"  prop="remark" sortable='custom' show-overflow-tooltip/>
      <el-table-column label="操作" align="center" 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="['poster:poster:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['poster:poster:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :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="posterType">
              <el-select style="width: 100%;" v-model="form.posterType" 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="posterName">
              <el-input v-model="form.posterName" placeholder="请输入海报名称" :disabled="readonly"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="海报标签" prop="posterTag">
              <el-select style="width: 100%;" v-model="form.posterTag" placeholder="选择海报标签" filterable clearable :disabled="readonly"
                         multiple>
                <el-option v-for="dict in dict.type.poster_tag" :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="status">
              <el-select style="width: 100%;" v-model="form.status" placeholder="选择状态" clearable :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="12">-->
          <!--            <el-form-item label="海报图片" prop="posterFileId">-->
          <!--              <el-input v-model="form.posterFileId" placeholder="请输入海报图片"/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="排序" prop="serialNumber">
              <el-input-number v-model="form.serialNumber" placeholder="请输入排序" :disabled="readonly" :min="0" style="width: 100%;"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.posterType != '1'">
            <el-form-item label="产品名称" prop="productId">
              <el-select style="width: 100%;" filterable v-model="form.productId" placeholder="请选择产品名称" :disabled="readonly"
                         clearable>
                <el-option v-for="info in lsInfo" :key="info.id" :label="info.productName"
                           :value="info.id"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <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>
<!--          <BannerUpload v-show="form.id" :bannerId="form.id" :businessUnits="'海报信息'" :maxLength="1">-->
<!--          </BannerUpload>-->
        </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 {pageListPoster, getPoster, delPoster, addPoster, updatePoster, changeStatusByPoster} from "@/api/poster/poster";
import {changeStatus, getInfo, listInfo} from "@/api/product/info";
import {uploadFiles, getAttachList} from "@/api/common/file";
import {getToken} from "@/utils/auth";
import {changeStatusByArticle} from "@/api/article/article";

export default {
  name: "Poster",
  dicts: ["product_status", "product_classify", "poster_tag"],
  data() {
    return {
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      formData: {
        field101: null,
      },
      field101Action: process.env.VUE_APP_BASE_API + '/common/uploads',
      fileList: [],
      //产品
      lsInfo: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中名字
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品海报表格数据
      posterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      readonly: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productId: null,
        posterType: null,
        posterName: null,
        posterTag: null,
        posterFileId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productId: [
          {required: true, message: "产品名称不能为空", trigger: "blur"}
        ],
        posterType: [
          {required: true, message: "海报分类不能为空", trigger: "change"}
        ],
        posterName: [
          {required: true, message: "海报名称不能为空", trigger: "blur"}
        ],
        posterTag: [
          {required: true, message: "海报标签不能为空", trigger: "blur"}
        ],
        posterFileId: [
          {required: true, message: "海报图片不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "状态不能为空", trigger: "change"}
        ],
        // serialNumber: [
        //   {required: true, message: "序号不能为空", trigger: "change"}
        // ],
      }
    };
  },
  created() {
    this.getList();
    this.getInfoList();
  },
  methods: {
    offShelves() {
      const ids = this.ids;
      let param = {
        ids: ids,
        status: "0"
      }
      this.$modal.confirm('是否确认下架数据？').then(function () {
        return  changeStatusByPoster(param)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("下架成功");
      }).catch(() => {
      });
      // changeStatusByPoster(param).then(response => {
      //   this.getList();
      //   this.$modal.msgSuccess("修改成功");
      // });
    },
    shelves() {
      const ids = this.ids;
      let param = {
        ids: ids,
        status: "1"
      }
      changeStatusByPoster(param).then(response => {
        this.getList();
        this.$modal.msgSuccess("修改成功");
      });
    },
    getInfoList() {
      listInfo().then(response => {
        this.lsInfo = response.data;
      });
    },
    /** 查询产品海报列表 */
    getList() {
      this.loading = true;
      pageListPoster(this.queryParams).then(response => {
        this.posterList = 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,
        productId: null,
        posterType: null,
        posterName: null,
        posterTag: null,
        posterFileId: 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.posterName);
      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;
      getPoster(id).then(response => {
        this.form = response.data;
        if (this.form.posterTag) {
          this.form.posterTag = this.form.posterTag.split(',');
        }
        getAttachList(this.form.id).then(response => {
          this.fileList = response.data;
        });
        this.open = true;
        this.readonly = true;
        this.title = "查看产品海报";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPoster(id).then(response => {
        this.form = response.data;
        if (this.form.posterTag) {
          this.form.posterTag = this.form.posterTag.split(',');
        }
        getAttachList(this.form.id).then(response => {
          this.fileList = response.data;
        });
        this.open = true;
        this.readonly = false;
        this.title = "修改产品海报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (this.form.posterTag) {
          this.form.posterTag = this.form.posterTag.join(',');
        }
        if (valid) {
          if (this.title.indexOf('修改') !== -1) {
            updatePoster(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPoster(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
          // if (this.form.id != null) {
          //   updatePoster(this.form).then(response => {
          //     this.$modal.msgSuccess("修改成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
          //   addPoster(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 delPoster(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('poster/poster/export', {
        ...this.queryParams
      }, `产品海报数据_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

</style>
