<template>
  <div class="main_root">
    <div class="search_header">
      <div class="search_left">
        <Input
          v-model="searchForm.appgroupName"
          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="iconPath">
          <div class="iconPath" v-if="row.iconPath">
            <img :src="$api.showImageUrl + row.iconPath" />
          </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 application"
      :mask-closable="false"
      v-model="wallpaperInfo.show"
      :title="wallpaperInfo.appgroupid ? '编辑' : '新建'"
    >
      <Form
        ref="addForm"
        :model="wallpaperInfo"
        :rules="rules"
        :label-width="100"
        inline
      >
        <FormItem label="名称" prop="appgroupName" style="width: 100%">
          <Input
            v-model="wallpaperInfo.appgroupName"
            placeholder="请输入名称"
          ></Input>
        </FormItem>
        <FormItem label="图标" style="width: 100%" class="require">
          <div class="logo">
            <Upload
              action
              :show-upload-list="false"
              :before-upload="
                (file) => {
                  upLoadUserpic(file);
                  return false;
                }
              "
              accept=".png,.jpeg,.jpg"
            >
              <Button type="primary" class="primary_btn">上传图标</Button>
            </Upload>
            <span>支持图片格式：jpg、jgeg、png</span>
          </div>
          <div class="app_img">
            <img
              v-if="wallpaperInfo.iconPath"
              :src="$api.showImageUrl + wallpaperInfo.iconPath"
            />
          </div>
        </FormItem>
        <FormItem label="排序" prop="appgroupOrder">
          <InputNumber
            placeholder="请输入排序"
            v-model="wallpaperInfo.appgroupOrder"
            :min="1"
          />
        </FormItem>
        <FormItem label="描述信息" style="width: 100%">
          <Input
            v-model="wallpaperInfo.comm"
            type="textarea"
            :rows="4"
            placeholder="请输入描述信息"
          ></Input>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button @click="wallpaperInfo.show = false">取消</Button>
        <Button @click="saveAddInfo()" class="primary_btn" type="primary"
          >保存</Button
        >
      </div>
    </Modal>
    <!-- 查看 -->
    <Modal
      class="modal1"
      :mask-closable="false"
      v-model="viewInfo.show"
      title="查看"
    >
      <div class="view_title">河南星火燎原软件科技有限公司</div>
      <Table
        style="border: 1px solid #e8eaec; border-bottom: none"
        :columns="viewInfo.columns"
        :data="viewInfo.list"
      >
      </Table>
      <div slot="footer" style="text-align: center">
        <Button @click="viewInfo.show = false">关闭</Button>
      </div>
    </Modal>
    <Spin fix v-if="showLoading" style="background-color: transparent">
      <Icon type="ios-loading" size="18" class="demo-spin-icon-load"></Icon>
      <div>加载中</div>
    </Spin>
  </div>
</template>

<script>
import { date_format } from "@/utils";
import axios from "axios";
import {
  appgroup_add,
  appgroup_delete,
  appgroup_edit,
  appgroup_list
} from "@/api/appgroup";
export default {
  data() {
    return {
      date_format,
      showLoading: false,
      searchForm: {
        appgroupName: "",
        page: 1,
        size: 10,
        list: [],
        total: 0
      },
      // 新建/编辑
      wallpaperInfo: {
        show: false,
        appgroupid: null,
        appgroupName: "",
        appgroupOrder: 1,
        iconPath: "",
        comm: ""
      },
      // 查看
      viewInfo: {
        show: false,
        areaname: "",
        list: [],
        columns: [
          {
            title: "序号",
            type: "index",
            width: 70,
            align: "center"
          },
          {
            title: "名称",
            key: "admin",
            align: "center"
          },
          {
            title: "账号",
            key: "loginname",
            align: "center"
          },
          {
            title: "手机号码",
            key: "phone",
            align: "center"
          }
        ]
      },
      rules: {
        appgroupName: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur"
          }
        ]
      },
      columns: [
        {
          title: "序号",
          align: "center",
          width: 70,
          render: (h, params) => {
            return h(
              "span",
              params.index +
                (this.searchForm.page - 1) * this.searchForm.size +
                1
            );
          }
        },
        {
          title: "名称",
          key: "appgroupName",
          align: "center"
        },
        {
          title: "图标",
          slot: "iconPath",
          width: 190,
          align: "center"
        },
        {
          title: "创建人",
          key: "createname",
          width: 140,
          align: "center"
        },
        {
          title: "创建时间",
          key: "createtime",
          width: 190,
          align: "center"
        },
        {
          title: "操作人",
          key: "updatename",
          width: 140,
          align: "center"
        },
        {
          title: "操作时间",
          key: "updatetime",
          width: 190,
          align: "center"
        },
        {
          title: "操作",
          slot: "actionSlot",
          width: 220,
          align: "center"
        }
      ]
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    upLoadUserpic(file) {
      let str = file.name.split(".");
      let suffix = str[str.length - 1];
      suffix = suffix.toLowerCase();
      if (suffix !== "png" && suffix !== "jpeg" && suffix !== "jpg") {
        this.$Message.warning("请上传png、jpeg、jpg类型的文件");
        return false;
      }
      let formData = new FormData();
      formData.append("file", file);
      formData.append("savefolder", "app");
      formData.append("isrename", "1");
      let Url = this.$api.baseImageUrl + "up/upfile";
      this.showLoading = true;
      axios
        .post(Url, formData, {
          timeout: 1000 * this.$api.axiosFileTimeout
        })
        .then((res) => {
          this.showLoading = false;
          if (res.data.code === 0) {
            this.wallpaperInfo.iconPath = res.data.obj;
            this.$Message.success(res.data.msg);
          } else {
            this.$Message.error(res.data.msg);
          }
        })
        .catch(() => {
          this.showLoading = false;
          this.$Message.error("图片上传失败");
        });
    },
    // 区域选择改变
    searchAreaChange() {},
    // 搜索
    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() {
      appgroup_list({
        page: this.searchForm.page,
        size: this.searchForm.size,
        appgroupName: this.searchForm.appgroupName
      }).then((res) => {
        if (res.code === 0) {
          this.searchForm.list = res.obj.data;
          this.searchForm.total = res.obj.total;
        } else {
          this.$Message.error(res.msg);
        }
      });
    },
    // 新建
    toAdd() {
      this.wallpaperInfo = {
        show: true,
        appgroupid: null,
        appgroupName: "",
        iconPath: "",
        appgroupOrder: Number(this.searchForm.total) + 1,
        comm: ""
      };
      this.$refs.addForm.resetFields();
    },
    // 保存新建
    saveAddInfo() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          if (!this.wallpaperInfo.iconPath) {
            this.$Message.error("请上传图标!");
            return;
          }
          this.wallpaperInfo.show = false;
          let api = this.wallpaperInfo.appgroupid
            ? appgroup_edit
            : appgroup_add;
          let form = {
            appgroupName: this.wallpaperInfo.appgroupName,
            iconPath: this.wallpaperInfo.iconPath,
            comm: this.wallpaperInfo.comm,
            appgroupOrder: this.wallpaperInfo.appgroupOrder
          };
          //编辑
          if (this.wallpaperInfo.appgroupid) {
            form.appgroupid = this.wallpaperInfo.appgroupid;
            form.rversion = this.wallpaperInfo.rversion;
          }
          api(form).then((res) => {
            if (res.code === 0) {
              this.$Message.success(res.msg);
              this.searchList();
            } else {
              this.$Message.error(res.msg);
            }
          });
        }
      });
    },
    // 查看
    toView() {
      this.viewInfo.show = true;
      this.viewInfo.areaname = "河南星火燎原软件科技有限公司";
      this.viewInfo.list = [
        {
          admin: "周文彦",
          loginname: "zhouwenyan@xhkjedu.com",
          phone: "15610230213"
        },
        {
          admin: "齐思宇",
          loginname: "zhouwenyan@xhkjedu.com",
          phone: "15610230213"
        }
      ];
    },
    // 编辑
    toEdit(row) {
      this.$refs.addForm.resetFields();
      this.wallpaperInfo = {
        show: true,
        appgroupName: row.appgroupName,
        iconPath: row.iconPath,
        comm: row.comm,
        appgroupid: row.appgroupid,
        rversion: row.rversion,
        appgroupOrder: row.appgroupOrder
      };
    },
    // 删除
    toDel(row) {
      this.$Modal.confirm({
        title: "提示",
        content: "您确定删除选中数据吗？",
        onOk: () => {
          appgroup_delete({
            appgroupid: row.appgroupid,
            rversion: row.rversion
          }).then((res) => {
            if (res.code === 0) {
              this.$Message.success(res.msg);
              this.searchList();
            } else {
              this.$Message.error(res.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_drop {
      margin-right: 10px;
      width: 150px;
    }
  }
}
.code_btn {
  display: inline-block;
  margin-left: 10px;
  padding: 0 10px;
  color: #339dff;
  line-height: 30px;
  border: 1px solid #339dff;
  border-radius: 6px;
  background-color: #fff;
  cursor: pointer;
  &.disabled {
    color: #c5c8ce;
    background-color: #f7f7f7;
    border-color: #dcdee2;
    cursor: not-allowed;
  }
}
.view_title {
  margin-bottom: 16px;
  font-size: 18px;
  font-weight: bold;
  text-align: center;
}
.application {
  .logo {
    height: 34px;
    display: flex;
    align-items: center;
    span {
      font-size: 16px;
      margin-left: 10px;
      color: #7c8db5;
    }
  }
  .app_img {
    margin-top: 10px;
    width: 90px;
    height: 90px;
    border-radius: 20px;
    opacity: 1;
    border: 1px solid #e5e5e5;
    background: #ffffff;
    overflow: hidden;
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
}
.table_wrap {
  .iconPath {
    width: 60px;
    height: 60px;
    border-radius: 20px;
    border: 1px solid #e5e5e5;
    margin: 10px auto;
    text-align: center;
    padding: 10px;
    img {
      width: 40px;
      height: 40px;
      object-fit: cover;
    }
  }
}
</style>
