<template>
  <div class="app-container">
    <el-row>
      <el-button @click="onAdd" type="primary">添加</el-button>
      <el-button @click="onExport" type="primary">导出</el-button>
      <el-button v-if="!editing" @click="onEdit" plain type="primary">编辑</el-button>
      <el-button v-if="editing" @click="onDeleteSelect" type="danger">删除</el-button>
      <el-button v-if="editing" @click="onCancel" plain>取消</el-button>
    </el-row>
    <el-table :data="list" border stripe highlight-current-row ref="table" class="mt10" @selection-change="onTableSelectionChange" @row-click="onRowClick">
      <el-table-column v-if="editing" type="selection" width="55" fixed></el-table-column>
      <el-table-column type="index" :index="indexCompute" label="序号" align="center" fixed="left" width="60px"/>
      <el-table-column prop="typeName" label="分类目录名称" align="center" min-width="200px"/>
      <el-table-column label="操作" align="center" width="180px" v-if="true">
         <template v-slot="scope">
          <el-link type="primary" icon="el-icon-edit" @click="onUpdate(scope.row)">编辑</el-link>
          <el-link type="danger" icon="el-icon-delete" @click="onDelete(scope.row)" class="ml20">删除</el-link>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="导出" :visible.sync="exportVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="600px">
      <el-progress :text-inside="true" :stroke-width="24" :percentage="exportProgress" status="success"></el-progress>
      <el-button slot="footer" @click="onCancelExport">取 消</el-button>
    </el-dialog>

    <dialog-edit v-if="editDialogVisible" :visible.sync="editDialogVisible" :data="selectItem" @add="onAddComplete" @update="onUpdateComplete"/>
  </div>
</template>

<script>
import DialogEdit from "./dialogEdit";

export default {
  name: "inspectionClassify",
  components: {DialogEdit},

  data() {
    return {
      shrinkable: false, // 查询条件是否可以展开

      list: [], // 结果列表
      total: 0, // 总结果数量

      exportVisible: false, // 是否显示导出对话框
      exportData: [], // 导出数据
      exportProgress: 0, // 导出进度
      exportCancel: false, // 是否取消导出

      editDialogVisible: false, // 编辑对话框是否可见

      editing: false, // 表格编辑中

      selectItem: undefined, // 选中的单条记录
      selectIds: undefined // 选中多条记录的id
    }
  },

  watch: {},

  // 页面创建
  created() {
    this.initData();
  },

  // 页面挂载
  mounted() {
    this.initQuery();
    this.getList();
    this.initView();
  },

  // keep-alive 显示界面时调用
  activated() {
    this.$refs["table"].doLayout();
  },

  // keep-alive 页面隐藏
  deactivated() {
  },

  methods: {
    // 初始化数据
    initData() {
    },

    // 初始化查询条件
    initQuery() {

    },

    // 初始化视图
    initView() {
    },

    // 表格索引
    indexCompute(index) {
      return index + 1;
    },

    // 添加
    onAdd() {
      this.onCancel();
      this.selectItem = undefined;
      this.editDialogVisible = true;
    },

    // 添加完成
    onAddComplete() {
      this.getList();
    },

    // 编辑
    onEdit() {
      this.editing = true;
    },

    // 删除选中
    onDeleteSelect() {
      this.$confirm('此操作将永久删除选中的记录, 是否继续?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.$axios.delete("/v1/check/item/type/delete", {params:{ids: this.selectIds}}).then(() => {
          this.getList();
        });
      }).catch(() => {
        this.$message.info("取消操作")
      });
    },

    // 取消编辑
    onCancel() {
      this.$refs['table'].clearSelection();
      this.editing = false;
    },

    // 更新
    onUpdate(item) {
      this.selectItem = item;
      this.editDialogVisible = true;
    },

    // 更新完成
    onUpdateComplete() {
      this.getList();
    },

    // 删除
    onDelete(item) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.$axios.delete(`/v1/check/item/type/delete/${item.id}`).then(() => {
          this.getList();
        });
      }).catch(() => {
        this.$message.info("取消操作")
      });
    },

    // 数据选择
    onTableSelectionChange(val) {
      this.selectIds = val.map(item => item.id);
    },

    // 行点击
    onRowClick(row, column, event) {
      this.$refs["table"].setCurrentRow(row);
      if (this.editing) {
        this.$refs["table"].toggleRowSelection(row);
      }
    },

    // 加载列表
    getList() {
      this.$axios.get("/v1/check/item/type/list", {params: this.listQuery}).then(data => {
        this.list = data.list;
        this.total = data.total;
        this.onCancel();
      });
    },

    // 导出数据
    onExport() {
      if (this.total > 0) {
        this.exportData = [];
        this.exportVisible = true;
        this.getExportData();
      }
    },

    // 取消导出
    onCancelExport() {
      this.exportVisible = false;
      this.exportCancel = true;
    },

    // 获取数据并导出
    getExportData() {
      if (!this.exportCancel) {
        this.$axios.get("/v1/check/item/type/list").then(data => {
          this.exportData = this.exportData.concat(data.list);
          this.exportProgress = Math.round(this.exportData.length * 100 / this.total);
          if (this.exportData.length < this.total && data.list.length > 0) {
            this.getExportData();
          } else {
            this.exportVisible = false;

            let tHeader = ["序号", "分类目录名称"];
            let filterVal = ["index", "typeName"];
            let data = this.exportData.map((value, index) => filterVal.map((key) => {
              if (key === "index") {
                return index + 1;
              } else {
                return value[key];
              }
            }));
            import('@/utils/Export2Excel').then(excel => {
              excel.export_json_to_excel({
                header: tHeader, // 表头 必填
                data, // 具体数据 必填
                filename: '项目分类列表', // 非必填
                autoWidth: true, // 非必填
                bookType: 'xlsx' // 非必填
              })
            })
          }
        });
      }
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {

}
</style>
