<template>
  <div class="main_root">
    <div class="search_header">
      <div class="search_left">
        <DatePicker
          v-model="searchForm.dataRange"
          @on-change="searchList()"
          :editable="false"
          :transfer="true"
          format="yyyy-MM-dd"
          type="daterange"
          placement="bottom-start"
          placeholder="请选择日期范围"
          style="margin-right: 10px; width: 220px"
        ></DatePicker>
        <Input
          v-model="searchForm.title"
          placeholder="请输入名称"
          search
          @on-search="searchList()"
          style="width: 150px"
        />
      </div>
    </div>
    <div class="table_wrap">
      <Table :columns="columns" :data="searchForm.list">
        <template slot-scope="{ row }" slot="actionSlot">
          <div class="action_list">
            <div @click="toView(row)">查看</div>
          </div>
        </template>
      </Table>
    </div>
    <div class="page_wrap">
      <Page
        :transfer="true"
        :total="searchForm.total"
        :current="searchForm.page"
        :page-size="searchForm.size"
        :page-size-opts="[10, 20, 40, 60]"
        @on-change="pageChange"
        @on-page-size-change="pageSizeChange"
        show-total
        show-sizer
      ></Page>
    </div>
    <!-- 查看 -->
    <Modal
      class="modal1"
      :mask-closable="false"
      v-model="viewInfo.show"
      title="查看"
    >
      <div class="notice_title">{{ viewInfo.noticeTitle }}</div>
      <div class="notice_create">
        <div>{{ viewInfo.createname }}</div>
        <div>{{ viewInfo.updatetime }}</div>
      </div>
      <div class="notice_content" v-html="viewInfo.noticeContent"></div>
      <div slot="footer" style="text-align: center">
        <Button @click="viewInfo.show = false">关闭</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import { notice_list_region, notice_detail_region } from "@/api/notice";
import { dateFormat } from "@/utils";
export default {
  data() {
    return {
      searchForm: {
        title: "",
        page: 1,
        size: 10,
        dataRange: [],
        list: [],
        total: 0
      },
      // 查看信息
      viewInfo: {
        show: false,
        noticeTitle: null,
        createname: null,
        updatetime: null,
        noticeContent: null,
        noticeType: null,
        regions: []
      },
      columns: [
        {
          title: "序号",
          align: "center",
          width: 70,
          render: (h, params) => {
            return h(
              "span",
              params.index +
                (this.searchForm.page - 1) * this.searchForm.size +
                1
            );
          }
        },
        {
          title: "标题",
          key: "noticeTitle",
          align: "center"
        },
        {
          title: "发布人",
          key: "createname",
          width: 140,
          align: "center"
        },
        {
          title: "时间",
          key: "updatetime",
          width: 190,
          align: "center"
        },
        {
          title: "操作",
          slot: "actionSlot",
          width: 90,
          align: "center"
        }
      ]
    };
  },
  created() {
    this.searchList();
  },
  methods: {
    // 搜索
    searchList() {
      this.searchForm.page = 1;
      this.getList();
    },
    // 页码改变
    pageChange(page) {
      this.searchForm.page = page;
      this.getList();
    },
    // 每页显示数量改变
    pageSizeChange(size) {
      this.searchForm.size = size;
      this.searchForm.page = 1;
      this.getList();
    },
    // 获取列表
    getList() {
      let _begindate = this.searchForm.dataRange[0];
      _begindate = _begindate ? dateFormat(_begindate, "yyyy-MM-dd") : null;
      let _enddate = this.searchForm.dataRange[1];
      _enddate = _enddate ? dateFormat(_enddate, "yyyy-MM-dd") : null;
      notice_list_region({
        page: this.searchForm.page,
        size: this.searchForm.size,
        title: this.searchForm.title,
        begindate: _begindate,
        enddate: _enddate
      }).then((data) => {
        if (data.code === 0) {
          this.searchForm.list = data.obj.data;
          this.searchForm.total = data.obj.total;
        } else {
          this.$Message.error(data.msg);
        }
      });
    },
    // 查看
    toView(row) {
      notice_detail_region({
        noticeid: row.noticeid
      }).then((data) => {
        if (data.code === 0) {
          this.viewInfo = {
            show: true,
            noticeTitle: data.obj.noticeTitle,
            createname: data.obj.createname,
            updatetime: data.obj.updatetime,
            noticeContent: data.obj.noticeContent,
            noticeType: data.obj.noticeType,
            regions: data.obj.noticeType === 1 ? [] : data.obj.regions
          };
        } else {
          this.$Message.error(data.msg);
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.search_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 16px 16px;
  .search_left {
    display: flex;
    justify-content: flex-start;
    align-items: center;
  }
}
.notice_title {
  margin-bottom: 16px;
  font-size: 18px;
  font-weight: bold;
  text-align: center;
}
.notice_create {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 16px;
  font-size: 12px;
  color: #afbcc7;
  > div {
    margin: 0 10px;
  }
}
.notice_content {
  font-size: 16px;
}
.notice_regions {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
  margin-top: 16px;
  padding: 10px 16px;
  font-size: 14px;
  border-radius: 8px;
  background: #f0f6fc;
  > div {
    margin: 10px 16px;
  }
}
</style>
