<template>
  <div class="hcategory">
    <div class="head-title">
      分类管理
      <el-button style="float:right;margin-top: -5px;" type="primary" class="el-button--small"
        @click="showCategoryForm(null)">新增</el-button>
    </div>
    <div class="content">
      <div style="margin-bottom:20px">
        <span>分类名称</span>
        <el-input type="text" style="width:250px" size="small" maxlength="100" v-model="msg.Name"
          @input="(msg.pageIndex = 1), getList()" clearable>
        </el-input>
        <el-button :disabled="iscx" @click="(msg.pageIndex = 1), getList()" size="small" type="primary">
          查询
        </el-button>
      </div>
      <el-table :data="tableData" v-loading="loading" border style="width: 100%">
        <el-table-column prop="Id" label="编号" width="150"> </el-table-column>
        <el-table-column prop="Name" label="名称"></el-table-column>
        <el-table-column label="Logo" width="300px">
          <template slot-scope="scope">
            <img :src="scope.row.Image" style="width:35px;height:35px;" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="启用状态" width="300px">
          <template slot-scope="scope">
            {{ scope.row.Enable == 1 ? "启用" : "禁用" }}
          </template>
        </el-table-column>
        <el-table-column prop="Sort" label="排序"> </el-table-column>
        <el-table-column label="操作" width="250px">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top">
              <img class="edit-img" src="../../assets/img/userman/edit.png" @click="showCategoryForm(scope.row)" />
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top">
              <img src="../../assets/img/userman/del.png" @click="RemmoveCategory(scope.row)" />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination style="text-align:right" background @current-change="handleCurrentChange" :page-size="msg.pageSize"
        :current-page.sync="msg.pageIndex" layout="prev, pager, next" :total="total">
      </el-pagination>
    </div>
    <!-- 修改及新增分类 -->
    <el-dialog :title="editTitle" :visible.sync="isShowCategoryForm" width="500px">
      <el-form ref="editform" :model="postMsg" :rules="rules" label-width="150px" @validate="formTest">
        <el-form-item label="分类名称" prop="Name">
          <el-input type="text" style="width:250px" size="small" maxlength="100" v-model="postMsg.Name">
          </el-input>
        </el-form-item>
        <el-form-item label="Logo" prop="Image">
          <el-input v-if="false" v-model="Image"></el-input>
          <div>
            <el-button @click="openChangeDig()" size="small">选择文件</el-button>
          </div>
          <div>
            <img v-if="postMsg.Image" :src="postMsg.Image" alt="" style="width:100px;height:100px;" />
          </div>
        </el-form-item>
        <el-form-item label="启用状态">
          <el-radio v-model="postMsg.Enable" :label="1">启用</el-radio>
          <el-radio v-model="postMsg.Enable" :label="2">禁用</el-radio>
        </el-form-item>
        <el-form-item label="排序">
          <el-input style="width:250px" size="small" maxlength="200" v-model="postMsg.Sort">
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="isShowCategoryForm = false">取 消</el-button>
        <el-button size="small" type="primary" @click="saveData('editform')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 选择文件 -->
    <el-dialog title="选择文件" :visible.sync="isShowChooseImage" width="1240px">
      <ChooseImg @SelectId="SelectId"></ChooseImg>
    </el-dialog>
  </div>
</template>

<script>
import ChooseImg from "@/components/global/ChooseImg.vue";
export default {
  components: {
    ChooseImg
  },
  data() {
    return {
      loading: false,
      tableData: [],
      total: 0,
      msg: {
        pageIndex: 1,
        pageSize: 15,
        Name: ""
      },
      dateList: [], //日期
      editTitle: "新增分类分类",
      postMsg: {
        Id: 0,
        Name: "",
        Image: "",
        Enable: 1,
        Sort: 0
      },
      isShowCategoryForm: false,
      isShowChooseImage: false,
      rules: {
        Name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        Image: [{ required: true, message: "请选择文件" }]
      },

      // 验证是否通过
      pass: "",
      //节流字段
      iscx: ''
    };
  },
  created() { },
  mounted() {
    this.getList();
  },
  methods: {
    openChangeDig() {
      this.isShowChooseImage = true;
    },
    SelectId(msg) {
      let url = this.getIconLink(msg.url);
      this.postMsg.Image = url;
      this.isShowChooseImage = false;
    },
    //获取数据
    getList() {
      this.isShowCategoryForm = false;
      this.loading = true;
      this.assetsApipost("/api/Assess/GetCategoryPageList", this.msg, res => {
        this.loading = false;
        if (res.data.resultCode == 1) {
          this.total = res.data.data.count;
          this.tableData = res.data.data.pageData;
        } else {
          this.Error(res.data.message);
        }
      });
    },
    handleCurrentChange(val) {
      this.msg.pageIndex = val;
      this.getList();
    },
    //显示表单
    showCategoryForm(item) {
      if (item) {
        this.editTitle = "修改分类";
        let { Id, Name, Image, Enable, Sort } = item;
        this.postMsg = { Id, Name, Image, Enable, Sort };
      } else {
        this.editTitle = "新增分类";
        this.postMsg.Id = 0;
        this.postMsg.Name = "";
        this.postMsg.Image = "";
        this.postMsg.Enable = 1;
        this.postMsg.Sort = 0;
      }
      this.isShowCategoryForm = true;
    },
    //表单验证是否通过
    formTest(res, pass) {
      this.pass = pass;
    },
    //新增修改分类
    saveData(editform) {
      this.$refs[editform].validate((valid) => {
        if (valid && this.postMsg.Image) {
          this.assetsApipost("/api/Assess/SetCategoryInfo", this.postMsg, res => {
            if (res.data.resultCode == 1) {
              this.Success(res.data.message);
              this.getList();
            } else {
              this.Error(res.data.message);
            }
          });
        } else {
          this.$message.error('请填写完整信息');
          return false;
        }
      });
    },
    //删除分类
    RemmoveCategory(item) {
      let that = this;
      that.Confirm("是否删除此分类?", function () {
        that.apipost(
          "/api/Assess/DelCategoryInfo",
          {
            CategoryId: item.Id
          },
          res => {
            if (res.data.resultCode == 1) {
              that.Success(res.data.message);
              that.getList();
            } else {
              that.Error(res.data.message);
            }
          }
        );
      });
    }
  }
};
</script>
<style scoped>
.hcategory .content {
  background: #fff;
  margin-top: 10px;
  padding: 20px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

.hcategory .edit-img {
  margin-right: 10px;
}
</style>
