<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="到访单位" prop="unitId">
        <!-- <el-input
          v-model="queryParams.unitId"
          placeholder="请输入到访单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        /> -->
        <treeselect
            v-model="queryParams.unitId"
            :options="users"
            placeholder="请选择到访单位"
            class="threeselects"
            @select="selectPeo1"

          />
      </el-form-item>
      <el-form-item label="到访人员姓名" prop="name">
        <el-input
        class="daofang"
          v-model="queryParams.name"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="search">
        <el-button type="btr1" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="btr2" @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="['doormanManage:bdglVisit: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="['doormanManage:bdglVisit: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="['doormanManage:bdglVisit:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['doormanManage:bdglVisit:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bdglVisitList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="接待人姓名" align="center" prop="id" /> -->
      <el-table-column label="到访单位" align="center" prop="unitId" :formatter="unit2Format"/>
      <el-table-column label="接待人" align="center" prop="peopleName"  />
      <el-table-column label="到访人员姓名" align="center" prop="name" />
      <el-table-column label="到访时间" align="center" prop="arriveTime" width="180">
        <template slot-scope="scope">
          <!-- <span>{{ parseTime(scope.row.arriveTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>     -->
          <span>{{ dateFormat('YYYY-mm-dd HH:MM:SS',scope.row.arriveTime) }}</span>
          <!-- rTime -->
        </template>
      </el-table-column>
      <el-table-column label="离队时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <!-- <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span> -->
           <span>{{ dateFormat('YYYY-mm-dd HH:MM:SS',scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到访车牌号" align="center" prop="numberPlate" />
      <el-table-column label="来访原因" align="center" prop="contents"  show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="btu"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['doormanManage:bdglVisit:edit']"
          >修改</el-button>
          <el-button
            size="btd"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['doormanManage:bdglVisit: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="1090px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px"  :inline="true">
        <el-form-item label="到访单位" prop="unitId" style="margin-left:20px">
          <treeselect
            v-model="form.unitId"
            :options="users"
            placeholder="请选择到访单位"
            class="threeselects"
            @select="selectPeo"
          />
          <!-- <el-input v-model="form.unitId" placeholder="请输入到访单位" /> -->
        </el-form-item>
        <el-form-item label="接待人" prop="peopleId" class="jiedai">
          <!-- <el-input v-model="form.peopleId" placeholder="请输入接待人" /> -->
          <el-select
                v-model="form.peopleName"
                placeholder="请选择接待人"
                
                ref="peoplenames"
              >
                <el-option
                  v-for="item in executor"
                  :key="item.id"
                  :label="item.name"
                  :value="item.name"
                  @click.native="peopleIds(item.id)"
                  id="execute"
                />
              </el-select>
        </el-form-item>
        <el-form-item label="到访人员姓名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="到访时间" prop="arriveTime" style="margin-left:20px">
          <el-date-picker clearable size="small"
            v-model="form.arriveTime"
            type="datetime"
            placeholder="请选择到访时间"
            >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="离队时间" prop="endTime">
          <el-date-picker clearable size="small"
            v-model="form.endTime"
            type="datetime"
             placeholder="请选择离队时间"
            >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到访车牌号" prop="numberPlate">
          <el-input v-model="form.numberPlate" />
        </el-form-item>
        <div class="jiben" style="margin-left:20px">来访原因</div>
        <el-form-item  prop="contents" class="daofang" style="margin-left:20px" >
          <el-input v-model="form.contents" type="textarea"  />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBdglVisit, getBdglVisit, delBdglVisit, addBdglVisit, updateBdglVisit, exportBdglVisit, deptUser } from "@/api/doormanManage/bdglVisit";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getDept2 } from "@/api/grassrootsregistration/bdglmeeting";
import {allUser} from "@/api/workingArrangements/workTask"

export default {
  name: "BdglVisit",
  components: { Treeselect },
  computed:{
    
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员来访登记表格数据
      bdglVisitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unitId: null,
        peopleId: null,
        name: null,
        arriveTime: null,
        endTime: null,
        adminId: null,
        contents: null,
        createtime: null,
        updatetime: null,
        numberPlate: null,
        unitName: null,
        peopleName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        unitId: [{ required: true, message: "到访单位不能为空", trigger: "blur" }],
        // peopleId: [{ required: true, message: "接待人不能为空", trigger: "blur" }],
        name: [{ required: true, message: "到访人员姓名不能为空", trigger: "blur" }],
        arriveTime: [{ required: true, message: "到访时间不能为空", trigger: "blur" }],
        endTime: [{ required: true, message: "离队时间不能为空", trigger: "blur" }],
        numberPlate: [{ required: true, message: "到访车牌号不能为空", trigger: "blur" }],
      },
      // 执行单位列表
      users: [],
      // 接待人
      executor:[],
      // 人员列表
      rylist:[]
    };
  },
  created() {
    this.getList();
    allUser().then((res) => {
      this.rylist = res.rows;
    });
    this.selectPeo1()
   
  },
  methods: {
    dateFormat(fmt, date) {
      let ret="";
      date=new Date(date);
      const opt = {
        'Y+': date.getFullYear().toString(), // 年
        'm+': (date.getMonth() + 1).toString(), // 月
        'd+': date.getDate().toString(), // 日
        'H+': date.getHours().toString(), // 时
        'M+': date.getMinutes().toString(), // 分
        'S+': date.getSeconds().toString() // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
      }
      for (let k in opt) {
        ret = new RegExp('(' + k + ')').exec(fmt)
        if (ret) {
          fmt = fmt.replace(
            ret[1],
            ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')
          )
        }
      }
      return fmt
    },
    unit2Format(row, column) {
      console.log(row)
      var deptName = "";
      deptName=row.unitName
      // if(this.bdglVisitList[0].unitId==row.unitId){
      //   deptName=row.unitName
      // }
      return deptName;
    },
    peopleIds(id){
      this.form.peopleId=id
      console.log(id)
    },
    // 获取执行人
    getZhuChi(id) {
      // console.log(id)
      deptUser(id).then((res) => {
        this.executor = res.data;
        // console.log(this.executor)
        // this.form.unitName=this.executor
      });
    },
    // 到访单位选中触发
    selectPeo(val) {
      console.log(val)
      this.form.unitId = val.id;
      this.form.peopleName=""
      this.form.unitName=val.label
      // console.log(this.form.unitId)
      this.getZhuChi(this.form.unitId);
    },
    // 到访单位选中触发
    selectPeo1(val) {
      //  this.getDept2()
      // this.handleAdd()
      // 获取执行单位列表
      getDept2().then((res) => {
        // console.log(res,2)
        this.users = res.data;
        console.log(this.users);
      });
      this.queryParams.unitId = val.id;
      this.queryParams.peopleName=""
      this.queryParams.unitName=val.label
      console.log(this.queryParams)
      this.getZhuChi(this.queryParams.unitId);
    },
    /** 查询人员来访登记列表 */
    getList() {
      this.loading = true;
      listBdglVisit(this.queryParams).then(response => {
        // console.log(response)
        this.bdglVisitList = response.rows;
        console.log(this.bdglVisitList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        unitId: null,
        peopleId: null,
        name: null,
        arriveTime: null,
        endTime: null,
        adminId: null,
        contents: null,
        createtime: null,
        updatetime: null,
        numberPlate: null,
        unitName: null,
        peopleName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams)
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams={}
      this.resetForm("queryForm");
      this.handleQuery();
      this.getList()
    },
    // 多选框选中数据
    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 = "添加人员来访登记";
      // 获取执行单位列表
      getDept2().then((res) => {
        // console.log(res,2)
        this.users = res.data;
        console.log(this.users);
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getDept2().then((res) => {
        // console.log(res)
        // console.log(res,2)
        this.users = res.data;
        console.log(this.users);
      });
      const id = row.id || this.ids
      // console.log(row)
      getBdglVisit(id).then(response => {
        console.log(response.data)
        this.form = response.data;
        this.open = true;
        this.title = "修改人员来访登记";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            console.log(this.form)
            updateBdglVisit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBdglVisit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除人员来访登记编号为"' + ids + '"的数据项？').then(function() {
        return delBdglVisit(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有人员来访登记数据项？').then(() => {
        this.exportLoading = true;
        return exportBdglVisit(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
<style scoped>
::v-deep .el-dialog{
   background-color: #004d86 !important;
}
/* 输入框 */
::v-deep .el-dialog .el-input__inner{
  background-color: #004D86 !important;
  width: 220px;
  color: white;
}
/* 标题弹框 */
::v-deep .el-dialog__title{
  color: white;
  /* border-bottom: 1px solid white; */
}
/* 标题下划线 */
::v-deep .el-dialog__header{
  border-bottom: 1px solid white;
}
::v-deep .el-form-item__content{
  width: 220px;
}
::v-deep .el-form-item__label{
  text-align: end;
}
::v-deep .jiedai .el-form-item__label{
  padding-left: 10px;
}
/* ::v-deep .el-form-item{
  width: 200px;
} */
/* ::v-deep .jiedainame .el-form-item__label{
  width: 90px !important;
}
::v-deep .el-input--small .el-input__inner{
  width: 230px;
} */
::v-deep .el-textarea .el-textarea__inner{
  /* text-align: center; */
  width: 1010px;
  height: auto !important;
  min-height: 130px !important;
}
/* 到访单位 */
::v-deep .el-form-item__content .el-input--small .el-input__inner{
  width: 220px;
}
/* 表格头部 */
v-deep .el-table thead tr{
  background-color: #004d86 !important;
}
/* 树形 */
.threeselects {
  width: 200px;
}
.threeselects .vue-treeselect__input {
  background-color: #004d86;
  /* color: white; */
}
::v-deep .search .el-form-item__content{
  margin-top: 0px;
}
::v-deep .el-picker-panel__body{
  background-color: #004d86 !important;
}
::v-deep .vue-treeselect__control{
  background-color: transparent;
  color: white;
}
::v-deep .vue-treeselect__single-value{
  color: white;
}
::v-deep .threeselects{
  width: 220px;
}
/* #execute{
  background: white !important;
  /* color: white !important; */
/* } */
.el-scrollbar__view {
   background: white !important;
}
/* 修改按钮btu */
.el-button--btus {
  width: 60px !important;
  border: 1px solid transparent;
  padding: 3px 8px;
  font-size: 14px;
  line-height: 1.5;
  border-radius: 3px;
  color: #fff;
  background-color: #13ce66;
}
/* 删除按钮btu */
.el-button--btds {
  width: 60px !important;
  border: 1px solid transparent;
  padding: 3px 8px;
  font-size: 14px;
  line-height: 1.5;
  border-radius: 3px;
  color: #fff;
  background-color: #ff4949;
}
::v-deep .el-time-spinner__list .el-scrollbar li{
  color: white !important;
}
::v-deep .el-select--medium{
  background-color: white  !important;;
}
::v-deep .el-table .el-table__header-wrapper th, .el-table .el-table__fixed-header-wrapper th {
  background-color:rgb(0, 60, 105);
  color: white;
}
::v-deep .daofang .el-input__inner{
  width: 230px;
  height: 38px;
  background: transparent;
  line-height: 38px;
  color: white;
}
/* 文字多余部分省略 */
::v-deep .el-table__cell .cell {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.el-button--btr1 {
  /* border-color: #20B2AA; */
  color: #FFFFFF;
  width: 76px;
  height: 36px;
  background-color: #1d96ff;
  border-radius: 4px;
  font-size: 14px;
  text-align: center;
  /* line-height: 36px; */
  /* margin-top: 1px; */
  padding-top: 11px !important;
}
.el-button--btr2 {
  /* border-color: #20B2AA; */
  color: #FFFFFF;
  width: 76px;
  height: 36px;
  background-color: #1d96ff;
  border-radius: 4px;
  font-size: 14px;
  text-align: center;
  /* margin-top: 1px; */
  /* padding-top: 12px; */

  padding-top: 12px !important;
}
::v-deep .el-picker-panel__link-btn span{
  display: none !important;
}
/* 调整输入框提示文字颜色 */
::v-deep .vue-treeselect__placeholder {
  color: #bdbdbd4f !important;
}
::v-deep input::-webkit-input-placeholder {
  color: #bdbdbd4f !important;
}
::v-deep input:-moz-placeholder {
  color: #bdbdbd4f !important;
}
::v-deep .el-select--medium{
  border-radius: 5px;
}
::v-deep .el-date-editor .el-input__inner{
  height: 36px;
}
::v-deep .el-date-picker__editor-wrap .el-input__inner{
  width: 30px;
}
</style>
