<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               :search.sync="search"
               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.workpermit_delete"
                   @click="handleDelete">批量删除
        </el-button>
      </template>
      <template slot="menuRight">
        <el-button type="primary"
                   size="small"
                   plain
                   v-if="permission.workpermit_add"
                   @click="handleDetailAdd">新增
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   v-if="permission.workpermit_audit"
                   @click="handleAuditing">批量审核
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   v-if="permission.workpermit_reject"
                   @click="handleReject">批量驳回
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   v-if="permission.workpermit_export"
                   @click="handleExport()">导出
        </el-button>
      </template>
      <template #menu="{row,index,size}">
        <el-button @click="handleDetailSearch(row,index)" type="text" v-if="permission.workpermit_view">查看详情</el-button>
        <el-button @click="handleCloseTicket(row)" type="text" v-if="permission.workpermit_close">关票</el-button>
        <el-button @click="rowDel(row)" type="text" v-if="permission.workpermit_delete">删除</el-button>
      </template>
    </avue-crud>
    <el-dialog title="关票提醒"
               append-to-body
               :visible.sync="closeTicketBox"
               width="555px">
      <avue-form :option="closeTicketOption" v-model="closeTicketForm" ref="closeTicketForm">
      </avue-form>
      <div class="container">
         <span>
           <el-button type="primary"
                      size="small"
                      plain
                      @click="handleDetailCancel">取消
            </el-button>
            <el-button type="submit"
                       size="small"
                       plain
                       @click="handleDetailClose">关票
            </el-button>
         </span>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove,closeTicket} from "@/api/smart/workpermit";
  import {mapGetters} from "vuex";
  import {auditing, reject} from "@/api/smart/workpermit";
  import expUtil from "@/util/exportUtil";
  import {getToken} from "@/util/auth";
  import {Alert} from "element-ui";
  import {closeticket} from "@/api/smart/operationticket";

  export default {
    data() {
      return {
        form: {},
        query: {},
        search:{},
        loading: true,
        closeTicketBox: false,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 22,
          searchBtnText: '查询',
          emptyBtnText: '重置',
          tip: false,
          border: true,
          indexLabel: "序号",
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "KKS编码",
              prop: "kksEncoding",
              rules: [{
                required: true,
                message: "请输入KKS编码",
                trigger: "blur"
              }],
              span:8,
              hide: true,
            },
            {
              label: "KKS描述",
              prop: "kksDescription",
              rules: [{
                required: true,
                message: "请输入KKS描述",
                trigger: "blur"
              }],
              span:8,
              hide: true,
            },
            {
              label: "工作票编号",
              prop: "workTicketNo",
              search: true,
              rules: [{
                required: true,
                message: "请输入工作票编号",
                trigger: "blur"
              }],
              span:8,
            },
            {
              label: "工作票类型",
              prop: "workTicketType",
              search: true,
              rules: [{
                required: true,
                message: "请输入工作票类型",
                trigger: "blur"
              }],
              span:8,
            },
            {
              label: "工单号",
              prop: "tickets",
              span:8,
            },
            {
              label: "场站",
              prop: "stations",
              type: "select",
              search: true,
              dicUrl: "/api/daf-system/dict/dictionary?code=station",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              span:8,
            },
            {
              label: "班组",
              prop: "team",
              rules: [{
                required: true,
                message: "请输入班组",
                trigger: "blur"
              }],
              span:8,
            },
            {
              label: "工作负责人",
              prop: "workChargePerson",
              search: true,
              rules: [{
                required: true,
                message: "请输入工作负责人",
                trigger: "blur"
              }],
              span:8,
            },
            {
              label: "工作班成员",
              prop: "workClassMembers",
              rules: [{
                required: true,
                message: "请输入工作班成员",
                trigger: "blur"
              }],
              span:8,
            },
            {
              label: "许可人",
              prop: "licensors",
              search: true,
              rules: [{
                required: true,
                message: "请输入许可人",
                trigger: "blur"
              }],
              span:8,
            },
            {
              label: "签发人",
              prop: "issuer",
              search: true,
              rules: [{
                required: true,
                message: "请输入签发人",
                trigger: "blur"
              }],
              span:8,
            },
            {
              label: "功能位置",
              prop: "functionalLocation",
              span:8,
            },
            {
              label: "工作地点",
              prop: "placeWork",
              rules: [{
                required: true,
                message: "请输入工作地点",
                trigger: "blur"
              }],
              span:8,
            },
            {
              label: "工作内容",
              prop: "jobDescription",
              rules: [{
                required: true,
                message: "请输入工作内容",
                trigger: "blur"
              }],
              span:8,
            },
            {
              label: "计划开始时间",
              prop: "planStartDateExt",
              hide: true,
              type: "date",
              format: 'yyyy/MM/dd',
              search: true,
              rules: [{
                required: true,
                message: "请输入计划开始时间",
                trigger: "blur"
              }],
              span:8,
            },
            {
              label: "计划开始时间",
              prop: "planStartDate",
              type: "date",
              format: 'yyyy/MM/dd',
              rules: [{
                required: true,
                message: "请输入计划开始时间",
                trigger: "blur"
              }],
              span:8,
            },
            {
              label: "工作条件",
              prop: "workingConditions",
              hide:true,
              rules: [{
                required: true,
                message: "请输入工作条件",
                trigger: "blur"
              }],
              span:8,
            },
            {
              label: "计划结束时间",
              prop: "planEndDate",
              type: "date",
              format: 'yyyy/MM/dd',
              rules: [{
                required: true,
                message: "请输入计划结束时间",
                trigger: "blur"
              }],
              span:8,
            },
            {
              label: "实际结束时间",
              hide:true,
              prop: "actualEndDate",
              type: "date",
              format: 'yyyy/MM/dd',
              span:8,
            },
            {
              label: "是否延期",
              hide:true,
              prop: "isExtended",
              type: "select",
              dicUrl: "/api/daf-system/dict/dictionary?code=yes_no",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              span:8,
            },
            {
              label: "状态",
              search: true,
              type: "select",
              prop: "workState",
              dicUrl: "/api/daf-system/dict/dictionary?code=work_status",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              span:8,
            },
            {
              label: "审核状态",
              prop: "checkStatus",
              hide:true,
              dicUrl: "/api/daf-system/dict/dictionary?code=check_status",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
          // this.option.column = this.option.column.filter(v => {
          // //     return this.permission['workpermit_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: "操作成功!"
            });
          });
      },
      handleDetailAdd() {
        this.$router.push({
          path: "/smart/workpermitDetail",
          query: {
            frameMode:"add"
          },
        });
      },
      handleDetailSearch(row) {
        debugger
        this.$router.push({
          path: "/smart/workpermitDetail",
          query: {
            frameMode:"search",
            id: row.id,
            ticketCode:row.ticketCode
          },
        });
      },
      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();
          });
      },
      handleCloseTicket(row) {
        this.$confirm("是否结束该工作票?", "提示",{
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return closeTicket(row.id);
          })
          .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/smart/workpermit/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();
        });
      }
    }
  };
</script>

<style>
</style>
