<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.equipmentledger_delete"
                   @click="handleDelete">批量删除
        </el-button>
      </template>
      <template slot="menuRight">
        <el-button type="primary"
                   size="small"
                   plain
                   v-if="permission.equipmentledger_add"
                   @click="handleDetailAdd">新增
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   v-if="permission.equipmentledger_audit"
                   @click="handleAuditing">批量审核
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   v-if="permission.equipmentledger_reject"
                   @click="handleReject">批量驳回
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   v-if="permission.equipmentledger_export"
                   @click="handleExport()">导出
        </el-button>
      </template>
      <template #menu="{row,index,size}">
        <el-button @click="handleDetailSearch(row,index)" type="text" v-if="permission.equipmentledger_view">查看详情</el-button>
        <el-button @click="rowDel(row)" type="text" v-if="permission.equipmentledger_delete">删除</el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {getList, getDetail, add, update, remove, reject, auditing} from "@/api/leger/equipmentledger";
  import {mapGetters} from "vuex";
  import website from "@/config/website";
  import {getToken} from "@/util/auth";
  import expUtil from "@/util/exportUtil";

  export default {
    data() {
      return {
        form: {},
        search:{},
        query: {},
        loading: true,
        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,
          column: [
            {
              label: "场站",
              prop: "station",
              type: "select",
              search: true,
              hide: true,
              dicUrl: "/api/daf-system/dict/dictionary?code=station",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入场站",
                trigger: "blur"
              }]
            },
            {
              label: "专业",
              prop: "major",
              search: true,
              hide: true,
              rules: [{
                required: true,
                message: "请输入专业",
                trigger: "blur"
              }]
            },
            {
              label: "安装位置",
              prop: "installationLocation",
              hide: true,
              search: true,
              rules: [{
                required: true,
                message: "请输入安装位置",
                trigger: "blur"
              }]
            },
            {
              label: "设备台账编码",
              prop: "eqLedgerCode",
              search: true,
              rules: [{
                required: true,
                message: "请输入设备台账编码",
                trigger: "blur"
              }]
            },
            {
              label: "用户状态",
              prop: "userStatus",
              type: "select",
              search: true,
              hide: true,
              dicUrl: "/api/daf-system/dict/dictionary?code=user_status",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入用户状态",
                trigger: "blur"
              }]
            },
            {
              label: "设备描述",
              prop: "deviceDescription",
              search: true,
              overHidden: true,
              rules: [{
                required: true,
                message: "请输入设备描述",
                trigger: "blur"
              }]
            },
            {
              label: "KKS编码",
              prop: "kksEncoding",
              rules: [{
                required: true,
                message: "请输入KKS编码",
                trigger: "blur"
              }]
            },
            {
              label: "设备种类",
              prop: "eqType",
              search: true,
              rules: [{
                required: true,
                message: "请输入设备种类",
                trigger: "blur"
              }]
            },
            {
              label: "资产编码",
              prop: "assetCode",
              rules: [{
                required: true,
                message: "请输入资产编码",
                trigger: "blur"
              }]
            },
            {
              label: "制造商",
              prop: "manufacturer",
              rules: [{
                required: true,
                message: "请输入制造商",
                trigger: "blur"
              }]
            },
            {
              label: "制造商国家",
              prop: "manufacturerCountry",
              rules: [{
                required: true,
                message: "请输入制造商国家",
                trigger: "blur"
              }]
            },
            {
              label: "制造商零件号",
              prop: "manufacturerPartNo",
              rules: [{
                required: true,
                message: "请输入制造商零件号",
                trigger: "blur"
              }]
            },
            {
              label: "制造年月",
              prop: "manufactureDate",
              rules: [{
                required: true,
                message: "请输入制造年月",
                trigger: "blur"
              }]
            },
            {
              label: "检修班组",
              prop: "maintenanceTeam",
              hide: true,
              search: true,
              rules: [{
                required: true,
                message: "请输入检修班组",
                trigger: "blur"
              }]
            },
            {
              label: "制造序列号",
              prop: "manufacturingSerialNo",
              search: true,
              rules: [{
                required: true,
                message: "请输入制造序列号",
                trigger: "blur"
              }]
            },
            {
              label: "型号",
              prop: "model",
              rules: [{
                required: true,
                message: "请输入型号",
                trigger: "blur"
              }]
            },
            {
              label: "大小尺寸",
              prop: "sizeAndSize",
              rules: [{
                required: true,
                message: "请输入大小尺寸",
                trigger: "blur"
              }]
            },
            {
              label: "对象重量",
              prop: "objectWeight",
              rules: [{
                required: true,
                message: "请输入对象重量",
                trigger: "blur"
              }]
            },
            {
              label: "审核状态",
              prop: "checkStatus",
              dicUrl: "/api/daf-system/dict/dictionary?code=check_status",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入审核状态",
                trigger: "blur"
              }]
            },
            {
              label: "系统状态",
              prop: "systemState",
              hide: true,
              dicUrl: "/api/daf-system/dict/dictionary?code=system_state",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入系统状态",
                trigger: "blur"
              }]
            },
            {
              label: "计划员组",
              prop: "plannerGroup",
              hide: true,
              rules: [{
                required: true,
                message: "请输入计划员组",
                trigger: "blur"
              }]
            },
            {
              label: "计划工厂",
              prop: "plannedFactory",
              hide: true,
              rules: [{
                required: true,
                message: "请输入计划工厂",
                trigger: "blur"
              }]
            },
            {
              label: "设备定级",
              prop: "eqRank",
              hide: true,
              rules: [{
                required: true,
                message: "请输入设备定级",
                trigger: "blur"
              }]
            },
            {
              label: "设备责任人姓名",
              prop: "eqResponsibleName",
              hide: true,
              rules: [{
                required: true,
                message: "请输入设备责任人姓名",
                trigger: "blur"
              }]
            },
            {
              label: "供应商",
              prop: "supplier",
              hide: true,
              rules: [{
                required: true,
                message: "请输入供应商",
                trigger: "blur"
              }]
            },
            {
              label: "出厂编号",
              prop: "factoryNo",
              hide: true,
              rules: [{
                required: true,
                message: "请输入出厂编号",
                trigger: "blur"
              }]
            },
            {
              label: "出厂日期",
              prop: "productionDate",
              hide: true,
              rules: [{
                required: true,
                message: "请输入出厂日期",
                trigger: "blur"
              }]
            },
            {
              label: "安装日期",
              prop: "installDate",
              hide: true,
              rules: [{
                required: true,
                message: "请输入安装日期",
                trigger: "blur"
              }]
            },
            {
              label: "调试单位",
              prop: "debuggingUnit",
              hide: true,
              rules: [{
                required: true,
                message: "请输入调试单位",
                trigger: "blur"
              }]
            },
            {
              label: "设备分类",
              prop: "eqClass",
              hide: true,
              rules: [{
                required: true,
                message: "请输入设备分类",
                trigger: "blur"
              }]
            },
            {
              label: "图纸编码",
              prop: "drawingCode",
              hide: true,
              rules: [{
                required: true,
                message: "请输入图纸编码",
                trigger: "blur"
              }]
            },
            {
              label: "KKS描述",
              prop: "kksDescription",
              hide: true,
              rules: [{
                required: true,
                message: "请输入KKS描述",
                trigger: "blur"
              }]
            },
            {
              label: "工厂区域",
              prop: "factoryArea",
              hide: true,
              rules: [{
                required: true,
                message: "请输入工厂区域",
                trigger: "blur"
              }]
            },
            {
              label: "设备责任人工号",
              prop: "eqResponsibleNo",
              hide: true,
              rules: [{
                required: true,
                message: "请输入设备责任人工号",
                trigger: "blur"
              }]
            },
            {
              label: "设计单位",
              prop: "designUnit",
              hide: true,
              rules: [{
                required: true,
                message: "请输入设计单位",
                trigger: "blur"
              }]
            },
            {
              label: "投运日期",
              prop: "operationDate",
              hide: true,
              rules: [{
                required: true,
                message: "请输入投运日期",
                trigger: "blur"
              }]
            },
            {
              label: "安装单位",
              prop: "installationUnit",
              hide: true,
              rules: [{
                required: true,
                message: "请输入安装单位",
                trigger: "blur"
              }]
            },
            {
              label: "成本中心",
              prop: "costCenter",
              hide: true,
              rules: [{
                required: true,
                message: "请输入成本中心",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
          // this.option.column = this.option.column.filter(v => {
          //     return this.permission['equipmentledger_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();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDetailAdd() {
        this.$router.push({
          path: "/leger/equipmentledgerDetail",
          query: {
            frameMode:"add"
          },
        });
      },
      handleDetailSearch(row) {
        debugger
        this.$router.push({
          path: "/leger/equipmentledgerDetail",
          query: {
            frameMode:"search",
            id: row.id
          },
        });
      },
      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();
          });
      },
      handleAuditing() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("是否将选择数据审核?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return auditing(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      handleReject() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("是否将选择数据审核?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return reject(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      handleExport() {
        this.$confirm("是否导出数据?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          expUtil.excelExportEasy(`/api/equipmentledger/export?daf-auth=${getToken()}`, this.search);
        });
      },
      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;
}
</style>
