<template>
  <div v-if="pageStatus == 0">配置加载中...</div>
  <div v-else-if="pageStatus == 1">此页面不对此账号开放</div>
  <div v-else-if="pageStatus == 2">没有此页内容的配置权限，请检查集团上的配置</div>
  <div v-else-if="pageStatus == 3">加载发生错误</div>
  <div v-else-if="pageStatus == 4" class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item style="float: right">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >添加员工</el-button
        >
      </el-form-item>
    </el-form>
    <el-table :data="personnelList">
      <el-table-column
        label="油站员工主键id"
        v-if="false"
        align="center"
        prop="personnelId"
      />
      <el-table-column label="员工姓名" align="center" prop="personnelName" />
      <el-table-column label="负责枪号" align="center" prop="gunNo" />
      <el-table-column label="手机号" align="center" prop="personnelPhone" />
      <el-table-column label="加油站名称" align="center" prop="stationName" 
        v-if="jiBie!=2"
      />
      <el-table-column
        label="操作"
        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)"
            >修改</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="personnelName">
          <el-input v-model="form.personnelName" placeholder="请输入员工姓名" />
        </el-form-item>
        <el-form-item label="负责枪号" prop="checkedGunList">
          <template v-if="allGunList.length === 0">
            请先在油枪管理里配置油枪
          </template>
          <template v-else>
            <el-checkbox-group v-model="form.checkedGunList">
              <el-checkbox
                v-for="item in allGunList"
                :key="item.oilGunId"
                :label="item.oilGunNo"
                :value="item.oilGunNo"
              >
                {{ item.oilGunNo + "号枪/" + item.oilName }}
              </el-checkbox>
            </el-checkbox-group>
          </template>
        </el-form-item>
        <el-form-item label="手机号" prop="personnelPhone">
          <el-input
            v-model="form.personnelPhone"
            placeholder="请输入员工手机号"
          />
        </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 {
  listPersonnel,
  getPersonnel,
  delPersonnel,
  addPersonnel,
  updatePersonnel,
  exportPersonnel,
} from "@/api/station/personnel";
import { stationinfo, listGun } from "@/api/station/gun";
import { getDept } from "@/api/system/dept";
export default {
  name: "Station_Employee",
  data() {
    return {
      // 总条数
      total: 0,
      // 油站员工信息表格数据
      personnelList: [],
      allGunList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        personnelName: null,
        gunNo: null,
        stationId: null,
        stationName: null,
        qrCode: null,
        personnelPhone: null,
      },
      // 查询参数
      queryInfo: {
        stationId: null,
      },
      query: {
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        personnelName: [
          { required: true, message: "请输入员工姓名", trigger: "blur" },
        ],
        checkedGunList: [
          { required: true, message: "请选择油枪", trigger: "change" },
        ]
      },
      pageStatus:0
    };
  },
  created() {
    this.init();
    this.setPageStatus();
  },
  methods: {
    /** 查询油站员工信息列表 */
    getList() {
      listPersonnel(this.queryParams).then((response) => {
        this.queryParams.stationId = null;
        this.personnelList = response.rows;
        this.total = response.total;
      });
    },
    setPageStatus() {
      this.queryPageStatus([2]).then((res) => {
        this.pageStatus = res;
      });
    },
    init() {
      //加载所有油枪
      this.getList();
      listGun({
        stationId: this.deptId,
      }).then((response) => {
        this.allGunList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    // /** 搜索按钮操作 */
    // handleQuery() {
    //   this.queryParams.pageNum = 1;
    //   this.getList();
    // },
    // /** 重置按钮操作 */
    // resetQuery() {
    //   this.resetForm("queryForm");
    //   this.handleQuery();
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.title = "添加油站员工";
      //【**】
      this.form = {
        stationId: this.deptId,
        checkedGunList: [],
      };
      this.form.stationId = this.deptId;
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      let checkedGunList;
      if (Object.prototype.toString.call(row.gunNo) === "[object String]") {
        checkedGunList = [...new Set(row.gunNo.split(","))].filter((ele) => {
          return ele.toString().trim() !== "";
        });
      } else {
        checkedGunList = [];
      }
      this.form = { ...row, checkedGunList };
      this.open = true;
      this.title = "修改油站员工";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let list=[];
          for(let i=0;i<this.form.checkedGunList.length;i++){
            for(let j=0;j<this.allGunList.length;j++){
              if(this.form.checkedGunList[i]== this.allGunList[j].oilGunNo){
                list.push(this.allGunList[j].oilGunNo);
              }
            }
          }
          this.form.gunNo=list.toString();
          if (!!this.form.personnelId) {
            updatePersonnel(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPersonnel(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const personnelIds = row.personnelId;
      this.$confirm("是否确认删除油站员工信息", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delPersonnel(personnelIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有油站员工信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportPersonnel(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
