<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="作业方式" prop="modeOperation">
        <el-select v-model="queryParams.modeOperation" placeholder="请选择作业方式" clearable>
          <el-option
            v-for="dict in dict.type.tapping_operation"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工程名称" prop="enginName">
        <el-input
          v-model="queryParams.enginName"
          placeholder="请输入工程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工程地点" prop="locations">
        <el-input
          v-model="queryParams.locations"
          placeholder="请输入工程地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zdsz:touchOperationEngineering:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zdsz:touchOperationEngineering:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zdsz:touchOperationEngineering:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zdsz:touchOperationEngineering:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="touchOperationEngineeringList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="" align="center" prop="id" v-if="false"/>
      <el-table-column label="作业方式" align="center" prop="modeOperation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tapping_operation" :value="scope.row.modeOperation"/>
        </template>
      </el-table-column>
      <el-table-column label="工程名称" align="center" prop="enginName"/>
      <el-table-column label="工程地点" align="center" prop="locations"/>
      <el-table-column label="审核状态" align="center" prop="nodeReViewStateList" width="250">
        <template slot-scope="scope">
          <div style="text-align:center">
            <el-popover
              v-for="item in scope.row.nodeReViewStateList"
              placement="top-start"
              trigger="hover"
              :content="item.Type">
              <span slot="reference" v-if="(item.state == '1'||item.state == '2')"
                    style="height: 20px;margin-right: 5px;width: 20px; border-radius: 50%; display: inline-block; background-color: #30B08F;"></span>
              <span slot="reference" v-if="item.state == '0' "
                    style="height: 20px;margin-right: 5px;width: 20px; border-radius: 50%; display: inline-block; background-color: #C03639;"></span>
            </el-popover>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zdsz:touchOperationEngineering:edit']"
          >修改工程信息
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="updateviewSource(scope.row)"
            v-hasPermi="['zdsz:touchOperationEngineering:edit']"
          >修改施工信息
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-add"
            @click="handleAddYL(scope.row)"
            v-hasPermi="['zdsz:touchOperationEngineering:edit']"
          >添加用料
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-time"
            @click="handleHistory(scope.row)"
            v-hasPermi="['zdsz:touchOperationEngineering:edit']"
          >历史施工
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zdsz:touchOperationEngineering:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改碰口作业对话框 -->
    <el-dialog :title="title" :visible.sync="open" :width="title == '添加碰口作业用料信息'?'1500px':'800px'"
               append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" >
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="作业方式" prop="modeOperation">
              <el-select v-model="form.modeOperation" placeholder="请选择作业方式" style="width: 100%" :disabled="title == '添加碰口作业用料信息'">
                <el-option
                  v-for="dict in dict.type.tapping_operation"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工程名称" prop="enginName">
              <el-input v-model="form.enginName" placeholder="请输入工程名称" :disabled="title == '添加碰口作业用料信息'"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
          <el-form-item label="工程地点" prop="locations">
            <el-input v-model="form.locations" placeholder="请输入地点" :disabled="title == '添加碰口作业用料信息'"/>
          </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="title == '添加碰口作业用料信息'"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="title=='添加碰口作业用料信息'">
          <el-col :span="24">
            <el-form-item label="环节">
              <el-checkbox-group v-model="currentCheckList">
                <el-checkbox
                  :label="item.dictValue"
                  v-for="item in nodeList"
                  :key="item.dictValue"
                ></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
        <EnginNodeInfo
          v-if="title=='添加碰口作业用料信息'"
          v-for="(e,idx) in currentCheckList"
          :key="idx"
          :name="e"
          :ref="'EnginNodeInfo'+idx"
          :types="currentCheckList"
          :currentContain="currentCheckList"
          :enginType="'碰口作业工程'"
          :enginSort="form.enginType"
        />
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" v-if="title == '添加碰口作业用料信息'" @click="addForm">添加用料</el-button>
        <el-button :loading="buttonLoading" type="primary" v-else @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <ConstructionDetails
      ref="ConstructionDetails"
      :currentCollapses="currentCollapses"
      :zEngineeringNodeBo="zEngineeringNodeBo"
      @updateNodeOption="updateNodeOption"
      @checkWorking="checkWorking"
      :enginType="'碰口作业'"
    />
  </div>
</template>

<script>
import {
  listTouchOperationEngineering,
  getTouchOperationEngineering,
  delTouchOperationEngineering,
  addTouchOperationEngineering,
  updateTouchOperationEngineering, QueryTouchOperationEngineering
} from "@/api/zdsz/touchOperationEngineering";
import {getDicts} from "@/api/system/dict/data";
import EnginNodeInfo from "@/components/EnginNodeInfo/indexPK.vue";
import ConstructionDetails from "@/components/ConstructionDetails/indexPK.vue";
import {EditEngineeEngineeIndustry, QueryEngineeIndustry} from "@/api/zdsz/engineeringIndustry";
import {insertReview} from "@/api/zdsz/enginee";

export default {
  name: "TouchOperationEngineering",
  components: {ConstructionDetails, EnginNodeInfo},
  dicts: ['tapping_operation', 'bury_the_wrench_hole', 'aerial_wrench_hole', 'shutdown_operation', 'pe_plugging', 'the_steel_pipe_sealed_with_gas'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      currentCheckList: [],
      currentCollapses: [],
      zEngineeringNodeBo: {},
      nodeList: [],
      status: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 碰口作业表格数据
      touchOperationEngineeringList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modeOperation: undefined,
        enginName: undefined,
        locations: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        modeOperation: [
          {required: true, message: "作业方式不能为空", trigger: "change"}
        ],
        enginName: [
          {required: true, message: "工程名称不能为空", trigger: "blur"}
        ],
        locations: [
          {required: true, message: "工程地点不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 节点审核
    checkWorking(checkingInfo) {
      let params = checkingInfo
      params.createTime = this.createTime
      console.log('节点审核 checkingInfo===', params)
      insertReview(params).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '审核成功',
            type: 'success'
          });
          this.$refs.ConstructionDetails.dialogVisible = false
          this.getList()
        }
      })
    },
    updateNodeOption(value) {
      try {
        this.form.zEngineeringNodeBo.zEngineeringInfoBoList = this.form.zEngineeringNodeBo.zEngineeringInfoBoList.filter(obj => value.map(o => o.id).includes(obj.id))
      } catch (e) {
      }
      this.form.files = this.form.pics
      updateTouchOperationEngineering(this.form).then(res => {
        this.$modal.msgSuccess("修改成功");
        this.$refs.ConstructionDetails.dialogVisible = false
      })
      this.getList();
    },
    viewNodeSource(e) {
      this.currentCollapses = []
      if (!e) return
      // todo: 获取数据
      QueryTouchOperationEngineering({
        id: this.currentId,
        type: e
      }).then(res => {
        this.createTime = res.data.createTime
        this.form = res.data
        try {
          this.currentCollapses = res.data.zEngineeringNodeBo.zEngineeringInfoBoList
          this.$refs.ConstructionDetails.setEngineId(res.data.zEngineeringNodeBo.id || null)
        } catch (error) {
          this.currentCollapses = [];
        }
      })
    },
    updateviewSource(e) {
      this.currentId = e.id
      let dists ;
      if ("地埋扳孔"  == e.modeOperation){
        dists="bury_the_wrench_hole";
      }else if ("架空扳孔"  == e.modeOperation){
        dists="aerial_wrench_hole";
      }else if ("停气作业"  == e.modeOperation){
        dists="shutdown_operation";
      }else if ("PE 封堵"  == e.modeOperation){
        dists="pe_plugging";
      }else if ("钢管带气封堵"  == e.modeOperation){
        dists="the_steel_pipe_sealed_with_gas";
      }
      getDicts(dists).then(res => {
        this.nodeList = res.data
        let dict = []
        for (let i = 0; i < this.nodeList.length; i++) {
          dict.push({
            label: this.nodeList[i].dictLabel,
            value: this.nodeList[i].dictValue
          })
        }
        this.status = 'put'
        this.$refs.ConstructionDetails.open(dict, null, "修改施工信息", null, this.status)
      })

    },
    handleHistory(e, type = null){
      this.currentId = e.id
      let dict = []
      let dists ;
      if ("地埋扳孔"  == e.modeOperation){
        dists="bury_the_wrench_hole";
      }else if ("架空扳孔"  == e.modeOperation){
        dists="aerial_wrench_hole";
      }else if ("停气作业"  == e.modeOperation){
        dists="shutdown_operation";
      }else if ("PE 封堵"  == e.modeOperation){
        dists="pe_plugging";
      }else if ("钢管带气封堵"  == e.modeOperation){
        dists="the_steel_pipe_sealed_with_gas";
      }
      getDicts(dists).then(res => {
        this.nodeList = res.data
        for (let i = 0; i < this.nodeList.length; i++) {
          dict.push({
            label: this.nodeList[i].dictLabel,
            value: this.nodeList[i].dictValue
          })
        }
        if (type) {
          this.status = 'review'
        } else {
          this.status = 'read-only'
        }
        this.$refs.ConstructionDetails.open(dict, null, "历史施工信息", null, this.status)
      })
    },
    addForm(){
      // 收集节点信息
      let nodeCollection = []
      this.currentCheckList.forEach((e,idx) => {
        let nodeItem = this.$refs['EnginNodeInfo'+ idx][0].infoCollection()
        //  console.log(nodeItem)
        nodeCollection.push(nodeItem)
      })
      this.form.zEngineeringNodeBoList = nodeCollection
      addTouchOperationEngineering(this.form).then(response => {
        this.$modal.msgSuccess("添加用料成功");
        this.cancel()
        this.getList();
      }).finally(() => {
        this.buttonLoading = false;
      });
    },
    /** 查询碰口作业列表 */
    getList() {
      this.loading = true;
      listTouchOperationEngineering(this.queryParams).then(response => {
        this.touchOperationEngineeringList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.title = "";
      this.reset();
    },
    // 表单重置
    reset() {
      this.currentCheckList=[]
      this.nodeList=[]
      this.form = {
        id: undefined,
        modeOperation: undefined,
        enginName: undefined,
        locations: undefined,
        remark: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        updateInfoTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加碰口作业";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getTouchOperationEngineering(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改碰口作业";
      });
    },
    handleAddYL(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getTouchOperationEngineering(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "添加碰口作业用料信息";
        let dists ;
        if ("地埋扳孔"  == response.data.modeOperation){
          dists="bury_the_wrench_hole";
        }else if ("架空扳孔"  == response.data.modeOperation){
          dists="aerial_wrench_hole";
        }else if ("停气作业"  == response.data.modeOperation){
          dists="shutdown_operation";
        }else if ("PE 封堵"  == response.data.modeOperation){
          dists="pe_plugging";
        }else if ("钢管带气封堵"  == response.data.modeOperation){
          dists="the_steel_pipe_sealed_with_gas";
        }
        getDicts(dists).then(res => {
          this.nodeList = res.data
        })
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateTouchOperationEngineering(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addTouchOperationEngineering(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除碰口作业编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delTouchOperationEngineering(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zdsz/touchOperationEngineering/export', {
        ...this.queryParams
      }, `touchOperationEngineering_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
