<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="学期计划" prop="tpid">
        <el-select v-model="queryParams.tpid" size="small">
          <el-option
            v-for="item in themePlanOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主题内容" prop="themeconent">
        <el-select v-model="queryParams.themeconent" size="small">
          <el-option
            v-for="item in themeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['benyi:themetermplan:add']"
          v-show="isShow"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['benyi:themetermplan:edit']"
          v-show="isShow"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['benyi:themetermplan:remove']"
          v-show="isShow"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="termplanitemList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <el-table-column label="学期计划" align="center" prop="tpid" :formatter="themePlanFormat" />
      <el-table-column label="月份" align="center" prop="month" />
      <el-table-column label="主题内容" align="center" prop="themeconent" :formatter="themeFormat" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['benyi:themetermplan:edit']"
            v-show="isShow"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['benyi:themetermplan:remove']"
            v-show="isShow"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改主题整合学期计划明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属计划" prop="tpid">
          <el-select v-model="form.tpid" size="small" :disabled="true">
            <el-option
              v-for="item in themePlanOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="月份" prop="month">
          <el-date-picker
            v-model="form.month"
            type="month"
            placeholder="请选择月"
            value-format="yyyy-MM"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="选择主题" prop="themeconent">
          <el-checkbox-group v-model="themeList" :max="max" @change="getThemeconentValue">
            <el-checkbox v-for="(item,i) in themeOptions" :label="item.id" :key="i">{{item.name}}</el-checkbox>
          </el-checkbox-group>
          <el-input v-model="form.themeconent" v-if="false" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTermplanitem,
  getTermplanitem,
  delTermplanitem,
  addTermplanitem,
  updateTermplanitem,
  exportTermplanitem,
} from "@/api/benyi/themetermplanitem";

import { listTermplan, getTermplan } from "@/api/benyi/themetermplan";
import { listTheme } from "@/api/benyi/theme";

export default {
  name: "Termplanitem",
  data() {
    return {
      isShow: true,
      //最大选中个数
      max: 2,
      //主题
      themeOptions: [],
      themeList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 主题整合学期计划明细表格数据
      termplanitemList: [],
      //计划列表
      themePlanOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultThemeType: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tpid: undefined,
        themeconent: undefined,
        createuserid: undefined,
        updateuserid: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        month: [{ required: true, message: "月份不能为空", trigger: "blur" }],
        themeconent: [
          { required: true, message: "主题不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    const themeplanid = this.$route.params && this.$route.params.id;
    //console.log(themeplanid);
    this.getThemePlan(themeplanid);
    this.getThemePlanList();
    this.getThemeList();
  },
  methods: {
    // 主题--字典状态字典翻译
    themeFormat(row, column) {
      if (row.themeconent != null) {
        var ilength = row.themeconent.split(";").length - 1;
        var names = "";
        for (var i = 1; i < ilength; i++) {
          names =
            names +
            this.selectMoeDictLabel(
              this.themeOptions,
              row.themeconent.split(";")[i]
            ) +
            " ";
        }
        //this.selectDictLabel(this.scopeOptions, row.xnxq);
        return names;
      }
      return "";
    },
    //获取选中的checkbox
    getThemeconentValue() {
      //console.log(this.themeList);
      var text = ";";
      this.themeList.forEach(function (value, key, arr) {
        //console.log(value); // 结果依次为1，2，3
        text = text + value + ";";
        //console.log(text);
      });
      this.form.themeconent = text;
    },
    //主题
    getThemeList() {
      listTheme(null).then((response) => {
        //console.log(response.rows);
        this.themeOptions = response.rows;
      });
    },
    // 字典翻译
    themePlanFormat(row, column) {
      // return this.selectDictLabel(this.classOptions, row.classid);
      var actions = [];
      var datas = this.themePlanOptions;
      Object.keys(datas).map((key) => {
        if (datas[key].id == "" + row.tpid) {
          actions.push(datas[key].name);
          return false;
        }
      });
      return actions.join("");
    },
    //计划详情
    getThemePlan(themeplanid) {
      getTermplan(themeplanid).then((response) => {
        this.queryParams.tpid = response.data.id;
        this.defaultThemeType = response.data.id;

        if (response.data.status == "0") {
          this.isShow = true;
        } else {
          this.isShow = false;
        }

        this.getList();
      });
    },
    getThemePlanList() {
      listTermplan().then((response) => {
        this.themePlanOptions = response.rows;
      });
    },
    /** 查询主题整合学期计划明细列表 */
    getList() {
      this.loading = true;
      listTermplanitem(this.queryParams).then((response) => {
        this.termplanitemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        tpid: undefined,
        themeconent: undefined,
        remark: undefined,
        createuserid: undefined,
        month: undefined,
        updateuserid: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
      this.themeList = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.tpid = this.defaultThemeType;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加主题整合学期计划明细";
      this.form.tpid = this.queryParams.tpid;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTermplanitem(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改主题整合学期计划明细";
        var themeconent = response.data.themeconent.split(";");
        var array = [];
        //console.log(arr);
        themeconent.forEach(function (value, key, arr) {
          //console.log(value); // 结果依次为1，2，3
          if (value != "") {
            array.push(parseInt(value));
          }
        });
        this.themeList = array;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateTermplanitem(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addTermplanitem(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除主题整合学期计划明细编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delTermplanitem(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
  },
};
</script>
