<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布者" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入发布者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平台" prop="platformName">

        <el-select v-model="queryParams.platformName" placeholder="请选择平台" clearable>
          <el-option
            v-for="data in platformList"
            :key="data.code"
            :label="data.title"
            :value="data.code"
          />
        </el-select>

      </el-form-item>
      <el-form-item label="稿费区间">
        <el-col :span="11">
          <el-form-item prop="beginFeeDown">
            <el-input-number
              v-model="queryParams.beginFeeDown"
              placeholder="稿费下限"
              controls-position="right"
              :min="0"
              @change="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="11" style="margin-left: 5%">
          <el-form-item prop="endFeeDown">
            <el-input-number
              v-model="queryParams.endFeeDown"
              placeholder="稿费上限"
              controls-position="right"
              :min="0"
              @change="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="粉丝要求" prop="fansDown">
        <el-col :span="11">
          <el-form-item prop="beginFeeDown">
            <el-input-number
              v-model="queryParams.beginFeeDown"
              placeholder="粉丝下限"
              controls-position="right"
              :min="0"
              @change="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="11" style="margin-left: 5%">
          <el-form-item prop="endFeeDown">
            <el-input-number
              v-model="queryParams.endFansDown"
              placeholder="粉丝上限"
              controls-position="right"
              :min="0"
              @change="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="内容形式" prop="pic">
        <el-select v-model="queryParams.pic" placeholder="请选择内容形式" clearable>
          <el-option
            v-for="dict in dict.type.notice_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="展示形式" prop="collect">
        <el-select v-model="queryParams.collect" placeholder="请选择展示形式" clearable>
          <el-option
            v-for="dict in dict.type.notice_collect"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否使用通告券" prop="isUseCoupon">
        <el-select v-model="queryParams.isUseCoupon" placeholder="请选择是否使用通告券" clearable>
          <el-option
            v-for="dict in dict.type.true_or_false"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="approvalStatus">
        <el-select v-model="queryParams.approvalStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.dl_approval_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </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="['busi:notice:add']"
        >新增</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="['busi:notice: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="['busi:notice:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="平台" width="100" align="center" prop="platformName" />
      <el-table-column label="标题" width="100" align="center" prop="title" />
      <el-table-column label="发布者" width="100" align="center" prop="userNickName" />
      <el-table-column label="浏览量" width="100" align="center" prop="viewNum" />

      <el-table-column label="平台名称" width="100" align="center" prop="platformName" />
      <el-table-column label="稿费下限" width="100" align="center" prop="feeDown" />
      <el-table-column label="稿费上限" width="100" align="center" prop="feeUp" />
      <el-table-column label="是否需自报价" width="100" align="center" prop="isSelfPrice">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.true_or_false" :value="scope.row.isSelfPrice"/>
        </template>
      </el-table-column>
      <el-table-column label="截止日期" width="100" align="center" prop="endDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="品牌" width="100" align="center" prop="brand" />
      <el-table-column label="是否公开品牌" width="100" align="center" prop="isShowBrand">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.true_or_false" :value="scope.row.isShowBrand"/>
        </template>
      </el-table-column>
      <el-table-column label="招募名额" width="100" align="center" prop="needNum" />
      <el-table-column label="粉丝要求下限" width="100" align="center" prop="fansDown" />
      <el-table-column label="粉丝要求上限" width="100" align="center" prop="fansUp" />


      <el-table-column label="产品图" width="100" align="center" prop="images" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.images" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="博主类型" width="100" align="center" prop="bloggerTypes" />
      <el-table-column label="使用通告券" width="100" align="center" prop="isUseCoupon">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.true_or_false" :value="scope.row.isUseCoupon"/>
        </template>
      </el-table-column>
      <el-table-column label="认证时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="审核状态" width="100" align="center" prop="approvalStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dl_approval_status" :value="scope.row.approvalStatus"/>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="审核人" align="center" prop="approvalUserName" >
        <template slot-scope="scope">
         <span v-if="scope.row.approvalUserName">
           {{scope.row.approvalUserName}}
         </span>
          <span v-else>
            <span v-if="scope.row.approvalStatus == 1">
              {{"自动通过"}}
            </span>
          </span>
        </template>
      </el-table-column>
      <el-table-column fixed="right"  label="审核备注" align="center" prop="approvalRemark" />
      <el-table-column fixed="right" label="审核时间" align="center" prop="approvalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approvalTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="220px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.approvalStatus == 0"
            @click="handleAudit(scope.row)"
            v-hasPermi="['busi:notice:edit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.approvalStatus == 1"
            @click="closeNotice(scope.row)"
            v-hasPermi="['busi:notice:edit']"
          >关闭</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.approvalStatus == 0"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['busi:notice:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['busi:notice:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1500px" append-to-body>
      <EditForm ref="editForm" v-if="open" :is-detail="isDetail"></EditForm>
      <div slot="footer" class="dialog-footer" v-if="!isDetail">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核功能弹窗 -->
    <el-dialog title="通告审核" :visible.sync="auditOpen" width="800px" append-to-body>
      <el-form  ref="form" :model="auditForm" v-if="auditOpen"  label-width="150px">
        <el-row :gutter="20">
            <el-form-item label="审核结果" prop="approvalStatus">
              <el-radio-group v-model="auditForm.approvalStatus">
                <el-radio :label="1">通过</el-radio>
                <el-radio :label="0">拒绝</el-radio>
              </el-radio-group>
            </el-form-item>
        </el-row>
        <el-form-item label="备注" prop="detail">
          <el-input
            type="textarea"

            placeholder="请输入内容"
            v-model="auditForm.approvalRemark">
          </el-input>

        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" >
        <el-button type="primary" @click="auditFunction()">完 成</el-button>
        <el-button @click="auditOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listNotice, getNotice, delNotice, addNotice, updateNotice, auditInfo,publishTakeDown} from "@/api/busi/notice";
import {listByCode} from "@/api/base/category";
import {getRegion} from "@/api/base/util";
import  EditForm from "./component/EditForm.vue"

export default {
  name: "Notice",
  dicts: ['notice_type', 'notice_collect', 'dl_approval_status', 'true_or_false'],
  components: {EditForm},
  data() {
    return {
      isDetail:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      auditOpen:false,
      // 审核备注时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        title: null,
        platformCode: null,
        platformName: null,
        isSelfPrice: null,
        giftDetail: null,
        giftPrice: null,
        endDate: null,
        brand: null,
        isShowBrand: null,
        needNum: null,
        fansDown: null,
        fansUp: null,
        isEligible: null,
        pic: null,
        collect: null,
        detail: null,
        images: null,
        bloggerTypes: null,
        isShowTel: null,
        wechat: null,
        tel: null,
        groupImage: null,
        isUseCoupon: null,
        approvalStatus: null,
        approvalUserId: null,
        approvalTime: null,
        approvalRemark: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      auditForm:{
        approvalStatus:1,
        approvalRemark:null
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        platformCode: [
          { required: true, message: "平台code不能为空", trigger: "change" }
        ],
        feeDown: [
          { required: true, message: "稿费下限不能为空", trigger: "blur" }
        ],
        isSelfPrice: [
          { required: true, message: "是否需自报价不能为空", trigger: "change" }
        ],
        endDate: [
          { required: true, message: "截止日期不能为空", trigger: "blur" }
        ],
        pic: [
          { required: true, message: "图文/视频/不限不能为空", trigger: "change" }
        ],
        collect: [
          { required: true, message: "单品/合集/不限不能为空", trigger: "change" }
        ],
        detail: [
          { required: true, message: "通告明细不能为空", trigger: "blur" }
        ]
      },
      platformList:[],
      provinceList:[],
      cityList:[]
    };
  },
  created() {
    this.getList();
    this.getPlatform()
  },
  methods: {
    auditFunction(){
      auditInfo(this.auditForm).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.auditOpen = false;
        this.getList();
      });
    },
    getProvince(){
      getRegion(null).then(res=>{
        this.provinceList = res.data
      })
    },
    getCity(provinceId){
      getRegion(provinceId).then(res=>{
        this.cityList = res.data
      })
    },
    getPlatform(){
      listByCode({code:'dl_platform'}).then(res=>{
        this.platformList = res.data
      })
    },
    /** 查询通告列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listNotice(this.queryParams).then(response => {
        this.noticeList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        title: null,
        platformCode: null,
        platformName: null,
        country: null,
        feeDown: null,
        feeUp: null,
        isSelfPrice: null,
        giftDetail: null,
        giftPrice: null,
        endDate: null,
        brand: null,
        isShowBrand: null,
        needNum: null,
        fansDown: null,
        fansUp: null,
        isEligible: null,
        pic: null,
        collect: null,
        detail: null,
        images: null,
        bloggerTypes: null,
        isShowTel: null,
        wechat: null,
        tel: null,
        groupImage: null,
        isUseCoupon: null,
        approvalStatus: null,
        approvalUserId: null,
        approvalTime: null,
        approvalRemark: null,
        creator: null,
        createTime: null,
        updater: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isDetail = false
      this.open = true;
      this.title = "添加通告";
    },
    handleDetail(row){
      this.reset();
      const id = row.id || this.ids
      getNotice(id).then(response => {
        this.isDetail = true
        this.open = true;
        this.$nextTick(() => {
          this.$refs.editForm.form.bloggerTypeList = response.data.bloggerTypes.split(',')
          this.$refs.editForm.form = response.data;
        });
        this.title = "通告详情";
      });
    },
    closeNotice(row){
      let param = {}
      param.noticeId = row.id
      param.publishTakeDown = '0'
      publishTakeDown(param).then(response => {
        this.$modal.msgSuccess("关闭成功");
        this.getList();
      });
    },
    handleAudit(row) {
      this.auditForm.id = row.id
      this.auditForm.approvalStatus = 1
      this.auditForm.approvalRemark = null
      this.auditOpen = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNotice(id).then(response => {
        this.isDetail = false
        this.open = true;
        this.$nextTick(() => {
          this.$refs.editForm.form = response.data;
          this.$refs.editForm.form.bloggerTypeList = response.data.bloggerTypes.split(',')
        });
        this.title = "修改通告";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.editForm.$refs["form"].validate(valid => {
        if (valid) {
          let requestData = this.$refs.editForm.form
          requestData.bloggerTypes = this.$refs.editForm.form.bloggerTypeList.join(',')
          if (requestData.id != null) {
            updateNotice(requestData).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotice(requestData).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除通告编号为"' + ids + '"的数据项？').then(function() {
        return delNotice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('busi/notice/export', {
        ...this.queryParams
      }, `notice_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.fixed-height-dialog {
  height: 900px; /* 固定高度 */

}

.dialog-form {

  overflow-y: auto; /* 内容超出时滚动 */
}

</style>
