<template>
  <el-main v-loading="mainLoading">
    <el-card>
      <el-descriptions title="作业详情" :column="3">
        <el-descriptions-item label="作业名称:">
          {{ operationObj.name }}
        </el-descriptions-item>
        <el-descriptions-item label="作业区域:">
          <span v-if="operationObj.area_">{{ operationObj.area_.name }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="生产状态:">
          {{ operationObj.state_work }}
        </el-descriptions-item>
        <el-descriptions-item label="状态:">
          {{ state_[operationObj.state] }}
        </el-descriptions-item>
        <el-descriptions-item label="预计开始:">
          {{ operationObj.start_time }}
        </el-descriptions-item>
        <el-descriptions-item label="预计结束:">
          {{ operationObj.end_time }}
        </el-descriptions-item>
        <el-descriptions-item label="属地部门:">
          <span v-if="operationObj.dept_ter_">{{
            operationObj.dept_ter_.name
          }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="业务部门:">
          <span v-if="operationObj.dept_bus_">{{
            operationObj.dept_bus_.name
          }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="协调员:">
          <span v-if="operationObj.coordinator_">{{
            operationObj.coordinator_.name
          }}</span>
        </el-descriptions-item>
      <el-descriptions-item label="监控设备:" v-if="operationObj.vchannels">
        <span v-for="item in operationObj.vchannels_" :key="item.id">{{ item.name }}({{item.code}})/</span>
      </el-descriptions-item>
      </el-descriptions>
    </el-card>

    <el-card header="许可证" style="margin-top: 8px">
      <div class="left-panel">
        <el-button icon="el-icon-plus" style="margin: 3px 0px 0px 10px" v-for="(item, index) in oplcateList"
          :key="index" :title="item.name" type="primary" v-auth="'opl.create'" @click="addOpl(item.id, item.code)">
          {{ item.name }}
        </el-button>
      </div>
      <el-divider />
      <scTable ref="table" :data="apiObj" row-key="id" @selection-change="selectionChange" stripe hidePagination
        hideRefresh @resetQuery="resetQuery">
        <el-table-column label="#" type="index" width="50"></el-table-column>
        <el-table-column label="作业许可编号" prop="number" width="150"></el-table-column>
        <el-table-column label="作业级别" prop="level"></el-table-column>
        <el-table-column label="许可证种类" prop="cate_name"></el-table-column>
        <el-table-column label="流程状态" width="160">
          <template #default="scope">
            <span v-if="scope.row.ticket">{{ scope.row.ticket_.state_.name }}
              -{{ act_states[scope.row.ticket_.act_state]}}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="预计开始" prop="start_time" width="150"></el-table-column>
        <el-table-column label="作业部门" prop="dept_do" width="150" :show-overflow-tooltip="true"><template
            #default="scope">
            {{ scope.row.dept_do_.name }}
          </template></el-table-column>
        <el-table-column label="作业负责人" prop="charger"><template #default="scope">
            {{ scope.row.charger_.name }}
          </template></el-table-column>
        <el-table-column label="监控状态" prop="mtask_uid">
          <template #default="scope">
            <span v-if="scope.row.mtask_uid">进行中</span>
          </template></el-table-column>

        <el-table-column label="操作" fixed="right" width="100">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看
            </el-button>
            <el-button v-if="operationObj.state === 10" link type="warning" size="small" v-auth="'opl.update'"
              @click="table_edit(scope.row, scope.$index)">编辑
            </el-button>
          </template>
        </el-table-column>
      </scTable>
    </el-card>
    <el-drawer :size="'50%'" v-model="showLimited" :title="oplName + '作业许可证详情'">
      <sc-fire :id="oplId"></sc-fire>
    </el-drawer>
  </el-main>
</template>
<script>
import ScFire from "../../components/scOpl/scFire";

export default {
  name: "opl",
  components: { ScFire },
  data() {
    return {
      dialog: {
        save: false,
        permission: false,
      },
      mainLoading: false,
      apiObj: [],
      query: {},
      selection: [],
      search: {
        keyword: null,
      },
      oplcateList: {},
      operationObj: {},
      showLimited: false,
      oplId: "",
      state_: {
        10: "创建中",
        20: "审批中",
        30: "待开始",
        40: "进行中",
        50: "已完成",
      },
      act_states: {
        0: "草稿中",
        1: "进行中",
        2: "被退回",
        3: "被撤回",
        4: "已完成",
        5: "已关闭",
      },
    };
  },

  mounted() {
    this.getoplcate();
    this.getopllist();
    this.getOperation();
  },
  methods: {
    getOperation() {
      this.mainLoading = true;
      this.$API.opm.operation.read
        .req(this.$route.query.id)
        .then((res) => {
          this.mainLoading = false;
          this.operationObj = res;
        })
        .catch((e) => {
          this.mainLoading = false;
        });
    },
    //作业许可证列表
    getopllist() {
      this.$API.opm.opl.list
        .req({ operation: this.$route.query.id, page: 0 })
        .then((res) => {
          this.apiObj = res;
        });
    },

    //作业许可证类型列表
    getoplcate() {
      this.$API.opm.oplcate.list.req({ page: 0 }).then((res) => {
        this.oplcateList = res;
        console.log(res);
      });
    },

    //点击作业类型，创建作业许可证添加
    addOpl(id, code) {
      this.$router.push({
        name: code,
        query: {
          operationid: this.$route.query.id, //作业ID
          oplcateId: id, //许可证类型ID
          oplId: "", //许可证ID
        },
      });
    },
    //编辑
    table_edit(row) {
      this.$router.push({
        name: row.cate_code,
        query: {
          oplId: row.id, //许可证ID
        },
      });
    },
    //查看
    table_show(row) {
      this.showLimited = true;
      this.oplId = row.id;
      this.oplCode = row.cate_code;
      this.oplName = row.cate_name;
      this.workId = row.workers[0];
    },
    //权限设置
    permission() {
      this.dialog.permission = true;
      this.$nextTick(() => {
        this.$refs.permissionDialog.open();
      });
    },
    //删除
    async table_del(row) {
      var reqData = { id: row.id };
      var res = await this.$API.demo.post.post(reqData);
      if (res.code == 200) {
        this.$refs.table.refresh();
        this.$message.success("删除成功");
      } else {
        this.$alert(res.message, "提示", { type: "error" });
      }
    },
    //批量删除
    async batch_del() {
      this.$confirm(
        `确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`,
        "提示",
        {
          type: "warning",
        }
      )
        .then(() => {
          const loading = this.$loading();
          this.$refs.table.refresh();
          loading.close();
          this.$message.success("操作成功");
        })
        .catch(() => { });
    },
    //表格选择后回调事件
    selectionChange(selection) {
      this.selection = selection;
    },
    //表格内开关
    changeSwitch(val, row) {
      row.status = row.status == "1" ? "0" : "1";
      row.$switch_status = true;
      setTimeout(() => {
        delete row.$switch_status;
        row.status = val;
        this.$message.success("操作成功");
      }, 500);
    },
    //搜索
    handleQuery() { },
    //根据ID获取树结构
    filterTree(id) {
      var target = null;

      function filter(tree) {
        tree.forEach((item) => {
          if (item.id == id) {
            target = item;
          }
          if (item.children) {
            filter(item.children);
          }
        });
      }

      filter(this.$refs.table.tableData);
      return target;
    },
    //本地更新数据
    handleSaveSuccess(data, mode) {
      if (mode == "add") {
        this.$refs.table.refresh();
      } else if (mode == "edit") {
        this.$refs.table.refresh();
      }
    },
    resetQuery() {
      this.query = {};
    },
  },
};
</script>
