<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item :label="$t('table.deptName')">
        <el-input
          v-model="queryParams.deptName"
          :placeholder="$t('placeholder.deptName')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
	  <el-form-item :label="$t('table.beginDate')" >
        <el-date-picker clearable
          v-model="queryParams['params[beginDate]']"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('placeholder.beginDate')">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('table.endDate')">
        <el-date-picker clearable
          v-model="queryParams['params[endDate]']"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('placeholder.endDate')">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="祖级列表" prop="ancestors">
        <el-input
          v-model="queryParams.ancestors"
          placeholder="请输入祖级列表"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示顺序" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入显示顺序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="leader">
        <el-input
          v-model="queryParams.leader"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管理员用户Id" prop="manager">
        <el-input
          v-model="queryParams.manager"
          placeholder="请输入管理员用户Id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入公司地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('btn.search')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('btn.Reset')}}</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="['busCompany:busCompany:add']"
        >{{$t('btn.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="['busCompany:busCompany: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="['busCompany:busCompany: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="['busCompany:busCompany:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="busCompanyList" @selection-change="handleSelectionChange">
	  <el-table-column :label="$t('table.SerialNumber')" width="50px" align="center">
		<template slot-scope="scope">
			{{ scope.$index + (queryParams.pageNum - 1) * queryParams.pageSize + 1 }}
		</template>
	  </el-table-column>
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="部门id" align="center" prop="deptId" />
      <el-table-column label="父部门id" align="center" prop="parentId" />
      <el-table-column label="祖级列表" align="center" prop="ancestors" /> -->
      <el-table-column :label="$t('table.deptName')" align="center" prop="deptName" />
      <el-table-column :label="$t('table.leader')" align="center" prop="leader" />
      <el-table-column :label="$t('table.username1')" align="center" prop="phone" />
	  <el-table-column :label="$t('table.address')" align="center" prop="address" />
      <!-- <el-table-column label="显示顺序" align="center" prop="orderNum" /> -->
      <el-table-column :label="$t('table.username3')" align="center" prop="username" />
	  <el-table-column :label="$t('table.createTime1')" align="center" prop="createTime" />
      <!-- <el-table-column label="管理员用户Id" align="center" prop="manager" /> -->
      <!-- <el-table-column label="部门状态" align="center" prop="status" /> -->
      <el-table-column :label="$t('table.operation')" 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="['busCompany:busCompany:edit']"
          >{{$t('table.detail')}}</el-button>
		  <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate2(scope.row)"
            v-hasPermi="['busCompany:busCompany:resetPassword']"
          >{{$t('table.resetPassword')}}</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['busCompany:busCompany: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"
    />

    <!-- 添加或修改bus公司对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!-- <el-form-item label="父部门id" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父部门id" />
        </el-form-item> -->
        <el-form-item :label="$t('table.deptName')" prop="deptName">
          <el-input :disabled='isDetails' v-model="form.deptName" :placeholder="$t('placeholder.deptName')" />
        </el-form-item>
        <!-- <el-form-item label="显示顺序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
        </el-form-item> -->
        <el-form-item :label="$t('table.leader')" prop="leader">
          <el-input :disabled='isDetails' v-model="form.leader" :placeholder="$t('placeholder.leader')" />
        </el-form-item>
        <el-form-item :label="$t('table.username1')" prop="phone">
          <el-input :disabled='isDetails' v-model="form.phone" :placeholder="$t('placeholder.phone')" />
        </el-form-item>
        <!-- <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item> -->
        <!-- <el-form-item label="管理员用户Id" prop="manager">
          <el-input v-model="form.manager" placeholder="请输入管理员用户Id" />
        </el-form-item> -->
        <el-form-item :label="$t('table.address')" prop="address">
          <el-input :disabled='isDetails' v-model="form.address" :placeholder="$t('placeholder.address')" />
        </el-form-item>
		<el-form-item :label="$t('table.username3')" prop="username">
          <el-input :disabled='isDetails' v-model="form.username" :placeholder="$t('placeholder.username2')" />
        </el-form-item>
		<el-form-item :label="$t('table.password')" prop="password">
          <el-input :disabled='isDetails' type='password' v-model="form.password" :placeholder="$t('placeholder.password')" />
        </el-form-item>
        <!-- <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!isDetails" type="primary" @click="submitForm">{{$t('btn.submit')}}</el-button>
        <el-button @click="cancel">{{$t('btn.cancel')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBusCompany, getBusCompany, delBusCompany, addBusCompany, updateBusCompany,resetPassword } from "@/api/busCompany/busCompany";

export default {
  name: "BusCompany",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // bus公司表格数据
      busCompanyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
	  isDetails: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // parentId: null,
        // ancestors: null,
        deptName: null,
		'params[beginDate]':null,
		'params[endDate]':null,
        // orderNum: null,
        // leader: null,
        // phone: null,
        // email: null,
        // manager: null,
        // address: null,
        // status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
		deptName: [
          { required: true, message: this.$t('rules.deptName'), trigger: "blur" }
        ],
		leader: [
          { required: true, message: this.$t('rules.leader'), trigger: "blur" }
        ],
		phone: [
          { required: true, message: this.$t('rules.phone'), trigger: "blur" }
        ],
		address: [
          { required: true, message: this.$t('rules.address'), trigger: "blur" }
        ],
		username: [
          { required: true, message: this.$t('rules.username'), trigger: "blur" }
        ],
		password: [
          { required: true, message: this.$t('rules.password'), trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询bus公司列表 */
    getList() {
      this.loading = true;
      listBusCompany(this.queryParams).then(response => {
        this.busCompanyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deptId: null,
        parentId: null,
        ancestors: null,
        deptName: null,
        orderNum: null,
        leader: null,
        phone: null,
        email: null,
        manager: null,
        address: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deptId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
	  this.isDetails = false
      this.open = true;
      this.title = this.$t('dialog.busTitel1')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
	  this.isDetails = true
      const deptId = row.deptId || this.ids
      getBusCompany(deptId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('dialog.busTitel2')
      });
    },
	// 重置密码
	handleUpdate2(row) {
    //   this.reset();
      const deptId = row.deptId || this.ids
      resetPassword({deptId:deptId}).then(response => {
        if(response.code==200){
			this.$modal.msgSuccess("重置密码成功，初始密码为 123456");
		}
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deptId != null) {
            updateBusCompany(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('message.success1'));
              this.open = false;
              this.getList();
            });
          } else {
            addBusCompany(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('message.success2'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deptIds = row.deptId || this.ids;
    //   this.$modal.confirm('是否确认删除bus公司编号为"' + deptIds + '"的数据项？').then(function() {
		 this.$modal.confirm(this.$t('message.isDelete2')).then(function() {
        return delBusCompany(deptIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('message.success3'));
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('busCompany/busCompany/export', {
        ...this.queryParams
      }, `busCompany_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
