<template>
  <div class="app-container">
    <el-page-header content="重点工程" @back="back"/>
    <div class="level-title mt-20"/>
    <el-form label-width="100px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="关键字搜索">
            <el-input v-model="KEYWORDS" placeholder="搜索关键字" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="集团单位">
            <el-select v-model="GROUP_UNIT" placeholder="请选择集团单位" style="width: 100%;">
              <el-option v-for="item in groupCorpList" :key="item.CORPINFO_ID" :label="item.CORP_NAME" :value="item.CORPINFO_ID" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="主管部门">
            <SelectTree
              ref="deptTree"
              :clearable="false"
              :options="treeDataInspectDept"
              :props="defaultProps"
              v-model="COMPETENT_DEPT_ID"
              placeholder="请选择企业主管部门"
              style="width: 100%"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="施工相关方">
            <el-select v-model="UNITS_ID" placeholder="请选择施工相关方" style="width: 100%">
              <el-option
                v-for="item in unitsList"
                :key="item.UNITS_ID"
                :label="item.UNITS_NAME"
                :value="item.UNITS_ID" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="监管部门">
            <SelectTree
              ref="deptTree1"
              :clearable="false"
              :options="treeDataInspectDept"
              :props="defaultProps"
              v-model="DEPARTMENT_ID"
              placeholder="请选择股份监管部门"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="合同起止时间">
            <el-date-picker
              v-model="CONTRACTTIME"
              type="daterange"
              unlink-panels
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd"
              style="width: 100%;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>

        <el-col :span="8">
          <el-form-item label="状态">
            <el-select ref="SELECT_STATE" v-model="STATE" style="width: 100%;">
              <el-option v-for="item in stateList" :key="item.ID" :label="item.NAME" :value="item.ID" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label-width="10px">
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
              搜索
            </el-button>
            <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">
              重置
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="varList"
      :row-key="getRowKey"
      :header-cell-style="{
        'font-weight': 'bold',
        'color': '#000'
      }"
      tooltip-effect="dark"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        :reserve-selection="true"
        type="selection"
        width="55"
        align="center"/>
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="OUTSOURCED_NAME" label="重点工程名称" show-overflow-tooltip />
      <el-table-column prop="UNITS_NAME" label="施工相关方" width="250" show-overflow-tooltip />
      <el-table-column label="辖区单位" show-overflow-tooltip >
        <template slot-scope="{row}">
          <span v-if="row.JURISDICTION_UNIT_NAME"> {{ row.JURISDICTION_UNIT_NAME }} </span>
          <span v-else>{{ row.INVOLVING_CORPS_DEPART_NAME }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主管部门" show-overflow-tooltip >
        <template slot-scope="{row}">
          <span v-if="row.Q_COMPETENT_DEPT_NAME"> {{ row.Q_COMPETENT_DEPT_NAME }} </span>
          <span v-else>{{ row.MANAGER_DEPARTMENT_NAME }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="DEPARTMENT_NAME" label="监管部门" show-overflow-tooltip >
        <template slot-scope="{row}">
          <span v-if="row.Q_DEPARTMENT_NAME"> {{ row.Q_DEPARTMENT_NAME }} </span>
          <span v-else>{{ row.DEPARTMENT_NAME }}</span>
        </template>
      </el-table-column>
      <el-table-column label="监理单位" show-overflow-tooltip >
        <template slot-scope="{row}">
          <span v-if="row.MANAGE_CORPS_NAME"> {{ row.MANAGE_CORPS_NAME }} </span>
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column prop="STATE" label="状态" width="100" >
        <template slot-scope="{row}">
          <span v-if="row.STATE == 1">进行中</span>
          <span v-if="row.STATE == 2">已结束</span>
        </template>
      </el-table-column>
      <el-table-column prop="OUTSOURCED_CREATOR_NAME" label="创建人" show-overflow-tooltip />
      <el-table-column label="操作" align="center" width="400">
        <template slot-scope="{row}">
          <el-button type="success" icon="el-icon-view" size="mini" @click="goDetail(row.OUTSOURCED_ID)">查看</el-button>
          <el-button type="success" icon="el-icon-view" size="mini" @click="handleJie(row.OUTSOURCED_ID)">结束</el-button>
          <el-button v-show="edit" v-if="0 === row.IS_CORP_TYPE" type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(row.OUTSOURCED_ID)">编辑</el-button>
          <el-button v-show="del" v-if="0 === row.IS_CORP_TYPE" type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(row.OUTSOURCED_ID)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button v-show="add" type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
        <!--<el-button v-show="del" type="danger" icon="el-icon-delete" plain @click="batchDel">删除</el-button>-->
        <!--        <el-button type="primary" plain @click="erweiCode">重点项目登录页</el-button>-->
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <el-dialog :visible.sync="dialogFormShow" title="查看二维码" width="680px" class="top-dialog">
      <div id="printTest" style="border: 1px solid #fff;">
        <div class="table-qrcode">
          <vue-qr :text="qrcodeStr" :margin="0" :size="300" color-dark="#000" color-light="#fff" />
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormShow = false">关 闭</el-button>
        <el-button v-print="'#printTest'" type="primary">打 印</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
import SelectTree from '@/components/SelectTree'
import vueQr from 'vue-qr'
export default {
  components: { Pagination, SelectTree, vueQr },
  directives: { waves },
  data() {
    return {
      qrcodeStr: '',
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 20
      },
      total: 0,
      KEYWORDS: '',
      UNITS_ID: '',
      INVOLVING_CORP: '',
      DEPARTMENT_ID: '',
      COMPETENT_DEPT_ID: '',
      GROUP_UNIT: '',
      CORP_NAME: '',
      CONTRACTTIME: ['', ''], // 区间条件
      CONTRACT_STIME: '',
      CONTRACT_ETIME: '',
      STATE: '',
      varList: [],
      dialogFormShow: false,
      pd: [],
      unitsList: [],
      corpList: [],
      mangeList: [],
      groupCorpList: [],
      treeDataInspectDept: [],
      treeDeptOptions: [],
      defaultProps: {
        value: 'id',
        children: 'nodes',
        label: 'name'
      },
      seachform: {
        KEYWORDS: '',
        CORP_NAME: '',
        UNITS_ID: '',
        COMPETENT_DEPT_ID: ''
      },
      form: {
        OUTSOURCED_NAME: '', // 重点工程名称
        CONTRACT_STIME: '', // 合同起止时间
        CONTRACT_ETIME: '',
        UNITS_ID: '', // 相关方单位名称
        UNITS_PIC: '', // 相关方单位本项目负责人
        APPLICANT: '', // 申请人
        STATE: ''// 状态
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      dialogFormEdit: false,
      dialogType: 'add',
      stateList: [
        { ID: '1', NAME: '进行中' },
        { ID: '2', NAME: '已结束' }
      ]
    }
  },
  watch: {
    CONTRACTTIME: function(newData, oldData) {
      if (!newData) {
        this.CONTRACTTIME = ['', '']
        this.getQuery()
      }
    }
  },
  created() {
    this.getList()
    this.getUnitsList()
    this.getCorpList()
    this.selectDeptOption()
  },
  methods: {
    getRowKey(row) {
      return row.OUTSOURCED_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.UNITS_ID = ''
      this.STATE = ''
      this.CONTRACT_STIME = ''
      this.CONTRACT_ETIME = ''
      this.CONTRACTTIME = ['', '']
      this.$refs.deptTree.clearHandle()
      this.$refs.deptTree1.clearHandle()
      this.INVOLVING_CORP = ''
      this.DEPARTMENT_ID = ''
      this.COMPETENT_DEPT_ID = ''
      this.GROUP_UNIT = ''
      this.CORP_NAME = ''
      this.getQuery()
    },
    erweiCode() {
      this.dialogFormShow = true
      // https://qgjg.qhdsafety.com/xgf/#/login
      this.qrcodeStr = 'https://skqhdg.porthebei.com:9005/xgf/#/login'
      console.info(this.qrcodeStr)
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/outsourced/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          UNITS_ID: this.UNITS_ID,
          CONTRACT_STIME: this.CONTRACTTIME[0],
          CONTRACT_ETIME: this.CONTRACTTIME[1],
          STATE: this.STATE,
          CORPINFO_ID: this.$parent.CORPINFO_ID,
          INVOLVING_CORP: this.INVOLVING_CORP,
          DEPARTMENT_ID: this.DEPARTMENT_ID,
          COMPETENT_DEPT_ID: this.COMPETENT_DEPT_ID,
          GROUP_UNIT: this.GROUP_UNIT,
          CORP_NAME: this.CORP_NAME
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.$parent.activeName = 'Edit'
      this.$parent.OUTSOURCED_ID = ''
    },
    // 查看
    goDetail(ID) {
      this.$parent.OUTSOURCED_ID = ID
      this.$parent.activeName = 'Info'
    },
    // 修改
    handleEdit(ID) {
      this.$parent.activeName = 'Edit'
      this.$parent.OUTSOURCED_ID = ID
    },
    // 保存
    // confirm() {
    //   this.$refs.form.validate(valid => {
    //     if (valid) {
    //       this.listLoading = true
    //       requestFN(
    //         '/outsourced/' + this.dialogType,
    //         this.form
    //       ).then((data) => {
    //         this.listLoading = false
    //         this.dialogFormEdit = false
    //         this.getList()
    //       }).catch((e) => {
    //         this.listLoading = false
    //       })
    //     } else {
    //       return false
    //     }
    //   })
    // },
    handleJie(id) {
      this.$confirm('确定要结束吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/outsourced/jie',
          {
            OUTSOURCED_ID: id
          }
        ).then((data) => {
          if (data.code == '500') {
            this.listLoading = false
            this.$message({
              message: data.msg,
              type: 'error'
            })
            return
          }
          this.$message({
            message: '已结束',
            type: 'success'
          })
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/outsourced/delete',
          {
            OUTSOURCED_ID: id
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    getUnitsList() {
      requestFN(
        '/units/listAll',
        { UNITS_ID: this.form.UNITS_ID }
      ).then((data) => {
        this.unitsList = data.varList
      }).catch((e) => {
      })
    },
    getCorpList() {
      requestFN(
        '/corpinfo/listAll',
        {}
      ).then((data) => {
        this.corpList = data.varList
        this.mangeList = data.varList
        if (!this.$parent.OUTSOURCED_ID) {
          this.form.MANAGE_NAME = ['133a982a3e93464a9b1de25fe6dd4948']
        }
        for (let i = 0; i < data.varList.length; i++) {
          const corpId = data.varList[i].CORPINFO_ID
          if (corpId === '1e6dbbe16004402f8d2c0e52afd9a676' ||
            corpId === '3a854eefa7894e06aaa1a2611bca80f6' ||
            corpId === '020578a4c1f04bc692ee25145c2efbe5' || corpId === '90966974de3c4b83aca6f8fd6432d5c2') {
            this.groupCorpList.push(data.varList[i])
          }
        }
      }).catch((e) => {
      })
    },
    selectDeptOption() {
      // 合并
      return new Promise((resolve) => {
        requestFN(
          '/corpDepartment/listTreeManageAndCorp1',
        ).then((data) => {
          this.treeDeptOptions = this.listTransTree(JSON.parse(data.zTreeNodes), 'id', 'pId', 'nodes')
          this.treeDataInspectDept = this.listTransTree(JSON.parse(data.zTreeNodes), 'id', 'pId', 'nodes')
          resolve()
        }).catch((e) => {
        })
      })
    },
    batchDel() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要删除的项...',
          type: 'error'
        })
        return false
      }
      const ids = _selectData.map((item, index) => {
        return item.OUTSOURCED_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/outsourced/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton: function() {
      var keys = 'outsourced:add,outsourced:del,outsourced:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.outsourcedfhadminadd		// 新增权限
        this.del = data.outsourcedfhadmindel		// 删除权限
        this.edit = data.outsourcedfhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    back() {
      this.$parent.CORPINFO_ID = ''
      this.$parent.activeName = 'OutSourced'
    },
    // 获取数据字典数据
    getDict: function() {
    },
    resetForm() {
      this.form = {
        DEPARTMENT_ID: '', // 发包部门
        CONTRACT_PIC: '', // 发包部门负责人
        OUTSOURCED_NAME: '', // 重点工程名称
        STARTTIME: '', // 计划工期开始时间
        ENDTIME: '', // 计划工期结束时间
        CONTRACT_STIME: '', // 合同起止时间
        CONTRACT_ETIME: '',
        UNITS_ID: '', // 相关方单位名称
        CONTRACT_NUM: '', // 合同号
        UNITS_APTITUDE: '', // 相关方单位资质
        UNITS_PIC: '', // 相关方单位本项目负责人
        UNITS_PHONE: '', // 相关方单位本项目负责人手机
        MANAGE_NAME: '', // 监理单位名称
        MANAGE_APTITUDE: '', // 监理单位资质
        MANAGE_PIC: '', // 监理单位本项目负责人姓名
        MANAGE_PHONE: '', // 监理单位本项目负责人手机
        MEASURES: '', // 安全技术措施
        ATTACHMENTS: '', // 附件
        REMARKS: '', // 备注
        TRAINING_NAME: '', // 培训管理单位
        TRAINING_PIC: '', // 培训管理负责人
        TRAINING_STIME: '', // 培训时间
        TRAINING_ETIME: '', // 培训时间
        RECORDS: '', // 安全培训记录
        SHEET: '', // 培训签名表
        EXAMINATION: '', // 重点工程考试题
        OATTACHMENTS: '', // 其他附件
        APPLICANT: '', // 申请人
        STATE: ''// 状态
      }
    }
  }
}

</script>
<style lang="sass" scoped>
  .table-qrcode
    text-align: center
    padding-top: 20px
    width: 100%
    .filter-container
      position: relative
      .filter-flot
        position: absolute
        right: 0
        top: 0
    .uploader
      width: 570px
      display: flex
      align-items: center
    .el-form-item__content
      line-height: 1
</style>

