<template>
  <div class="main_root">
    <div class="search_header">
      <div class="search_left">
        <Input
          v-model="searchForm.whiteName"
          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="nameSlot">
          <div class="name_slot">
            <div class="name_content" :title="row.whiteName">
              {{ row.whiteName }}
            </div>
            <div v-if="row.whiteType === 2" class="public_tag">公用</div>
          </div>
        </template>
        <template slot-scope="{ row }" slot="whiteAddressSlot">
          <div class="white_address" :title="row.website">
            {{ row.website }}
          </div>
        </template>
        <template slot-scope="{ row }" slot="actionSlot">
          <div class="action_list">
            <div @click="toView(row)" v-if="row.whiteType !== 2">查看</div>
            <div @click="toEdit(row)" v-if="row.whiteType !== 2">编辑</div>
            <div class="action_del" @click="toDel(row)">
              {{ row.whiteType === 2 ? "移除" : "删除" }}
            </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="whiteListInfo.show"
      :title="whiteListInfo.whiteid ? '编辑' : '新建'"
    >
      <Form
        v-if="whiteListInfo.show"
        ref="addForm"
        :model="whiteListInfo"
        :rules="rules"
        :label-width="90"
        inline
      >
        <FormItem label="名称" prop="whiteName" style="width: 100%">
          <Input
            v-model="whiteListInfo.whiteName"
            placeholder="请输入名称"
          ></Input>
        </FormItem>
        <FormItem label="简介" prop="comm" style="width: 100%">
          <Input v-model="whiteListInfo.comm" placeholder="请输入简介"></Input>
        </FormItem>
        <FormItem label="网址" prop="website" style="width: 100%">
          <Input
            type="textarea"
            v-model="whiteListInfo.website"
            placeholder="请填写正确的IP地址或者域名，多个IP或域名之间用英文分号隔开"
          ></Input>
          <div style="color: #7c8db5">
            说明：IP例如：192.168.1.200或者192.168.1.200/24，域名例如：new.iflytek.com
          </div>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button @click="whiteListInfo.show = false">取消</Button>
        <Button @click="saveAddInfo()" type="primary">保存</Button>
      </div>
    </Modal>
    <!-- 查看 -->
    <Modal
      class="modal1"
      :mask-closable="false"
      v-model="viewInfo.show"
      title="查看"
    >
      <div class="view_title">
        <div>{{ viewInfo.whiteName }}</div>
        <div v-if="viewInfo.whiteType === 2" class="public">公用</div>
      </div>
      <Input
        v-if="viewInfo.comm"
        v-model="viewInfo.comm"
        disabled
        type="textarea"
      />
      <Form :label-width="46">
        <FormItem label="网址" style="margin-bottom: 0; width: 100%">{{
          viewInfo.website
        }}</FormItem>
      </Form>
      <div slot="footer" style="text-align: center">
        <Button @click="viewInfo.show = false">关闭</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import {
  white_list,
  white_add,
  white_edit,
  white_delete
} from "@/api/whiteLibrary";
export default {
  data() {
    return {
      searchForm: {
        whiteName: "",
        page: 1,
        size: 10,
        list: [],
        total: 0
      },
      rules: {
        whiteName: [
          {
            required: true,
            message: "请输入名称",
            trigger: "change"
          }
        ]
      },
      // 新建/编辑
      whiteListInfo: {
        show: false,
        whiteid: null,
        rversion: null,
        whiteName: "",
        // 1所有学校2指定学校
        targetSchool: 2,
        website: "",
        regionid: null,
        comm: "",
        schoolids: []
      },
      // 查看
      viewInfo: {
        show: false,
        whiteName: "",
        whiteType: "",
        website: "",
        comm: ""
      },
      columns: [
        {
          title: "序号",
          align: "center",
          width: 70,
          render: (h, params) => {
            return h(
              "span",
              params.index +
                (this.searchForm.page - 1) * this.searchForm.size +
                1
            );
          }
        },
        {
          title: "名称",
          slot: "nameSlot",
          align: "center"
        },
        {
          title: "网址",
          slot: "whiteAddressSlot",
          align: "center"
        },
        {
          title: "操作人",
          key: "updatename",
          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() {
      console.warn("---注意替换regionid---->white_list");
      white_list({
        regionid: 11,
        page: this.searchForm.page,
        size: this.searchForm.size,
        whiteName: this.searchForm.whiteName
      }).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() {
      console.warn("---注意替换regionid---schoolids->");
      this.whiteListInfo = {
        show: true,
        whiteid: null,
        rversion: null,
        whiteName: "",
        // 1所有学校2指定学校
        targetSchool: 2,
        website: "",
        regionid: 11,
        comm: "",
        schoolids: [4]
      };
    },
    // 保存新建
    saveAddInfo() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.whiteListInfo.show = false;
          let api =
            this.whiteListInfo.whiteid && this.whiteListInfo.rversion
              ? white_edit
              : white_add;
          let form = {
            whiteName: this.whiteListInfo.whiteName,
            targetSchool: this.whiteListInfo.targetSchool,
            website: this.whiteListInfo.website,
            comm: this.whiteListInfo.comm,
            schoolids: this.whiteListInfo.schoolids
          };
          if (this.whiteListInfo.whiteid && this.whiteListInfo.rversion) {
            form.whiteid = this.whiteListInfo.whiteid;
            form.rversion = this.whiteListInfo.rversion;
          } else {
            form.regionid = this.whiteListInfo.regionid;
          }
          api(form).then((data) => {
            if (data.code === 0) {
              this.searchList();
              this.$Message.success(data.msg);
            } else {
              this.$Message.error(data.msg);
            }
          });
        }
      });
    },
    // 查看
    toView(row) {
      this.viewInfo = {
        show: true,
        whiteName: row.whiteName,
        whiteType: row.whiteType,
        website: row.website,
        comm: row.comm
      };
    },
    // 编辑
    toEdit(row) {
      console.warn("---注意替换regionid-schoolids--->");
      this.whiteListInfo = {
        show: true,
        whiteid: row.whiteid,
        rversion: row.rversion,
        whiteName: row.whiteName,
        // 1所有学校2指定学校
        targetSchool: 2,
        website: row.website,
        regionid: 11,
        comm: row.comm,
        schoolids: [4]
      };
    },
    // 删除
    toDel(row) {
      this.$Modal.confirm({
        title: "提示",
        content: `确定要将“${row.whiteName}”从白名单中${
          row.whiteType === 2 ? "移除" : "删除"
        }？`,
        onOk: () => {
          white_delete({
            whiteid: row.whiteid,
            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;
  }
}
.view_title {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 16px;
  font-size: 18px;
  font-weight: bold;
  text-align: center;
  .public {
    margin-left: 10px;
    padding: 0 10px;
    line-height: 20px;
    color: #fff;
    font-weight: normal;
    font-size: 12px;
    border-radius: 4px;
    background: #52c41a;
  }
}
.name_slot {
  display: flex;
  justify-content: center;
  align-items: center;
  .name_content {
    max-width: calc(100% - 38px);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .public_tag {
    margin-left: 4px;
    width: 32px;
    text-align: center;
    color: #fff;
    line-height: 1.6;
    font-size: 12px;
    border-radius: 5px;
    background-color: #7854f6;
  }
}
.white_address {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
