<template>
  <div class="app-container">
    <div class="card-change">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="" prop="gunName">
          <el-input
            v-model="queryParams.gunName"
            placeholder="油枪名称"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="" prop="numberId">
          <el-select v-model="queryParams.numberId" placeholder="请选择油品油号" >
            <el-option
              v-for="option in selectOilTypeByPrice"
              :key="option.numberId"
              :label="option.oilType +' '+option.oilName"
              :value="option.numberId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-date-picker
            v-model="beginTime"
            style="width: 160px"
            type="date"
            placeholder="开始日期">
          </el-date-picker>
          至
          <el-date-picker
            v-model="endTime"
            style="width: 160px"
            type="date"
            placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item style="float: right;margin-right: 0px ">
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          <el-button
            type="primary"
            @click="gunAdd()"
            v-hasPermi="['oilConfig:oilGun:list:add']"
          >新增油枪
          </el-button>
        </el-form-item>
      </el-form>
      <div>
        <div style="width: 100%;overflow: auto;height: 72vh">
          <el-table ref="tables"
                  v-loading="loading"
                  :data="gunList"
                  border
                  :default-sort="defaultSort">
          <el-table-column align="center" type="index" label="序号"></el-table-column>
          <el-table-column label="油枪名称" align="center" prop="gunName"/>
          <el-table-column label="所属油罐" align="center" prop="tankName"/>
          <el-table-column label="油品-油号" align="center" prop="oilNumber"></el-table-column>
          <el-table-column label="关联员工" align="center" prop="tankName">
            <template slot-scope="scope">
              {{ jsonChange(scope.row.staffList) }}
            </template>
          </el-table-column>
          <el-table-column label="设备ID" align="center" prop="deviceName"/>
          <el-table-column label="设备枪号" align="center" prop="oilMachineGunNumber"/>
          <el-table-column label="状态" align="center" prop="status"/>
          <el-table-column label="操作人" align="center" prop="realName"/>
          <el-table-column label="操作时间" align="center" prop="createTime"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="gunEdit(scope.row)"
                v-hasPermi="['oilConfig:oilGun:list:update']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                v-hasPermi="['oilConfig:oilGun:list:del']"
                @click="delGun(scope.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        </div>
      </div>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="25%"
      :before-close="handleClose"
      :close-on-click-modal="false">
      <el-form :model="gunForm" :rules="rules" ref="gunForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="油枪名称" prop="gunName">
          <el-input style="width: 300px" placeholder="请输入内容" v-model="gunForm.gunName" class="input-with-select">
            <el-select style="width: 110px" v-model="gunForm.select" slot="append" placeholder="选择油枪"
                       @change="chooseName">
              <el-option
                v-for="option in 50"
                :key="option"
                :label="option + '号枪'"
                :value="option + '号枪'"
              ></el-option>
            </el-select>
          </el-input>
        </el-form-item>
        <el-form-item label="关联油品油号" prop="tankId">
          <el-select style="width: 300px" v-model="gunForm.tankId" placeholder="请选择所属油罐">
            <el-option
              v-for="option in tankList"
              :key="option.id"
              :label="option.tankName"
              :value="option.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属油罐" prop="tankId">
          <el-select style="width: 300px" v-model="gunForm.tankId" placeholder="请选择所属油罐">
            <el-option
              v-for="option in tankList"
              :key="option.id"
              :label="option.tankName"
              :value="option.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联员工" prop="status">
          <el-radio v-model="staffStatus" label="全部员工">全部员工</el-radio>
          <el-radio v-model="staffStatus" label="指定员工">指定员工</el-radio>
        </el-form-item>
        <el-form-item v-if="staffStatus == '指定员工'" label="选择员工" prop="staffId">
          <el-select
            v-model="staffIds"
            clearable
            placeholder=""
            style="width: 300px"
            multiple
            collapse-tags
          >
            <el-option v-for="item in staffList" :key="item.id" :label="item.realName" :value="item.id+''">
              <span style="float: left">{{ item.realName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.mobile }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="gunForm.status">
            <el-radio
              v-for="dict in dict.type.zhzt"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              @change="handleIsopenSelect"
            />
          </el-radio-group>
        </el-form-item>
        <el-form-item label="设备ID" prop="staffId">
          <el-select
            v-model="gunForm.deviceId"
            clearable
            placeholder=""
            style="width: 300px"
          >
            <el-option v-for="item in youjiList" :key="item.id" :label="item.deviceName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备枪号" prop="name">
          <el-input style="width: 300px" v-model="gunForm.oilMachineGunNumber"></el-input>
          <div style="font-size: 12px;width: 300px; color: #999999">
            请确保输入的集线器正确的枪号，否则出现设备显示屏与一件加油页面显示数据不一致。例如：0001，0002···
          </div>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogVisible = false,getList()">取 消</el-button>
          <el-button type="primary" @click="submitForm()">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getGunApi,
  insertGunApi,
  updateGunApi,
  selectTankByNumberApi,
  delGunApi,
  getGunListpageApi
} from "@/api/oilConfig/oilGuns";
import {cleanTankApi} from "@/api/oilConfig/oilTank";
import {queryStaffs} from "@/api/order/staff";
import {getListByPageApi} from "@/api/seeting/youqiji";
import {getOilNameListApi} from "@/api/oilPrice";

export default {
  name: 'oilqiang',
  dicts: ['zhzt'],

  data() {
    return {
      queryParams: {
        page: 1,
        pageSize: 10
      },
      beginTime: "",
      endTime: "",
      selectOilTypeByPrice:[],
      total: '',
      staffIds: [],
      staffList: [],
      youjiList: [],
      // 默认排序
      // defaultSort: {prop: 'createTime', order: 'descending'},
      defaultSort: '',
      title: "",
      dialogVisible: false,
      loading: false,
      list: [],

      // 判断当前窗口是新增还是修改该
      judgement: false,
      tankList: [],
      gunList: [],
      c: '',
      gunForm: {
        id: '',
        gunName: '',
        tankId: '',
        status: '',
        storeId: '',
        createTime: '',
        updateTime: '',
        createBy: '',
        updateBy: '',
        numberId: '',
        oilMachineGunNumber: '',
        select: '',
        staffStatus: '全部员工',
        staffList: '',
        deviceId: '',
        deviceName: '',

      },
      staffStatus: '全部员工',

      statusOut: '',
      ruleForm: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        select: '请选择',
        resource: '',
        desc: ''
      },
      rules: {
        gunName: [
          {required: true, message: '请输入油枪名称', trigger: 'blur'},
        ],

        tankId: [
          {required: true, message: '请选择所属油罐', trigger: 'change'}
        ],
      }
    }
  },

  async created() {
    let this_ = this
    await this_.getList()
    await this_.getStaffList()
    await this_.getyouji()
    await this_.getOilNameList()
  },
  watch: {
    dialogVisible: function (newValue, oldValue) {
      if (oldValue) {
        this.$refs["gunForm"].resetFields();
      }
    }
  },
  methods: {
    getOilNameList() {
      var this_ = this
      getOilNameListApi().then(response => {
        var list = response.data
        this_.selectOilTypeByPrice = list
      });
    },
    handleIsopenSelect() {
      this.$forceUpdate()
    },
    getList() {
      this.gunList = []
      let dateRange = []
      if (this.beginTime && this.endTime) {
        dateRange.push(this.beginTime.toLocaleDateString())
        dateRange.push(this.endTime.toLocaleDateString())
      }
      getGunListpageApi(this.addDateRange(this.queryParams, dateRange)).then(response => {
        this.gunList = response.data.records;
        this.total = response.data.total;
      })
    },
    getStaffList() {
      queryStaffs().then(response => {
        this.staffList = response.data;
      })
    },
    getyouji() {
      let queryParams = {
        page: 1,
        pageSize: 100000
      }
      getListByPageApi(queryParams).then(res => {
        this.youjiList = res.data.records;
      })
    },
    // 添加油枪
    gunAdd(numberId) {
      let this_ = this;
      this_.dialogVisible = true;
      this_.judgement = false;
      this_.staffIds = [],
        this.cler()
      // 清空数据
      this_.gunForm.status = '启用'
      this_.title = '添加油枪'
      this_.selectTankByNumber(numberId)
    },
    gunEdit(data) {
      let this_ = this;
      this_.staffIds = [],
        this_.dialogVisible = true;
      this_.judgement = true;
      this.cler()
      this_.gunForm = data;
      this_.statusOut = data.status;
      this_.title = '修改油枪'

      this_.selectTankByNumber(data.numberId)
    },

    delGun(id) {
      this.$confirm('此操作将删除油枪信息,请谨慎操作!是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let fil = {
          id: id
        }
        delGunApi(fil).then(response => {
          this.dialogVisible = false;
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        tankName: '',
        page: 1,
        pageSize: 10
      };
      this.beginTime = ''
      this.endTime = ''
      this.getList();
    },
    submitForm() {
      this.$refs["gunForm"].validate((valid) => {
        if (valid) {
          // 查询numberId
          for (let tankListKey in this.tankList) {
            if (parseInt(this.gunForm.tankId) === parseInt(this.tankList[tankListKey].id)) {
              this.gunForm.numberId = parseInt(this.tankList[tankListKey].numberId);
            }
          }

          let staff = [];
          this.staffIds.forEach(id => {
            let matchingStaff = this.staffList.find(staff => staff.id === Number(id));
            let names = {
              id: id,
              realName: matchingStaff ? matchingStaff.realName : id
            }
            staff.push(names)
          });
          this.gunForm.staffList = JSON.stringify(staff)
          this.youjiList.forEach(id => {
            if (id.id == this.gunForm.deviceId) {
              this.gunForm.deviceName = id.deviceName
            }
          })

          this.gunForm.staffStatus = this.staffStatus

          if (!this.gunForm.id) {
            insertGunApi(this.gunForm).then(response => {
              console.log("response", response)
              if (response.code == 200) {
                this.$modal.msgSuccess("新增成功");
                this.dialogVisible = false;
                this.getList();

              } else {

              }
            }).catch(reason => {
            });
          } else {
            console.log("111111", this.gunForm)
            updateGunApi(this.gunForm).then(response => {
              if (response.code == 200) {
                this.$modal.msgSuccess("修改成功");
                this.dialogVisible = false;
                this.getList();
                this.dialogVisible = false

              }
            });
          }

        } else {
          return false;
        }
      });
      // this.getList();
    },
    chooseName() {
      var this_ = this
      this_.gunForm.gunName = this_.gunForm.select
    },
    handleClose() {
      this.dialogVisible = false;
    },

    // 查询油罐信息
    selectTankByNumber(data) {
      let numberId = '';
      if (data) {
        numberId = data
      }
      selectTankByNumberApi(numberId).then(response => {
        this.tankList = response.data;
      });
    },
    cler() {
      this.gunForm = {
        id: '',
        gunName: '',
        tankId: '',
        status: '',
        storeId: '',
        createTime: '',
        updateTime: '',
        createBy: '',
        updateBy: '',
        numberId: '',
        oilMachineGunNumber: '',
        select: '',
        staffStatus: '全部员工',
        staffList: '',
        deviceId: '',
        deviceName: '',

      }
    },
    jsonChange(data) {
      if (data) {
        let a = JSON.parse(data);
        let b = a.map(res => res.realName).join(',');
        return b
      } else {
        return '--'
      }

    },
  }
}
</script>

<style scoped lang="scss">
.app-container {
  height: 100%;
}

.bor-bai {
  width: 100%;
  border-radius: 10px;
  box-sizing: border-box;
  padding: 20px;
  background: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
  margin-bottom: 20px;


}

.title_ {
  font-weight: bold;
  font-size: 18px;
  color: #333333;
}

.wrap-box {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
}

.oil-box {

}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 100%;
}

.conten-box {
  width: 100%;
  border: 1px solid #ebeef5;
  margin: 20px 0px;
}

.conten-top {
  border-bottom: 1px solid #ebeef5;
  box-sizing: border-box;
  padding: 18px 20px;
}

.conten-bottom {
  box-sizing: border-box;
  padding: 20px 20px;
  display: flex;
  flex-wrap: wrap;
}

.cardbox {
  height: 87px;
  width: 278px;
  margin-right: 35px;
  margin-bottom: 35px;
  box-sizing: border-box;
  padding: 10px;
  border-radius: 10px;
  //width: 19%;

  background-size: 40% 70%;
  background-color: #f9f9f9;
  position: relative;


}

.boxborder {
  border-color: #FF9655;
  border: 1px solid;
}

.cardbox-rd {
  position: absolute;
  right: 5px;
  bottom: 0px;
  width: 50px;
  height: 50px;

}

.act-cardbox {
  margin-right: 35px;
  height: 87px;
  width: 278px;
  position: relative;
  overflow: hidden;
  //margin-right: 10px;
  margin-bottom: 35px;
  box-sizing: border-box;
  padding: 10px;
  border-radius: 10px;
  //width: 19%;
  //background: url(../../../assets/images/jybjj.png) center no-repeat;
  background-size: 40% 70%;
  background-color: #fff9f5;
  color: #FF9655 !important;
}


.cardboxb {
  position: absolute;
  /*width: 50px;*/
  width: auto;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 12px;
  left: 0px;
  bottom: 0px;
  padding: 5px 10px;
  background: linear-gradient(312deg, #FF8646 0%, #FFA360 100%);
  border-radius: 0px 10px 0px 10px;
}

.actcolor {
  color: #FF9655 !important;
}

.cardbox-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  color: #5b5e62;
  font-size: 12px;

  font-family: Source Han Sans CN;
  //font-weight: 400;
}

.cardbox-title {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 28px;
  //margin: 15px 0px;


}

.cardbox-gunName {
  font-family: YouSheBiaoTiHei, YouSheBiaoTiHei;
  font-weight: 400;
  font-size: 28px;
  //color: #FF9655;
  //line-height: 0px;
  //text-align: center;
  //font-style: normal;
  //text-transform: none;
}

.cardbox-bottom {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 100%;
  color: #5b5e62;
  font-size: 14px;
}

.input-box {
  display: flex;
  align-items: center;
}

.box-top {
  display: flex;
  align-items: center;
  justify-content: space-between;

  width: 100%;
}


</style>
