<template>
  <div class="main_root">
    <div class="search_header">
      <div class="search_left">
        <Input
          v-model="searchForm.name"
          placeholder="请输入名称"
          search
          @on-search="searchList()"
          style="width: 150px"
        />
      </div>
      <div class="search_right">
        <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="enabledSlot">
          <i-switch
            size="large"
            v-model="row.enabled"
            :true-value="1"
            :false-value="2"
            @on-change="enabledChange(row)"
          >
            <span slot="open">启用</span>
            <span slot="close">禁用</span>
          </i-switch>
        </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 @click="toEmpower(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="empowerInfo.show"
      title="授权"
    >
      <div class="strategy_name">{{ empowerInfo.name }}</div>
      <div class="school_name">
        <Checkbox v-model="empowerInfo.school">{{
          schoolInfo.schoolname
        }}</Checkbox>
      </div>
      <CheckboxGroup v-model="empowerInfo.classids">
        <Checkbox
          :label="classItem.id"
          v-for="classItem in schoolInfo.classList"
          :key="classItem.id"
          >{{ classItem.name }}</Checkbox
        >
      </CheckboxGroup>
      <div slot="footer">
        <Button @click="empowerInfo.show = false">取消</Button>
        <Button @click="saveEmpowerInfo()" type="primary" class="primary_btn"
          >保存</Button
        >
      </div>
    </Modal>
  </div>
</template>

<script>
import {
  stApp_list,
  stApp_enable,
  stApp_disabled,
  stApp_delete,
  stApp_add_empower
} from "@/api/stPad";
import { class_list } from "@/api/school";
export default {
  data() {
    return {
      searchForm: {
        name: "",
        page: 1,
        size: 10,
        list: [],
        total: 0
      },
      empowerInfo: {
        show: false,
        name: "",
        stappid: null,
        school: false,
        classids: []
      },
      schoolInfo: {
        schoolid: null,
        schoolname: null,
        classList: []
      },
      columns: [
        {
          title: "序号",
          align: "center",
          width: 70,
          render: (h, params) => {
            return h(
              "span",
              params.index +
                (this.searchForm.page - 1) * this.searchForm.size +
                1
            );
          }
        },
        {
          title: "名称",
          minWidth: 100,
          key: "name",
          align: "center"
        },
        {
          title: "状态",
          width: 90,
          slot: "enabledSlot",
          align: "center"
        },
        {
          title: "操作人",
          minWidth: 100,
          key: "updatename",
          align: "center"
        },
        {
          title: "操作时间",
          key: "updatetime",
          width: 190,
          align: "center"
        },
        {
          title: "操作",
          slot: "actionSlot",
          width: 220,
          align: "center"
        }
      ]
    };
  },
  created() {
    this.searchList();
  },
  computed: {
    powerParams() {
      return this.$store.getters.powerParams;
    }
  },
  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() {
      stApp_list({
        rtype: this.powerParams.rtype,
        objectid: this.powerParams.objectid,
        page: this.searchForm.page,
        size: this.searchForm.size,
        schoolid: this.powerParams.objectid,
        name: this.searchForm.name
      }).then((data) => {
        if (data.code === 0) {
          this.searchForm.list = data.obj.data;
          this.searchForm.total = data.obj.total;
        } else {
          this.$Message.error(data.msg);
        }
      });
    },
    enabledChange(row) {
      let api = row.enabled === 1 ? stApp_enable : stApp_disabled;
      api({
        rtype: this.powerParams.rtype,
        objectid: this.powerParams.objectid,
        stappid: row.stappid,
        rversion: row.rversion
      }).then((data) => {
        this.getList();
        if (data.code === 0) {
          this.$Message.success(data.msg);
        } else {
          this.$Message.error(data.msg);
        }
      });
    },
    // 新建
    toAdd() {
      this.$router.push("/school/appStrategyTemplateAdd");
    },
    // 查看
    toView(row) {
      this.$router.push({
        path: "/school/appStrategyTemplateView",
        query: {
          stappid: row.stappid
        }
      });
    },
    // 编辑
    toEdit(row) {
      this.$router.push({
        path: "/school/appStrategyTemplateEdit",
        query: {
          stappid: row.stappid
        }
      });
    },
    // 删除
    toDel(row) {
      this.$Modal.confirm({
        title: "提示",
        content: "您确定删除选中数据吗？",
        onOk: () => {
          stApp_delete({
            rtype: this.powerParams.rtype,
            objectid: this.powerParams.objectid,
            stappid: row.stappid,
            rversion: row.rversion
          }).then((data) => {
            if (data.code === 0) {
              this.getList();
              this.$Message.success(data.msg);
            } else {
              this.$Message.error(data.msg);
            }
          });
        },
        onCancel: () => {}
      });
    },
    // 授权
    toEmpower(row) {
      this.empowerInfo = {
        show: true,
        name: row.name,
        stappid: row.stappid,
        school: false,
        classids: []
      };
      this.getClassList();
    },
    // 保存授权
    saveEmpowerInfo() {
      if (this.empowerInfo.classids.length === 0 && !this.empowerInfo.school) {
        this.$Message.error("请选择班级");
        return;
      }
      let form = {
        // rtype: this.powerParams.rtype,
        // objectid: this.powerParams.objectid,
        // schoolid: this.powerParams.objectid,
        stappid: this.empowerInfo.stappid,
        classids: this.empowerInfo.classids
      };
      if (this.empowerInfo.school) {
        form.schoolid = this.powerParams.objectid;
      }
      stApp_add_empower(form).then((data) => {
        if (data.code === 0) {
          this.empowerInfo.show = false;
          this.$Message.success(data.msg);
        } else {
          this.$Message.error(data.msg);
        }
      });
    },
    // 获取班级列表
    getClassList() {
      class_list({
        rtype: this.powerParams.rtype,
        objectid: this.powerParams.objectid,
        schoolid: this.powerParams.objectid
      }).then((data) => {
        if (data.code === 0) {
          this.schoolInfo = {
            schoolid: data.obj.id,
            schoolname: data.obj.name,
            classList: data.obj.children
          };
        } else {
          this.$Message.error(data.msg);
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.main_root {
  // display: flex;
  // justify-content: space-between;
  // align-items: center;
  // height: calc(100% - 10px);
  // border-radius: 0;
  // border: none;
  // background-color: transparent;
  .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;
    }
  }
}
.strategy_name {
  margin-bottom: 16px;
  font-size: 18px;
  text-align: center;
  font-weight: bold;
}
.school_name {
  margin-bottom: 16px;
  font-size: 16px;
  font-weight: bold;
}
</style>
