<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.operationticket_delete"
                   @click="handleDelete">批量删除
        </el-button>
      </template>
      <template slot="menuRight">
        <el-button type="primary"
                   size="small"
                   plain
                   @click="handleDetailAdd">新增
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   @click="handleReject">批量驳回
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   @click="handleAuditing">批量审核
        </el-button>

        <el-button type="primary"
                   size="small"
                   plain
                   @click="handleExport()">导出
        </el-button>
      </template>
      <template #menu="{row,index,size}">
        <el-button @click="handleDetailSearch(row,index)" type="text">查看</el-button>
        <el-button @click="handleDetailcls(row,index)" type="text">关票</el-button>
        <el-button @click="rowDel(row)" type="text">删除</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/operationticket";
  import {mapGetters} from "vuex";
  import {auditing, reject} from "@/api/smart/operationticket";
  import expUtil from "@/util/exportUtil";
  import {getToken} from "@/util/auth";

  export default {
    data() {
      return {
        form: {},
        updateid:0,
        query: {},
        loading: true,
        closeTicketBox: false,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          indexLabel: "序号",
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "主键",
              prop: "id",
              hide: true,
              rules: [{
                required: true,
                message: "请输入主键",
                trigger: "blur"
              }]
            },
            {
              label: "KKS编码",
              prop: "kksEncoding",
              hide: true,
              rules: [{
                required: true,
                message: "请输入KKS编码",
                trigger: "blur"
              }]
            },
            {
              label: "KSS描述",
              hide: true,
              prop: "kksDescription",
              rules: [{
                required: true,
                message: "请输入KSS描述",
                trigger: "blur"
              }]
            },
            {
              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: "操作票编号",
              search: true,
              prop: "operationTicketNo",
              rules: [{
                required: true,
                message: "请输入操作票编号",
                trigger: "blur"
              }]
            },
            {
              label: "操作票类型",
              search: true,
              prop: "operationTicketType",
              rules: [{
                required: true,
                message: "请输入操作票类型",
                trigger: "blur"
              }]
            },
            {
              label: "工作班组",
              prop: "workTeam",
              rules: [{
                required: true,
                message: "请输入工作班组",
                trigger: "blur"
              }]
            },
            {
              label: "工作负责人",
              prop: "workLeader",
              search: true,
              rules: [{
                required: true,
                message: "请输入工作负责人",
                trigger: "blur"
              }]
            },
            {
              label: "监护人",
              prop: "guardian",
              rules: [{
                required: true,
                message: "请输入监护人",
                trigger: "blur"
              }]
            },
            {
              label: "发令人",
              prop: "givingOrdersUser",
              rules: [{
                required: true,
                message: "请输入发令人",
                trigger: "blur"
              }]
            },
            {
              label: "作业风险等级",
              prop: "jobRiskLevel",
              search: true,
              rules: [{
                required: true,
                message: "请输入作业风险等级",
                trigger: "blur"
              }]
            },
            {
              label: "控制等级",
              prop: "controlLevel",
              search: true,
              rules: [{
                required: true,
                message: "请输入控制等级",
                trigger: "blur"
              }]
            },
            {
              label: "计划开始时间",
              prop: "plannedStartTime",
              search: true,
              type: "date",
              format: 'yyyy/MM/dd',
              valueFormat: "yyyyMMdd",
              rules: [{
                required: true,
                message: "请输入计划开始时间",
                trigger: "blur"
              }]
            },
            {
              label: "计划结束时间",
              prop: "plannedEndTime",
              type: "date",
              format: 'yyyy/MM/dd',
              valueFormat: "yyyyMMdd",
              rules: [{
                required: true,
                message: "请输入计划结束时间",
                trigger: "blur"
              }]
            },
            {
              label: "签发时间",
              prop: "workIssuanceTime",
              type: "date",
              format: 'yyyy/MM/dd',
              valueFormat: "yyyyMMdd",
              rules: [{
                required: true,
                message: "请输入签发时间",
                trigger: "blur"
              }]
            },
            {
              label: "操作项目数",
              prop: "operationItemsCount",
            },
            {
              label: "人员资格",
              prop: "personnelQualification",
              hide: true,
              rules: [{
                required: true,
                message: "请输入人员资格",
                trigger: "blur"
              }]
            },
            {
              label: "人员状态",
              prop: "personnelState",
              hide: true,
              rules: [{
                required: true,
                message: "请输入人员状态",
                trigger: "blur"
              }]
            },
            {
              label: "人员防护",
              prop: "personnelPritection",
              hide: true,
              rules: [{
                required: true,
                message: "请输入人员防护",
                trigger: "blur"
              }]
            },
            {
              label: "安全距离",
              prop: "safeDistance",
              hide: true,
              rules: [{
                required: true,
                message: "请输入安全距离",
                trigger: "blur"
              }]
            },
            {
              label: "走错间隔",
              prop: "wrongInterval",
              hide: true,
              rules: [{
                required: true,
                message: "请输入走错间隔",
                trigger: "blur"
              }]
            },
            {
              label: "安全措施落实",
              prop: "securityMeasuresImplement",
              hide: true,
              rules: [{
                required: true,
                message: "请输入安全措施落实",
                trigger: "blur"
              }]
            },
            {
              label: "开展过程检查",
              prop: "conductProcessInspections",
              hide: true,
              rules: [{
                required: true,
                message: "请输入开展过程检查",
                trigger: "blur"
              }]
            },
            {
              label: "安全培训落实",
              prop: "securityTrainingImplement",
              hide: true,
              rules: [{
                required: true,
                message: "请输入安全培训落实",
                trigger: "blur"
              }]
            },
            {
              label: "其他",
              prop: "other",
              hide: true,
              rules: [{
                required: true,
                message: "请输入其他",
                trigger: "blur"
              }]
            },
            {
              label: "安全技术措施交底",
              prop: "securityMeasuresDisclosure",
              hide: true,
              rules: [{
                required: true,
                message: "请输入安全技术措施交底",
                trigger: "blur"
              }]
            },
            {
              label: "作业后风险管控情况评价",
              prop: "riskControlEvaluation",
              hide: true,
              rules: [{
                required: true,
                message: "请输入作业后风险管控情况评价",
                trigger: "blur"
              }]
            },
            {
              label: "状态",
              prop: "sts",
              search: true,
              type: "select",
              dicUrl: "/api/daf-system/dict/dictionary?code=operation_sts",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入状态",
                trigger: "blur"
              }]
            },
            {
              label: "是否合格",
              prop: "isQuakified",
              hide: true,
              rules: [{
                required: true,
                message: "请输入是否合格",
                trigger: "blur"
              }]
            },
            {
              label: "审核状态",
              prop: "reviewStatus",
              dicUrl: "/api/daf-system/dict/dictionary?code=check_status",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
              rules: [{
                required: true,
                message: "请输入审核状态",
                trigger: "blur"
              }]
            },
          ]
        },
        closeTicketForm: {},
        closeTicketOption: {
          height: 'auto',
          calcHeight: 210,
          emptyBtn:false,
          submitBtn:false,
          tip: false,
          border: true,
          index: false,
          selection: true,
          column: [
            {
              label: "是否合格",
              type: "select",
              prop: "isQuakified",
              dicUrl: "/api/daf-system/dict/dictionary?code=is_qualified",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "是否合格",
                trigger: "blur"
              }],
              span: 24
            },
            {
              label: "作业后风险管控情况评价",
              type: "textarea",
              prop: "riskControlEvaluation",
              span: 24
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
          // this.option.column = this.option.column.filter(v => {
          //     return this.permission['operationticket_col_' + v.prop]
          // })
        return {
          // addBtn: this.validData(this.permission.operationticket_add, false),
          // viewBtn: this.validData(this.permission.operationticket_view, false),
          // delBtn: this.validData(this.permission.operationticket_delete, false),
          // editBtn: this.validData(this.permission.operationticket_edit, false)
          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();
          });
      },
      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'
      },
      handleDetailAdd() {
        this.$router.push({
          path: "/smart/operationticketDetail",
          query: {
            frameMode:"add"
          },
        });
      },
      handleDetailcls(row) {
        debugger
        this.updateid = row.id;
        this.closeTicketBox = true;
      },
      handleDetailCancel() {
        debugger
        this.closeTicketBox = false;
      },
      handleDetailClose() {
        debugger
         return closeticket( this.updateid, this.closeTicketForm.isQuakified,this.closeTicketForm.riskControlEvaluation)
          .then(() => {
            this.closeTicketForm.isQuakifie = '';
            this.closeTicketForm.riskControlEvaluation = '';
            this.closeTicketBox = false;
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection()
          });
      },
      handleDetailSearch(row) {
        debugger
        this.$router.push({
          path: "/smart/operationticketDetail",
          query: {
            frameMode:"search",
            id: row.id,
            operationTicketNo:row.operationTicketNo,
            reviewStatus: row.reviewStatus
          },
        });
      },
      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.excelExport(`/api/smart/operationticket/export?daf-auth=${getToken()}`, this.search);
        });
      },
    }
  };
</script>

<style>
.header-class{
  background-color: #3366cc !important;
  color: #FFFFFF !important;
}
</style>
