<template>
  <div class="main_root">
    <div class="search_header">
      <Input
        v-model="searchForm.regionName"
        placeholder="请输入壁纸名称"
        search
        @on-search="searchList()"
        style="width: 150px"
      />
      <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="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="wallpaperInfo.show"
      :title="wallpaperInfo.id ? '编辑' : '新建'"
    >
      <Form
        v-if="wallpaperInfo.show"
        ref="addForm"
        :model="wallpaperInfo"
        :rules="rules"
        :label-width="100"
        inline
      >
        <FormItem label="名称" prop="Name" style="width: 100%">
          <Input
            v-model="wallpaperInfo.name"
            placeholder="请输入壁纸名称"
          ></Input>
        </FormItem>
        <FormItem label="壁纸" prop="img" style="width: 100%">
          <div class="up">
            <Upload
              class="Upload"
              action
              :show-upload-list="false"
              :before-upload="
                (file) => {
                  upLoad(file);
                  return false;
                }
              "
              accept=".png,.jpeg,.jpg,gif"
            >
              <div class="icon">横屏壁纸</div>
            </Upload>
            <span>支持图片格式：jpg、jgeg、gif、png</span>
          </div>
        </FormItem>
        <FormItem label="适配类型" style="width: 100%"> </FormItem>
      </Form>
      <div slot="footer">
        <div>
          <Button @click="wallpaperInfo.show = false">取消</Button>
          <Button @click="saveAddInfo()" class="primary_btn" type="primary"
            >保存</Button
          >
        </div>
      </div>
    </Modal>
    <!-- 查看 -->
    <Modal
      class="modal1"
      :mask-closable="false"
      v-model="viewInfo.show"
      title="查看"
    >
      <div class="view_title">{{ viewInfo.regionName }}</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>
  </div>
</template>

<script>
import { generateRandomString } from "@/utils";
import {
  region_list_admin,
  region_delete,
  region_add,
  region_edit,
  region_list_qy
} from "@/api/region";
import { login_msg_code } from "@/api/login";
export default {
  data() {
    return {
      searchForm: {
        regionName: "",
        page: 1,
        size: 10,
        list: [],
        total: 0
      },
      // 新建
      wallpaperInfo: {
        show: true,
        id: null,
        name: "",
        img: ""
      },
      // 编辑
      modifyRegion: {
        show: false,
        regionid: null,
        regionName: "",
        regionCode: "",
        comm: "",
        rversion: null
      },
      // 查看
      viewInfo: {
        show: false,
        regionName: "",
        list: [],
        columns: [
          {
            title: "序号",
            type: "index",
            width: 70,
            align: "center"
          },
          {
            title: "名称",
            key: "aname",
            align: "center"
          },
          {
            title: "账号",
            key: "loginname",
            align: "center"
          },
          {
            title: "手机号码",
            key: "phone",
            align: "center"
          }
        ]
      },
      rules: {
        regionName: [
          {
            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: "regionName",
          align: "center"
        },
        {
          title: "壁纸名称",
          key: "createname",
          align: "center"
        },
        {
          title: "操作人",
          key: "createtime",
          align: "center"
        },
        {
          title: "操作时间",
          key: "updatetime",
          width: 190,
          align: "center"
        },
        {
          title: "操作",
          slot: "actionSlot",
          width: 220,
          align: "center"
        }
      ]
    };
  },
  created() {
    // this.searchList();
  },
  methods: {
    upLoad() {},
    // 搜索
    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() {
      region_list_qy({
        page: this.searchForm.page,
        size: this.searchForm.size,
        regionName: this.searchForm.regionName
      }).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.$refs.addForm.resetFields();
      this.wallpaperInfo = {
        show: true,
        // 验证码计时
        timer: null,
        name:"",
        img:""
      };

    },
    // 保存新建
    saveAddInfo() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.wallpaperInfo.show = false;
          region_add({
            regionName: this.wallpaperInfo.regionName,
            regionCode: this.wallpaperInfo.regionCode,
            admin: {
              aname: this.wallpaperInfo.admin.aname,
              loginname: this.wallpaperInfo.admin.loginname,
              loginpwd: this.wallpaperInfo.admin.loginpwd,
              phone: this.wallpaperInfo.admin.phone,
              msgcode: this.wallpaperInfo.admin.msgcode,
              address: this.wallpaperInfo.admin.address,
              comm: this.wallpaperInfo.admin.comm
            }
          }).then((data) => {
            if (data.code === 0) {
              this.searchList();
              this.$Message.success(data.msg);
            } else {
              this.$Message.error(data.msg);
            }
          });
        }
      });
    },
    // 保存编辑
    saveModifyInfo() {
      this.$refs.modifyForm.validate((valid) => {
        if (valid) {
          this.wallpaperInfo.show = false;
          region_edit({
            regionName: this.modifyRegion.regionName,
            regionCode: this.modifyRegion.regionCode,
            comm: this.modifyRegion.comm,
            regionid: this.modifyRegion.regionid,
            rversion: this.modifyRegion.rversion
          }).then((data) => {
            if (data.code === 0) {
              this.searchList();
              this.$Message.success(data.msg);
            } else {
              this.$Message.error(data.msg);
            }
          });
        }
      });
    },
    // 查看
    toView(row) {
      region_list_admin({
        regionid: row.regionid
      }).then((data) => {
        if (data.code === 0) {
          this.viewInfo.show = true;
          this.viewInfo.regionName = row.regionName;
          this.viewInfo.list = data.obj;
        } else {
          this.$Message.error(data.msg);
        }
      });
    },
    // 编辑
    toEdit(row) {
      this.$refs.addForm.resetFields();
      this.wallpaperInfo = {
        show: true,
        // 验证码计时
        timer: null,
        name:"",
        img:""
      };
    },
    // 删除
    toDel(row) {
      this.$Modal.confirm({
        title: "提示",
        content: "您确定删除选中数据吗？",
        onOk: () => {
          region_delete({
            regionid: row.regionid,
            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;
}
.view_title {
  margin-bottom: 16px;
  font-size: 18px;
  font-weight: bold;
  text-align: center;
}
.up {
  display: flex;
  .Upload {
    width: 180px;
    height: 120px;
    border-radius: 8px;
    border: 3px solid #333333;
    background-color: #f0f6fc;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    .icon {
      color: #b8c2d9;
    }
  }
  span {
    margin-left: 20px;
    color: #7c8db5;
    font-size: 16px;
  }
}
</style>
