<template>
  <div class="main_root">
    <div class="search_header">
      <div class="search_left">
        <Input
          v-model="searchForm.visitName"
          placeholder="请输入名称"
          search
          @on-search="searchList()"
          style="width: 180px"
        />
      </div>
      <div class="search_right">
        <div class="download_btn">
          <Icon type="md-download" />
          <div>下载浏览器</div>
        </div>
        <Button type="primary" class="primary_btn" @click="toAdd()"
          >新建</Button
        >
      </div>
    </div>
    <div class="table_wrap">
      <Table :columns="columns" :data="searchForm.list">
        <template slot-scope="{ row }" slot="actionSlot">
          <div class="action_list">
            <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="browserInfo.show"
      :title="browserInfo.browserid ? '编辑' : '新建'"
    >
      <Form
        v-if="browserInfo.show"
        ref="addForm"
        :model="browserInfo"
        :rules="rules"
        :label-width="60"
      >
        <FormItem label="名称" prop="visitName">
          <Input
            v-model="browserInfo.visitName"
            placeholder="请输入名称"
          ></Input>
        </FormItem>
        <FormItem label="网址" prop="visitUrl">
          <Input
            v-model="browserInfo.visitUrl"
            placeholder="请输入网址"
          ></Input>
          <div style="color: #b50000">
            请填写正确的网址，例如:https://www.baidu.com/m/a.htm。
          </div>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button @click="browserInfo.show = false">取消</Button>
        <Button @click="saveAddInfo()" type="primary">保存</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import {
  browser_list,
  browser_add,
  browser_edit,
  browser_delete
} from "@/api/browser";
export default {
  data() {
    return {
      searchForm: {
        visitName: "",
        page: 1,
        size: 10,
        list: [],
        total: 0
      },
      columns: [
        {
          title: "序号",
          align: "center",
          width: 70,
          render: (h, params) => {
            return h(
              "span",
              params.index +
                (this.searchForm.page - 1) * this.searchForm.size +
                1
            );
          }
        },
        {
          title: "名称",
          key: "visitName",
          align: "center"
        },
        {
          title: "网址",
          key: "visitUrl",
          align: "center"
        },
        {
          title: "操作人",
          key: "updatename",
          align: "center"
        },
        {
          title: "操作时间",
          key: "updatetime",
          width: 190,
          align: "center"
        },
        {
          title: "操作",
          slot: "actionSlot",
          width: 140,
          align: "center"
        }
      ],
      browserInfo: {
        show: false,
        browserid: null,
        rversion: null,
        schoolid: null,
        visitName: null,
        visitUrl: null
      },
      rules: {
        visitName: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur"
          }
        ],
        visitUrl: [
          {
            required: true,
            message: "请输入网址",
            trigger: "blur"
          }
        ]
      }
    };
  },
  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---schoolid->");
      browser_list({
        schoolid: 4,
        page: this.searchForm.page,
        size: this.searchForm.size,
        visitName: this.searchForm.visitName
      }).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---schoolid->");
      this.browserInfo = {
        show: true,
        browserid: null,
        rversion: null,
        schoolid: 4,
        visitName: null,
        visitUrl: null
      };
    },
    saveAddInfo() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.browserInfo.show = false;
          let api = this.browserInfo.browserid ? browser_edit : browser_add;
          let form = {
            schoolid: this.browserInfo.schoolid,
            visitName: this.browserInfo.visitName,
            visitUrl: this.browserInfo.visitUrl
          };
          if (this.browserInfo.browserid) {
            form.browserid = this.browserInfo.browserid;
            form.rversion = this.browserInfo.rversion;
          }
          api(form).then((data) => {
            if (data.code === 0) {
              this.searchList();
              this.$Message.success(data.msg);
            } else {
              this.$Message.error(data.msg);
            }
          });
        }
      });
    },
    toEdit(row) {
      console.warn("---注意替换regionid---schoolid->");
      this.browserInfo = {
        show: true,
        browserid: row.browserid,
        rversion: row.rversion,
        schoolid: 4,
        visitName: row.visitName,
        visitUrl: row.visitUrl
      };
    },
    toDel(row) {
      this.$Modal.confirm({
        title: "提示",
        content: "您确定删除选中数据吗？",
        onOk: () => {
          browser_delete({
            browserid: row.browserid,
            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;
  }
  .search_right {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .download_btn {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      margin-right: 16px;
      color: #95abdf;
      cursor: pointer;
      &:hover {
        color: #339dff;
      }
    }
  }
}
</style>
