<template>
  <div class="examination">
    <div class="common-head-search">
      <el-form :inline="true" @submit.native.prevent>
        <el-form-item label='制样点名称'>
          <el-input v-model="search.pointName" size="small" @keyup.enter.native="searchData()" placeholder="请输入查询内容" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="goon" size="mini" @click="searchData">查询</el-button>
          <el-button icon="el-icon-refresh" type="info" plain size="mini" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-title-div">
      <div class="common-title-name">
        <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/logo.png' />
        制样点信息
      </div>
      <div>
      <el-button icon="el-icon-circle-plus-outline" type="goon" size="mini" @click="dialogTableVisible = true">新增</el-button>
      <el-button icon="el-icon-edit" type="goon" size="mini" @click="modify">修改</el-button>
      <el-button icon="el-icon-circle-close" type="danger" size="mini" @click="deleteData">停用</el-button>
      <el-button icon="el-icon-check" type="goon" size="mini" @click="recoverData">启用</el-button>
      </div>
    </div>
    <div class="common-table-div">
       <el-table v-loading="dataLoading" ref="dataTable" border :height="height_top" highlight-current-row @selection-change="handleSelectionChange" :data="table.list" :icore-filter-flag="icoreFilterFlag"
       :header-cell-style="tableHeaderCellStyle" :row-class-name="tableRowClassName" :summary-method="getSummaries" show-summary>
        <el-table-column type="index" label="NO" fixed="left" align="center">
        </el-table-column>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="pointNo" label="制样点代码" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="pointName" label="制样点名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="companyName" label="公司名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="deptName" label="部门名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="sectionName" label="班组名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="90px"  prop="validFlag1" label="数据状态" align="center"></el-table-column>
        <el-table-column sortable min-width="80px" prop="memo" :show-overflow-tooltip="true" label="备注" align="center"> </el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="165px" prop="createTime" label="创建时间" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="createMan" label="创建人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="165px" prop="updateTime" label="修改时间" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="95px" prop="updateMan" label="修改人" align="center"></el-table-column>
      </el-table>
    </div>
    <div class="common-foot-style">
        <el-pagination v-loading="dataLoading" @size-change="pageSizeChange" @current-change="pageCurrentChange"
          :current-page="page.pageNum" :page-sizes="[10, 20, 50, 100, 200]" :page-size="page.pageSize" layout="total, sizes, prev, pager, next, jumper"
          :total="page.totalPage" background>
        </el-pagination>
    </div>
    <el-dialog @close="closeDialog('baseData')" :visible.sync="dialogTableVisible">
        <div slot="title" class="titleBox">
        <i :class="tableType === 0? 'el-icon-document':'el-icon-edit-outline'" style="font-size: 20px"></i>
        <!-- <img style="width: 25px; height: 25px" src='../../../../assets/img/imgScreen/more.png' /> -->
        <span style="margin-left:10px;font-size:18px">{{tableTypeDsc}}</span>
        </div>
       <el-form ref="baseData" label-width="80px" :rules="ruleValidate" :model="baseData">
         <el-row>
          <el-col :span="24">
            <el-form-item prop="pointName" label="制样点名称">
              <el-input v-model="baseData.pointName" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
          <el-row>
           <el-col :span="24">
            <el-form-item prop="companyNo" label="公司名称">
              <el-select v-model="baseData.companyNo" filterable style="width:100%" collapse-tags @change="selectDepartment" size="small">
                <el-option v-for="item in companyNameType" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="deptNo" label="部门名称">
             <el-select v-model="baseData.deptNo" filterable clearable style="width:100%" @change="selectClass" collapse-tags size="small" >
                <el-option v-for="item in sectionNameType" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item prop="deptName" label="班组名称">
             <el-select v-model="baseData.sectionNo" clearable style="width:100%" filterable collapse-tags size="small">
                <el-option v-for="item in sectionClass" :key="item.orgCode" :label="item.orgName" :value="item.orgCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         </el-row>
         <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="baseData.memo" size="small"></el-input>
            </el-form-item>
          </el-col>
         </el-row>
       </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button type="goon" size="mini" icon="el-icon-check" @click="tableType === 0 ? addData('baseData'):modifyData('baseData')">确定</el-button>
        <el-button size="mini" type="goon" icon="el-icon-remove-outline" @click="cancel">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getCookie
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
import alertComponets from './alertComponents';
let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);
export default {
  components: {alertComponets},
  data () {
    return {
      table: {
        list: []
      },
      dataLoading: false,
      icoreFilterFlag: true,
      tableType:0,
      tableTypeDsc:'新增制样点',
      companyNameType:[],
      sectionNameType:[],
      sectionClass:[],
      page: {
        pageSize: 50,
        pageNum: 1,
        totalPage: 0
      },
      dialogTableVisible:false,
      baseData:{},
      ruleValidate: {
        pointName: [{ required: true, message: '该项不能为空', trigger: 'change' }],
        companyNo: [{ required: true, message: '该项不能为空', trigger: 'change' }],
        deptNo: [{ required: true, message: '该项不能为空', trigger: 'change' }]
      },
      companyNameType:[],
      sectionNameType:[],
      deptNameType:[],
      showFlag: false,
      Params: {},
      multipleSelection: [],
      search: {
        pointNo: '',
        pointName: ''
      },
      height_top:0,
    }
  },
  created() {
    this.height_top = window.innerHeight - 190;
  },
 mounted () {
  //获取公司
  this.axios.post("pass/baseManagement/v1/sysorgs/querySysOrgCompanyAll",{}, {individualType: 'json'}).then(response => {
    this.companyNameType = response.data
  });
  this.getDataList();
  this.addScreen()
  },
  methods: {
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
        }
        if (index === 1) {
          sums[index] = data.length;
        } 
      });
      return sums;
    },
    getCode (code, arr, codeKey, nameKey) {
        if (!code) {
          return '';
        }
        let targetRow = arr.find(item => {
          return item[codeKey] === code;
        });
        // console.log(targetRow, 'targetRow');
        return targetRow ? targetRow[nameKey] : '';
      },
    //根据选中的公司，查询部门名称数据
    selectDepartment(val){
      this.sectionNameType = []
      this.companyNameType.find(item =>{
        if(val === item.orgCode){
          this.sectionNameType = item.sysOrgList
        }
      })
    },
    //根据选中的部门名称，查询班组名称
    selectClass(val){
      this.sectionClass = []
      this.sectionNameType.find(item =>{
        if(val === item.orgCode){
          this.sectionClass = item.sysOrgList
        }
      })
    },
    refresh () {
      this.getDataList();
    },
    //关闭弹窗
    closeDialog (formName){
      this.$refs[formName].resetFields();
      this.baseData = {}
      this.tableType = 0
      this.tableTypeDsc = '新增制样点'
    },
    //取消
    cancel () {
      this.dialogTableVisible = false
      this.baseData = {}
      this.tableType = 0
      this.tableTypeDsc = '新增制样点'
    },
    addData (formName) {
        let _this = this;
        _this.tableData = [];
         _this.$refs[formName].validate((valid) => {
          if (valid) {
             _this.baseData.createMan = userName;
             _this.baseData.createNo = userId;
             _this.baseData.pointTypeCode = 480402;
             _this.baseData.pointTypeName = '制样';
             _this.baseData.companyName = this.getCode(this.baseData.companyNo,this.companyNameType,'orgCode','orgName')
             _this.baseData.deptName = this.getCode(this.baseData.deptNo,this.sectionNameType,'orgCode','orgName')
             _this.baseData.sectionName = this.getCode(this.baseData.sectionNo,this.sectionClass,'orgCode','orgName')
             _this.axios.post('pass/baseManagement/v1/limsbasepoints/', [_this.baseData], {individualType: 'json'}).then(res => {
                if (res.succeed) {
                  _this.dialogTableVisible = false;
                 this.$message.success('添加成功')
                 this.getDataList();
                } else {
                 this.$message.error(res.message)
                }
                }).catch(e => {
                 this.$message.error(e.message)
               })
         }
       })
      },
    //修改弹窗
    modify () {
      if (this.multipleSelection.length !== 1) {
        this.$message.warning('请勾选您要修改的一条数据！')
        return
      }
      this.tableType = 1
      this.tableTypeDsc = '修改制样点'
      this.baseData = Object.assign({},this.multipleSelection[0])
      this.dialogTableVisible = true
      this.selectDepartment(this.baseData.companyNo)
      this.selectClass(this.baseData.deptNo)
    },
    modifyData (formName) {
        let _this = this;
        _this.tableData = [];
        _this.$refs[formName].validate((valid) => {
        if (valid) {
        _this.baseData.updateMan = userName;
        _this.baseData.updateNo = userId;
        _this.baseData.companyName = this.getCode(this.baseData.companyNo,this.companyNameType,'orgCode','orgName')
        _this.baseData.deptName = this.getCode(this.baseData.deptNo,this.sectionNameType,'orgCode','orgName')
        _this.baseData.sectionName = this.getCode(this.baseData.sectionNo,this.sectionClass,'orgCode','orgName')
        _this.axios.put('pass/baseManagement/v1/limsbasepoints/', _this.baseData, {individualType: 'json'}).then(res => {
           if (res.succeed) {
            this.$message.success('修改成功')
            _this.dialogTableVisible = false;
           this.getDataList();
           } else {
            this.$message.error(res.message)
           }
           }).catch(e => {
            this.$message.error(e.message)
          })
        }
        })
    },
    recoverData () {
       let _this = this;
        if (_this.multipleSelection.length < 1) {
        this.$message.warning('请勾选您要启用的数据！')
      } else {
        _this.dialogTableVisible = false;
        for (var i = 0; i < _this.multipleSelection.length; i++) {
        _this.multipleSelection[i].updateMan = userName;
        _this.multipleSelection[i].updateNo = userId
        }
        _this.axios.post('pass/baseManagement/v1/limsbasepoints/recover', _this.multipleSelection, {individualType: 'json'}).then(res => {
        if (res.succeed) {
         this.$message.success('启用成功');
         this.getDataList();
        } else {
         this.$message.error(res.message)
        }
        }).catch(e => {
         this.$message.error(e.message)
       })
      }
    },
    deleteData () {
       let _this = this;
        if (_this.multipleSelection.length < 1) {
        this.$message.warning('请勾选您要停用的数据！')
      } else {
        _this.dialogTableVisible = false;
        for (var i = 0; i < _this.multipleSelection.length; i++) {
        _this.multipleSelection[i].updateMan = userName;
        _this.multipleSelection[i].updateNo = userId
        }
        _this.axios.post('pass/baseManagement/v1/limsbasepoints/abolish', _this.multipleSelection, {individualType: 'json'}).then(res => {
        if (res.succeed) {
         this.$message.success('停用成功');
         this.getDataList();
        } else {
         this.$message.error(res.message)
        }
        }).catch(e => {
         this.$message.error(e.message)
       })
      }
    },
    searchData () {
      this.page.pageNum = 1;
      this.getDataList()
    },
    reset () {
      let _this = this;;
      _this.search.pointName = '';
      this.page.pageNum = 1
      this.page.pageSize = 50
      _this.searchData();
    },
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    pageSizeChange (size) {
      this.page.pageSize = size;
      this.getDataList();
    },
    pageCurrentChange (index) {
      this.page.pageNum = index;
      this.getDataList();
    },
    addScreen () {
      this.$nextTick(() => {
          let ss = document.createElement('div');
          ss.innerHTML = '筛选';
          ss.style = 'text-align: center;'
          this.$refs.dataTable.$el.getElementsByClassName('el-table__fixed')[0].getElementsByTagName('td')[0].appendChild(ss);
       });
    },
    getDataList () {
      this.dataLoading = true;
      let obj ={
        'object':{'pointName':this.search.pointName,pointTypeCode:'480402'},
        'pageIndex':this.page.pageNum,
        'pageSize':this.page.pageSize
      }
      this.axios.post('pass/baseManagement/v1/limsbasepoints/queryLimsBasePointPage/',obj, {individualType: 'json'}).then(res => {
        if (res.code == 200 || res.code == 0) {
          this.table.list = res.data.list;
          this.table.list.forEach(item =>{
            item.validFlag1 = item.validFlag === '0' ? '无效' : item.validFlag === '1' ? '有效' : ''
          })
          this.page.totalPage = res.data.total;
        }else{
          this.$message.error(res.message)
        }
        this.dataLoading = false;
      }).catch(e => {
        this.dataLoading = false;
        this.$message.error(res.message)
      });
    },
    tableRowClassName ({row, column, rowIndex, columnIndex}) {
      if (row.validFlag !== '1') {
        return 'error-row';
      }
      return '';
    },
    tableHeaderCellStyle () {
      return 'color: rgb(255,255,255);border-bottom: 1px solid #DCDFE7;'
    }
  }
}
</script>
<style  scoped>
</style>
