<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>
      <Button type="primary" class="primary_btn" @click="toAdd()">新建</Button>
    </div>
    <div class="table_wrap">
      <Table :columns="columns" :data="searchForm.list">
        <template slot-scope="{ row }" slot="noticeTypeSlot">
          <div>{{ row.noticeType === 1 ? "所有区域" : "自定义区域" }}</div>
        </template>
        <template slot-scope="{ row }" slot="actionSlot">
          <div class="action_list">
            <div @click="toView(row)">查看</div>
            <div @click="toEdit(row)">编辑</div>
            <div class="action_del" @click="toDel(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="noticeInfo.show"
      :title="noticeInfo.noticeid ? '编辑' : '新建'"
    >
      <Form
        v-if="noticeInfo.show"
        ref="addForm"
        :model="noticeInfo"
        :rules="rules"
        :label-width="60"
      >
        <FormItem label="标题" prop="noticeTitle">
          <Input
            v-model="noticeInfo.noticeTitle"
            placeholder="请输入标题"
          ></Input>
        </FormItem>
        <FormItem label="内容" prop="noticeContent">
          <MyCkeditor
            editor_ref="noticeEditor"
            :height="240"
            savefolder="/notice"
            @change="getNoticeContent"
            :content="noticeInfo.noticeContent"
          ></MyCkeditor>
        </FormItem>
        <FormItem label="接收" prop="noticeType">
          <RadioGroup
            v-model="noticeInfo.noticeType"
            @on-change="noticeInfo.regionids = []"
          >
            <Radio :label="1" font="16">所有区域</Radio>
            <Radio :label="2" font="16">自定义区域</Radio>
          </RadioGroup>
          <div v-if="noticeInfo.noticeType === 2">
            <CheckboxGroup v-model="noticeInfo.regionids">
              <Checkbox
                :label="regionItem.regionid"
                v-for="regionItem in noticeInfo.regionList"
                :key="regionItem.regionid"
                >{{ regionItem.regionName }}</Checkbox
              >
            </CheckboxGroup>
          </div>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button @click="noticeInfo.show = false">取消</Button>
        <Button @click="saveAddInfo()" type="primary">保存</Button>
      </div>
    </Modal>
    <!-- 查看 -->
    <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 v-if="viewInfo.noticeType === 2" class="notice_regions">
        <div v-for="regionItem in viewInfo.regions" :key="regionItem.regionid">
          {{ regionItem.regionName }}
        </div>
      </div>
      <div slot="footer" style="text-align: center">
        <Button @click="viewInfo.show = false">关闭</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import { region_list_sel_pt } from "@/api/region";
import {
  notice_list,
  notice_delete,
  notice_add,
  notice_edit,
  notice_detail
} from "@/api/notice";
import { dateFormat, htmlHasContent } from "@/utils";
import MyCkeditor from "@/components/Editor/MyCkeditor";
export default {
  components: {
    MyCkeditor
  },
  data() {
    // 通知内容验证
    const noticeContentCheck = (rule, value, callback) => {
      if (htmlHasContent(this.noticeInfo.noticeContent)) {
        callback();
      } else {
        callback(new Error("请输入内容"));
      }
    };
    return {
      searchForm: {
        title: "",
        page: 1,
        size: 10,
        dataRange: [],
        list: [],
        total: 0
      },
      // 新建/编辑
      noticeInfo: {
        show: false,
        noticeid: null,
        rversion: null,
        noticeTitle: "",
        noticeContent: "",
        noticeTxt: "",
        // 1所有区域2自定义区域
        noticeType: 1,
        regionList: [],
        regionids: []
      },
      // 查看信息
      viewInfo: {
        show: false,
        noticeTitle: null,
        createname: null,
        updatetime: null,
        noticeContent: null,
        noticeType: null,
        regions: []
      },
      rules: {
        noticeTitle: [
          {
            required: true,
            message: "请输入标题",
            trigger: "blur"
          }
        ],
        noticeContent: [
          {
            required: true,
            validator: noticeContentCheck,
            trigger: "change"
          }
        ],
        noticeType: [
          {
            required: true,
            type: "number",
            message: "请选择接收区域",
            trigger: "change"
          }
        ]
      },
      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: "接收区域",
          slot: "noticeTypeSlot",
          width: 120,
          align: "center"
        },
        {
          title: "发布人",
          key: "createname",
          width: 140,
          align: "center"
        },
        {
          title: "更新时间",
          key: "updatetime",
          width: 190,
          align: "center"
        },
        {
          title: "操作",
          slot: "actionSlot",
          width: 180,
          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({
        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);
        }
      });
    },
    // 新建
    toAdd() {
      this.noticeInfo = {
        show: true,
        noticeid: null,
        rversion: null,
        noticeTitle: "",
        noticeContent: "",
        noticeTxt: "",
        // 1所有区域2自定义区域
        noticeType: 1,
        regionList: [],
        regionids: []
      };
      this.getRegionList().then((list) => {
        this.noticeInfo.regionList = list;
      });
    },
    // 获取搜索区域列表
    getRegionList() {
      return new Promise((resolve) => {
        region_list_sel_pt({}).then((data) => {
          if (data.code === 0) {
            resolve(data.obj);
          } else {
            resolve([]);
            this.$Message.error(data.msg);
          }
        });
      });
    },
    getNoticeContent(html, text = "") {
      this.noticeInfo.noticeContent = html;
      this.noticeInfo.noticeTxt = text.replace("\n", "");
    },
    saveAddInfo() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          if (
            this.noticeInfo.noticeType === 2 &&
            this.noticeInfo.regionids === 0
          ) {
            return;
          }
          this.noticeInfo.show = false;
          let form = {
            noticeTitle: this.noticeInfo.noticeTitle,
            noticeContent: this.noticeInfo.noticeContent,
            noticeTxt: this.noticeInfo.noticeTxt,
            noticeType: this.noticeInfo.noticeType,
            regionids: this.noticeInfo.regionids
          };
          if (this.noticeInfo.noticeid && this.noticeInfo.rversion) {
            form.noticeid = this.noticeInfo.noticeid;
            form.rversion = this.noticeInfo.rversion;
          }
          let api =
            this.noticeInfo.noticeid && this.noticeInfo.rversion
              ? notice_edit
              : notice_add;
          api(form).then((data) => {
            if (data.code === 0) {
              this.searchList();
              this.$Message.success(data.msg);
            } else {
              this.$Message.error(data.msg);
            }
          });
        }
      });
    },
    // 查看
    toView(row) {
      notice_detail({
        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);
        }
      });
    },
    // 编辑
    toEdit(row) {
      notice_detail({
        noticeid: row.noticeid
      }).then((data) => {
        if (data.code === 0) {
          this.noticeInfo = {
            show: true,
            noticeid: data.obj.noticeid,
            rversion: data.obj.rversion,
            noticeTitle: data.obj.noticeTitle,
            noticeContent: data.obj.noticeContent,
            noticeTxt: data.obj.noticeTxt,
            // 1所有区域2自定义区域
            noticeType: data.obj.noticeType,
            regionList: [],
            regionids:
              data.obj.noticeType === 1
                ? []
                : data.obj.regions.map((item) => item.regionid)
          };
          this.getRegionList().then((list) => {
            this.noticeInfo.regionList = list;
          });
        } else {
          this.$Message.error(data.msg);
        }
      });
    },
    // 删除
    toDel(row) {
      this.$Modal.confirm({
        title: "提示",
        content: "您确定删除选中数据吗？",
        onOk: () => {
          notice_delete({
            noticeid: row.noticeid,
            rversion: row.rversion
          }).then((data) => {
            if (data.code === 0) {
              this.searchList();
              this.$Message.success(data.msg);
            } else {
              this.$Message.error(data.msg);
            }
          });
        },
        onCancel: () => {}
      });
    }
  }
};
</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>
