<script>
import {queryOperationForResourceList,
  saveOperationForResource,
  queytOperationList,
  getSiteAndBuByUserName,
  queryEquipmentFolderLocationList,
  deleteEquipmentFolderLocationList,
  getSuffixList,
  saveEquipmentFolderLocationList
} from "@/api/qc/qc.js"
import excel from "@/utils/excel-util.js";
import Chooselist from '@/views/modules/common/Chooselist_eam'
export default {
  name: 'OperationForResource',
  components: {
    Chooselist
  },
  data () {
    return {
      queryParams: {
        page: 1,
        pageCount: 50,
        bu:'',
        operation: '',
        resource: ''
      },
      exportName: "外采设备维护" + this.dayjs().format('YYYYMMDDHHmmss'),
      dataList: [],
      dataListSelections: [],
      total: 0,
      modalFlag: false,
      resourceFlag:false,
      ifexist:0,
      resourData:{
        resourceId:'',
        resourceDesc:''
      },
      operationList:[],
      suffixList:[],
      userBuList: [],
      editFlag: false,
      viewFlag: false,
      modalData: {
        page: 1,
        pageCount: 50,
        equipmentNo: '',
        equipmentDesc: '',
      },
      columns: [
        {
          userId: this.$store.state.user.name,
          functionId: 200101106,
          serialNumber: '200101106Table1Operation',
          tableId: '200101106Table1',
          tableName: '用户默认工序设置表',
          columnProp: 'buDesc',
          headerAlign: 'center',
          align: 'center',
          columnLabel: 'BU',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200101106,
          serialNumber: '200101106Table1Operation',
          tableId: '200101106Table1',
          tableName: '用户默认工序设置表',
          columnProp: 'equipmentNo',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '设备编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200101106,
          serialNumber: '200101106Table1Operation',
          tableId: '200101106Table1',
          tableName: '用户默认工序设置表',
          columnProp: 'equipmentDesc',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '设备名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200101107,
          serialNumber: '200101107Table1Resource',
          tableId: '200101107Table1',
          tableName: '用户默认工序设置表',
          columnProp: 'fileNo',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '采集ID',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200101107,
          serialNumber: '200101107Table1Resource',
          tableId: '200101107Table1',
          tableName: '用户默认工序设置表',
          columnProp: 'ip',
          headerAlign: 'center',
          align: 'center',
          columnLabel: 'IP',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200101107,
          serialNumber: '200101107Table1Resource',
          tableId: '200101107Table1',
          tableName: '用户默认工序设置表',
          columnProp: 'suffix',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '采集方式',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200101107,
          serialNumber: '200101107Table1Resource',
          tableId: '200101107Table1',
          tableName: '用户默认工序设置表',
          columnProp: 'batchDate',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '每批次时间(s)',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200101107,
          serialNumber: '200101107Table1Resource',
          tableId: '200101107Table1',
          tableName: '用户默认工序设置表',
          columnProp: 'createBy',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '创建人',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200101107,
          serialNumber: '200101107Table1Resource',
          tableId: '200101107Table1',
          tableName: '用户默认工序设置表',
          columnProp: 'createTime',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '创建时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200101107,
          serialNumber: '200101107Table1Resource',
          tableId: '200101107Table1',
          tableName: '用户默认工序设置表',
          columnProp: 'updateBy',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '变更人',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 200101107,
          serialNumber: '200101107Table1Resource',
          tableId: '200101107Table1',
          tableName: '用户默认工序设置表',
          columnProp: 'updateTime',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '变更时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        }
      ]
    }
  },
  methods: {
    handleQuery () {
      queryEquipmentFolderLocationList(this.queryParams).then(({data}) => {
        if (data.code==0) {
          this.dataList = data.rows
          this.total = data.total
        }
      }).catch((errer) => {
        this.$message.error(errer)
      })
    },
    // 获取用户的bu
    getSiteAndBuByUserName () {
      let tempData = {
        username: this.$store.state.user.name,
      }
      getSiteAndBuByUserName(tempData).then(({data}) => {
        if (data.code === 0) {
          this.userBuList = data.rows
          this.$set(this.queryParams, 'bu', this.userBuList[0].buNo)
          //查询数据
          this.handleQuery()
        }
      })
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    addDialog () {
      this.editFlag = false
      this.viewFlag = false
      this.modalData = {
        page: 1,
        pageCount: 50,
        editFlag: 0,
        equipmentNo: '',
        equipmentDesc: '',
        createBy: this.$store.state.user.name
      }
      this.modalFlag = true
    },
    editData(row){
      this.editFlag = true
      this.viewFlag = false
      this.modalData = row
      this.modalData.bu = row.site + '_' + row.buNo
      this.modalData.editFlag = 1
      this.modalData.updateBy = this.$store.state.user.name
      this.modalFlag = true
    },
    viewData(row){
      this.viewFlag = true
      this.modalData = row
      this.modalData.bu = row.site + '_' + row.buNo
      this.modalFlag = true
    },
    // 获取基础数据列表S
    getBaseList (val,type) {
      this.tagNo = val
      this.$nextTick(() => {
        let strVal = ''
        let conSql = ''
        if (val === 200) {
          strVal = this.modalData.equipmentNo
          if (!this.modalData.bu || this.modalData.bu === null || this.modalData.bu === '') {
            // this.$message.success("99999999")
            this.$message.warning("请选择BU!")
            return
          } else {
            this.$message.success("99999999")
            conSql = " and bu_no = '" + this.modalData.bu.split('_')[1] + "'"
            this.$refs.baseList.init(val, strVal, conSql)
          }
        }
      })
    },
    // 列表方法的回调
    getBaseData (val) {
      if (this.tagNo === 200) {
        this.modalData.equipmentNo = val.ObjectID
        this.modalData.equipmentDesc = val.ObjectDesc
      }
    },
    async saveModalData(){
      if (!this.modalData.bu||this.modalData.bu==null ||this.modalData.bu==''){
        this.$message.warning("请选择BU！")
        return
      }
      if (this.modalData.equipmentNo==null ||this.modalData.equipmentNo==''){
        this.$message.warning("请输入设备编码！")
        return
      }
      saveEquipmentFolderLocationList(this.modalData).then(({data})=>{
        if (data.code==0){
          this.modalFlag = false
          this.$message.success(data.msg)
          this.handleQuery()
        } else {
          this.$message.error(data.msg)
        }
      }).catch()

    },
    getSuffixList(){
      getSuffixList().then(({data})=>{
        if (data && data.code === 0) {
          this.suffixList = data.rows
        }
      }).catch((error)=>{
        console.log(error)
      })
    },
    queryOperationList(){
      queytOperationList().then(({data})=>{
        if (data && data.code === 0) {
          this.operationList = data.rows
        }
      }).catch((error)=>{
        console.log("失败")
        console.log(error)
      })
    },
    deleteData(){
      if (!this.dataListSelections || this.dataListSelections.length === 0) {
        this.$message.warning("请选择要删除的记录")
        return
      }
      this.$confirm(`确认删除选中的` + this.dataListSelections.length +'条记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        deleteEquipmentFolderLocationList(this.dataListSelections).then(({data})=>{
          if (data.code==0){
            this.$message.success("删除成功")
            this.handleQuery()
          }
        }).catch()
      })
    },
    async exportExcel() {
      this.queryParams.page = 1
      this.queryParams.pageCount = -1
      excel.exportTable({
        url: "/pms/qc/queryEquipmentFolderLocationList",
        columnMapping: this.columns,//可以直接用table，不需要的列就剔除
        mergeSetting: [],//需要合并的列
        params: this.queryParams,
        fileName: this.exportName+".xlsx",
        rowFetcher: res => res.data,
        columnFormatter: [],
        dropColumns: [],//需要剔除的列，例如dropColumns: ["netWeight"]，即剔除净重列
      })
    }
  },
  created () {
    // 获取用户的 site 和 bu
    this.getSiteAndBuByUserName()
    //查询工序类别信息
    this.queryOperationList()
    this.getSuffixList()
  }
}
</script>

<template>
  <div class="box-container">
    <!--  查询条件-->
    <el-form :inline="true" label-position="top" :model="queryParams">
      <el-form-item :label="'BU'">
        <el-select v-model="queryParams.bu" placeholder="请选择"  style="width: 80px">
          <el-option
            v-for = "i in userBuList"
            :key = "i.buNo"
            :label = "i.buDesc"
            :value = "i.buNo">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="'设备编码'">
        <el-input v-model="queryParams.equipmentNo" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'设备名称'">
        <el-input v-model="queryParams.equipmentDesc" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'IP'">
        <el-input v-model="queryParams.ip" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'采集方式'">
        <el-input v-model="queryParams.suffix" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button  type="primary" @click="addDialog()">新增</el-button>
        <el-button  type="primary" @click="deleteData()">删除</el-button>
        <el-button @click="exportExcel()" type="primary" style="margin-left: 2px">{{'导出'}}</el-button>
      </el-form-item>
    </el-form>
    <!--  主表信息-->
    <div style="height: 90%">
      <el-table :data="dataList" border
                highlight-current-row
                @selection-change="selectionChangeHandle"
                height="100%" ref="table">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          v-for="(item,index) in columns" :key="index"
          :sortable="item.columnSortable"
          :prop="item.columnProp"
          :header-align="item.headerAlign"
          :show-overflow-tooltip="item.showOverflowTooltip"
          :align="item.align"
          :fixed="item.fixed===''?false:item.fixed"
          :min-width="item.columnWidth"
          :label="item.columnLabel">
        </el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope">
            <a @click="editData(scope.row)" type="primary" style="margin-left: 2px">{{'编辑'}}</a>
            <a @click="viewData(scope.row)" type="primary" style="margin-left: 2px">{{'详情'}}</a>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--  分页信息-->
    <el-pagination
      style="margin-top: 5px"
      :current-page="queryParams.page"
      :page-sizes="[50, 100, 500, 1000]"
      :page-size="queryParams.pageCount"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!--  新增数据dialog-->
    <el-dialog :title="!editFlag?'新增':'编辑'" :visible.sync="modalFlag" width="620px"
               :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :inline="true" label-position="top" :model="modalData"  style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="BU" prop="bu">
          <el-select v-model="modalData.bu" placeholder="请选择" :disabled="editFlag"  style="width: 221px">
            <el-option
              v-for = "i in userBuList"
              :key = "i.buNo"
              :label = "i.sitename"
              :value = "i.buNo">
              <span style="float: left;width: 100px">{{ i.sitename }}</span>
              <span style="float: right; color: #8492a6;white-space:nowrap;overflow:hidden;text-overflow:ellipsis; font-size: 11px;width: 60px">
                {{ i.buDesc }}
              </span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备编码">
          <span style="cursor: pointer" slot="label" @click="getBaseList(200)" v-if="!editFlag"><a>设备编码</a></span>
          <el-input v-model="modalData.equipmentNo" disabled style="width: 100px"></el-input>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input v-model="modalData.equipmentDesc" disabled style="width: 221px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="modalData" :disabled="viewFlag" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="IP">
          <el-input v-model="modalData.ip"  style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item label="端口">
          <el-input v-model="modalData.port"  style="width: 100px"></el-input>
        </el-form-item>
        <el-form-item label="采集方式">
<!--          <el-input v-model="modalData.suffix" disabled style="width: 104px"></el-input>-->
<!--          suffixList-->
          <el-select v-model="modalData.suffix" placeholder="请选择"  style="width: 104px">
            <el-option
              v-for = "i in suffixList"
              :key = "i"
              :label = "i"
              :value = "i">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="采集批次时间">
          <el-input v-model="modalData.batchDate"  style="width: 104px"></el-input>
        </el-form-item>
        <el-form-item label="采集ID">
          <el-input v-model="modalData.fileNo"  style="width: 104px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="modalData" :disabled="viewFlag" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="文件解析路径">
<!--          <span style="cursor: pointer" slot="label" @click="selectServerFolder()"><a>文件解析路径</a></span>-->
          <el-input v-model="modalData.folderPath"  style="width: 570px"></el-input>
        </el-form-item>
        <el-form-item label="文件备份路径">
          <el-input v-model="modalData.backupFolderPath"  style="width: 570px"></el-input>
        </el-form-item>
        <el-form-item label="图片备份路径">
          <el-input v-model="modalData.backupPhotoPath"  style="width: 570px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="modalData" :disabled="viewFlag" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="寄存器地址">
          <el-input v-model="modalData.registerAddress"  style="width: 133px"></el-input>
        </el-form-item>
        <el-form-item label="寄存器位数">
          <el-input v-model="modalData.registerNum"  style="width: 133px"></el-input>
        </el-form-item>
        <el-form-item label="设备报警声音标识">
          <el-select v-model="modalData.buzzer"  style="width: 133px">
            <el-option value="1" label="Y"></el-option>
            <el-option value="0" label="N"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型">
          <el-select v-model="modalData.repairType"  style="width: 133px">
            <el-option value="短信" label="短信"></el-option>
            <el-option value="邮件" label="邮件"></el-option>
            <el-option value="随身机" label="随身机"></el-option>
            <el-option value="三色灯" label="三色灯"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="modalData" :disabled="viewFlag" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="寄存器数量类型">
          <el-input v-model="modalData.registerNumColumntype"  style="width: 133px"></el-input>
        </el-form-item>
        <el-form-item label="寄存器地址类型">
          <el-input v-model="modalData.registerAddressInspection"  style="width: 133px"></el-input>
        </el-form-item>
        <el-form-item label="寄存器读取开始位">
          <el-input v-model="modalData.registerNumInspection"  style="width: 133px"></el-input>
        </el-form-item>
        <el-form-item label="寄存器检验单号类型">
          <el-input v-model="modalData.registerNumInspectionColumntype"  style="width: 133px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="modalData" :disabled="viewFlag" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="单位数量">
          <el-input v-model="modalData.unitId"  style="width: 133px"></el-input>
        </el-form-item>
        <el-form-item label="文件类型读取行特殊标识">
          <el-input v-model="modalData.excelCompareFieldsX"  style="width: 133px"></el-input>
        </el-form-item>
        <el-form-item label="文件读取列位数">
          <el-input v-model="modalData.excelCompareFieldsY"  style="width: 133px"></el-input>
        </el-form-item>
        <el-form-item label="文件从多少行开始读取">
          <el-input v-model="modalData.initialLine"  style="width: 133px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="modalData" :disabled="viewFlag" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="文件读取行数">
          <el-input v-model="modalData.dataRow"  style="width: 133px"></el-input>
        </el-form-item>
        <el-form-item label="文件读取跳过行数">
          <el-input v-model="modalData.skipLine"  style="width: 133px"></el-input>
        </el-form-item>
        <el-form-item label="数据分割标识">
          <el-input v-model="modalData.cuttingSymbol"  style="width: 133px"></el-input>
        </el-form-item>
        <el-form-item label="设备直连标识">
          <el-select v-model="modalData.transparentFlag"  style="width: 133px">
            <el-option value="Y" label="Y"></el-option>
            <el-option value="N" label="N"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="modalData" :disabled="viewFlag" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="采集是否参与公式计算">
          <el-select v-model="modalData.formulaCalculation"  style="width: 133px">
            <el-option value="Y" label="Y"></el-option>
            <el-option value="N" label="N"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公式计算函数">
          <el-input v-model="modalData.formula"  style="width: 133px"></el-input>
        </el-form-item>
        <el-form-item label="采集数据是否需要分组">
          <el-select v-model="modalData.groupCalculation"  style="width: 133px">
            <el-option value="Y" label="Y"></el-option>
            <el-option value="N" label="N"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="正则表达式">
          <el-input v-model="modalData.regexp"  style="width: 133px"></el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:40px;margin-top: 10px;text-align:center">
        <el-button type="primary" @click="saveModalData()" v-if="!viewFlag">保 存</el-button>
        <el-button @click="modalFlag = false">取 消</el-button>
      </el-footer>
    </el-dialog>

    <!-- chooseList -->
    <Chooselist ref="baseList" @getBaseData="getBaseData"></Chooselist>




  </div>
</template>

<style scoped>

</style>
