<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               :search.sync="search"
               :header-cell-class-name="headerClass"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.inspectionroute_delete"
                   @click="handleDelete">批量删除
        </el-button>
      </template>
      <template slot="menuRight">
        <el-button type="primary"
                   size="small"
                   v-if="permission.inspectionroute_add"
                   plain
                   @click="handleDetailAdd">新增
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   @click="handleTakeEffect">生效
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   @click="handleLapse">失效
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   @click="handleExport">导出
        </el-button>
      </template>
      <template #menu="{row,index,size}">
        <el-button v-if="permission.inspectionroute_view" @click="handleDetailSearch(row,index)" type="text">查看</el-button>
        <el-button v-if="permission.inspectionroute_delete" @click="rowDel(row)" type="text">删除</el-button>
      </template>
    </avue-crud>
    <el-dialog title="失效"
               append-to-body
               :visible.sync="cancelBox"
               width="555px">
      <avue-form :option="cancelOption" v-model="cancelForm" ref="cancelForm">
      </avue-form>
      <div class="container">
         <span>
            <el-button type="primary"
                       size="small"
                       plain
                       @click="handleDetailCancel">失效
            </el-button>
         </span>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove, takeEffect, lapse} from "@/api/inspection/inspectionplan";
  import {mapGetters} from "vuex";
  import expUtil from "@/util/exportUtil";
  import {getToken} from "@/util/auth";

  export default {
    data() {
      return {
        form: {},
        search:{},
        query: {},
        loading: true,
        cancelBox: false,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: false,
          viewBtn: true,
          selection: true,
          searchBtnText: '查询',
          emptyBtnText: '重置',
          column: [
            {
              label: "计划编号",
              prop: "planNo",
              search: true
            },
            {
              label: "计划名称",
              prop: "planName",
              search: true
            },
            {
              label: "场站",
              prop: "station",
              type: "select",
              search: true,
              dicUrl: "/api/daf-system/dict/dictionary?code=station",
              props: {
                label: "dictValue",
                value: "dictKey"
              }
            },
            {
              label: "计划类型",
              prop: "planType"
            },
            {
              label: "周期类型",
              prop: "periodType",
              search: true
            },
            {
              label: "周期值",
              prop: "periodValue"
            },
            {
              label: "计划起始时间",
              prop: "startTime",
              type: "date",
              format: 'yyyy年MM月dd日',
              valueFormat: "yyyyMMdd",
            },
            {
              label: "计划终了时间",
              prop: "endTime",
              type: "date",
              format: 'yyyy年MM月dd日',
              valueFormat: "yyyyMMdd",
            },
            {
              label: "计划状态",
              prop: "planStatus",
              type: "select",
              search: true,
              dicUrl: "/api/daf-system/dict/dictionary?code=istatus",
              props: {
                label: "dictValue",
                value: "dictKey"
              }
            },
            {
              label: "取消原因",
              prop: "cancelReason"
            },
          ]
        },
        cancelForm: {},
        cancelOption: {
          height: 'auto',
          calcHeight: 210,
          emptyBtn:false,
          submitBtn:false,
          tip: false,
          border: true,
          index: false,
          selection: true,
          column: [
            {
              label: "取消原因",
              type: "textarea",
              prop: "cancelReason",
              span: 24
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
          // this.option.column = this.option.column.filter(v => {
          //     return this.permission['inspectionplan_col_' + v.prop]
          // })
        return {
          addBtn: false,
          viewBtn: false,
          delBtn: false,
          editBtn: false
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      handleDetailAdd() {
        this.$router.push({
          path: "/inspection/inspectionPlanDetail",
          query: {
            frameMode:"add"
          },
        });
      },
      handleDetailSearch(row) {
        debugger
        this.$router.push({
          path: "/inspection/inspectionPlanDetail",
          query: {
            frameMode:"view",
            id: row.id
          },
        });
      },
      handleTakeEffect() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("是否将选择数据改为生效状态?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return takeEffect(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      handleLapse() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.cancelBox = true;
      },
      handleDetailCancel() {
        this.$confirm("是否将选择数据改为失效状态?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return lapse(this.ids, this.cancelForm.cancelReason);
          })
          .then(() => {
            this.cancelForm.cancelReason = '';
            this.cancelBox = false;
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection()
          });
      },
      handleExport() {
        this.$confirm("是否导出数据?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          expUtil.excelExport(`/api/inspection/inspectionplan/export?daf-auth=${getToken()}`, this.search, ['station', "planStatus"]);
        });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
      headerClass() {
        return 'header-class'
      }
    }
  };
</script>

<style>
.header-class{
  background-color: #3366cc !important;
  color: #FFFFFF !important;
}
.container {
  display: flex;
  justify-content: flex-end; /* 将子元素推到容器的末端 */
}
</style>
