<template>
  <div>
    <el-form :inline="true" :model="firstForm">
      <el-form-item class="mr10" label="德育维度：">
        <el-select v-model="firstForm.dywd" placeholder="请选择维度" @change="getListData">
          <el-option value="">全部</el-option>
          <el-option v-for="item in wdList" :label="item.xdww_name" :value="item.xdww_id" :key="item.xdww_id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="mr10" label="使用状态：">
        <el-select v-model="firstForm.syzt" placeholder="请选择状态" @change="getListData">
          <el-option value="">全部</el-option>
          <el-option v-for="item in ztList" :label="item.label" :value="item.value" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="mr10" label="使用学生：">
        <el-input v-model="firstForm.syxs" placeholder="输入学生"></el-input>
      </el-form-item>
      <el-button type="primary" @click="getListData">搜索</el-button>
    </el-form>
    <el-button type="primary" @click="handleAdd">添加</el-button>
    <el-button type="primary" @click="exportAll" :disabled="tableData.length === 0">导出</el-button>
    <el-button type="danger" @click="delData" :disabled="multipleSelection.length === 0">删除</el-button>
    <el-table :data="tableData" tooltip-effect="dark" class="mt20" @selection-change="handleSelectionChange"
      style="width: 100%">
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>

      <el-table-column label="德育维度" prop="xdww_name" width="150">
      </el-table-column>
      <el-table-column label="使用状态" prop="xdwd_syzt" width="100">
      </el-table-column>
      <el-table-column label="使用学生" prop="xdwd_syxs" width="100">
      </el-table-column>
      <el-table-column label="使用时间" prop="xdwd_sysj" align="center" width="150">
      </el-table-column>
      <el-table-column label="二维码">
        <template slot-scope="scope">
          <h3 class="qr-code hand" @click="showQr(scope.row)"></h3>
        </template>
      </el-table-column>
    </el-table>
    <footer class="flex-item-none" style="display: flex; justify-content: flex-end; margin-top: 30px">
      <el-pagination background layout="total,prev, pager, next" :page-size="limit" :total="total"
        :current-page.sync="cur_page" @current-change="handleCurrentChange"></el-pagination>
    </footer>
    <el-dialog title="二维码详情" :visible.sync="dialogVisible" width="300px">
      <div class="text-center">
        <vue-qr :size="200" :margin="0" :auto-color="true" :dot-scale="1" :text="appSrc" colorDark="white"
          colorLight="black" />
      </div>

      <div slot="footer" class="text-center">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="添加" :visible.sync="addVisible" width="500px">
      <el-form :model="addForm" ref="addForm" :rules="rules">
        <el-form-item label="德育维度" prop="dywd">
          <el-cascader ref="cascader" :props="props" :options="options" :show-all-levels="false" @change="handleChange"
            v-model="addForm.dywd" placeholder="请选择德育维度"></el-cascader>
        </el-form-item>
        <el-form-item label="使用学生" prop="xs">
          <el-select v-model="addForm.xs" filterable reserve-keyword placeholder="请输入学生姓名" remote clearable
            :remote-method="remoteMethod" :loading="xsLoading">
            <el-option v-for="item in xsList" :key="item.xdx_id" :label="item.xdx_xsxm" :value="item.xdx_xsxm">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="text-center">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button :disabled="isButton" type="primary" @click="formSubmit('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import VueQr from "vue-qr";
import request from '@/utils/request';
import { dyewm_list, dyewm_export, wdsz_list, xjk_list, dyewm_del } from "./api";
import { useUserStore } from "@/stores/user";
import { wdpfx_list } from '../wdsz/wdpfx/api';
const { token } = useUserStore();
export default {
  name: "index",
  components: {
    VueQr,
  },
  data() {
    return {
      xsLoading: false,
      limit: 10,
      total: 12,
      cur_page: 1,
      keyword: "",
      tableData: [],
      firstForm: {
        dywd: "",
        syzt: "",
        time: "",
        syxs: "",
      },
      wdList: [],
      ztList: [
        {
          value: "1",
          label: "已使用",
        },
        { value: "0", label: "未使用" },
      ],
      loading: false,
      logoSrc: "",
      appSrc: "",
      dialogVisible: false,
      multipleSelection: [],
      addVisible: false,
      xsList: [],
      addForm: {
        dywd: '',
        xs: '',
      },
      xdwwp_name: '',
      options: [],
      props: {
        children: 'children'
      },
      xsData: [],
      rules: {
        dywd: [
          { required: true, message: "请选择评分项", trigger: "submit" },
        ],
        xs: [
          { required: true, message: "请输入学生姓名", trigger: "submit" },
        ],
      },
      isButton: false,
    };
  },
  methods: {
    getWdList() {
      this.loading = true;
      let data = {
        page: 1,
        limit: 999,
      };
      wdsz_list(data).then((res) => {
        this.wdList = res.data.page_data;
        this.wdList.forEach(item => {
          let obj = {
            value: item.xdww_id,
            label: item.xdww_name,
            children: [],
          }
          let arr2 = [];
          item?.xddy_dygl_wypj_wdpfx.forEach(key => {
            arr2.push({
              value: key.xdwwp_id,
              label: key.xdwwp_name,
            });
          })
          obj.children = arr2;
          this.options.push(obj);
        })
      });
    },
    remoteMethod(query) {
      if (query !== '') {
        this.xsLoading = true;
        xjk_list({ limit: 999, page: 1, keyword: query }).then(res => {
          if (res.code == '1') {
            this.xsData = res.data.page_data;
            this.xsLoading = false;
            this.xsList = this.xsData.filter(item => {
              return item.xdx_xsxm.toLowerCase()
                .indexOf(query.toLowerCase()) > -1;
            });
          }
        })

      } else {
        this.xsList = [];
      }
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    getListData() {
      this.loading = true;
      let data = {
        keyword: this.firstForm.syxs,
        xdww_id: this.firstForm.dywd,
        xdwd_syzt: this.firstForm.syzt,
        page: this.cur_page,
        limit: this.limit,
      };

      dyewm_list(data).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },
    handleAdd() {
      this.isButton = false;
      this.addVisible = true;
      this.addForm = {
        dywd: '',
        xs: '',
      }
      this.xsList = [];
    },
    handleChange() {
      let nodesInfo = this.$refs['cascader'].getCheckedNodes()[0];
      this.xdwwp_name = nodesInfo.label;
      this.addForm.dywd = nodesInfo.value;
    },
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isButton = true;
          let data = {
            xddy_dygl_wypj_dyewm: {
              xdwd_syxs: this.addForm.xs,
              xdwwp_id: this.addForm.dywd,
              xdwwp_name: this.xdwwp_name,
            }
          };
          request({
            url: '/xddy/dygl_wypj_dyewm/add',
            data,
          }).then(res => {
            if (res.code == 1) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.isButton = true;
              this.addVisible = false;
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          })
        } else {
          this.isButton = false
        }
      })
    },
    delData() {
      this.$confirm("确认删除这些信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          dyewm_del({ xdwd_id: this.multipleSelection.join(',') }).then(res => {
            if (res.code == '1') {
              this.$message({
                message: "删除成功！",
                type: "success",
              });
              this.getListData();
            }
          })
        }).catch(() => { });
    },
    exportAll() {
      let link = "";
      if (this.multipleSelection.length === 0) {
        link =
          window.globalVariables.api +
          "/xddy/dygl_wypj_dyewm/index?api=xls&page=1&limit=10000&token=" +
          token;
      } else {
        link =
          window.globalVariables.api +
          "/xddy/dygl_wypj_dyewm/index?api=xls&page=1&limit=10000&token=" +
          token +
          "&xdwd_id=" +
          this.multipleSelection.join(",");
      }
      window.open(link, "_blank");
    },
    showQr(item) {
      this.appSrc =
        "dy," + item.xdww_id + "," + item.xdwx_xsxh + "," + item.dept_id;
      this.dialogVisible = true;
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].xdwd_id);
      }
    },
  },
  mounted() {
    this.getWdList();
    this.getListData();
  },
};
</script>

<style scoped lang="scss">
.qr-code {
  width: 28px;
  height: 28px;
  background: url("@/assets/images/a-2.png") center no-repeat;
  background-size: 28px 28px;
}
</style>