<template>
  <div class="border-style">
    <div class="inner-style">
      <div class="white-border" />
      <div class="align-center">
        <div class="name">{{ name }}</div>
        <div v-if="num" class="storage">{{ num }}</div>
        <div class="statement">{{ statement }}</div>
      </div>

      <div v-if="step" class="exchange-store">
        <div class="store-list active" @click="changePower">开始换电</div>
        <div class="store-list" @click="FireControlStoreHouse">消防换仓</div>
        <div class="store-list" @click="CheckStoreHouse">检修换仓</div>
        <!-- <div class="store-list" @click="resetMachine">机器人复位</div> -->
      </div>
    </div>
    <div class="rolemanage">
      <el-dialog :title="dialogType === 'check' ? '检修换仓' : '消防换仓'" :visible.sync="dialogStoreVisible">
        <el-form ref="StoreForm" :model="StoreForm" :rules="rules">
          <el-form-item
            :label="dialogType === 'check' ? '检修仓：' : '故障仓：'"
            :label-width="formLabelWidth"
            prop="currentStore"
          >
            <el-select
              v-model="StoreForm.currentStore"
              clearable
              :placeholder="dialogType === 'check' ? '请选择检修仓' : '请选择故障仓'"
            >
              <el-option
                v-for="item in StoreCodeList"
                :key="item.id"
                :label="item+'仓'"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="目标仓：" :label-width="formLabelWidth" prop="targetStore">
            <el-select v-model="StoreForm.targetStore" clearable placeholder="请选择目标仓">
              <el-option
                v-for="item in StoreCodeList"
                :key="item.id"
                :label="item+'仓'"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="exit" @click="dialogStoreVisible = false">取 消</el-button>
          <el-button type="primary" class="save" @click="ChangeStore('StoreForm')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { beginSwap, fireStore, mtStore, resetRobot, getStoreCode } from '@/api/equipment'
export default {
  name: 'SubTitle',
  props: ['name', 'statement', 'num', 'step'],
  data() {
    var checkcurrentStore = (rule, value, callback) => {
      if (!value) {
        return callback(new Error(`请选择${this.dialogType === 'check' ? '检修仓' : '故障仓'}`))
      } else {
        callback()
      }
    }
    var checktargetStore = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请选择目标仓位'))
      } else if (value === this.StoreForm.currentStore) {
        callback(new Error(`${this.dialogType === 'check' ? '检修仓' : '故障仓'}不可与目标仓位重名`))
      } else {
        callback()
      }
    }
    return {
      dialogType: 'fire',
      dialogStoreVisible: false,
      StoreForm: {
        currentStore: null,
        targetStore: null
      },
      rules: {
        currentStore: [
          { validator: checkcurrentStore, trigger: 'blur' }
        ],
        targetStore: [
          { validator: checktargetStore, trigger: 'blur' }
        ]
      },
      formLabelWidth: '120px',
      StoreCodeList: null,
      loading:false,
    }
  },
  mounted() {
    this.getStoreCode()
  },
  methods: {
    // 获取仓位
    getStoreCode() {
      getStoreCode().then((rs) => {
        this.StoreCodeList = rs.data
      })
    },
    // 手动发起换电
    changePower() {
      this.loading=true
      this.$confirm('请确认站控系统手动发起换电？', '开始换电', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await beginSwap()
          this.$message({
            type: 'success',
            message: res.data,
          })
        })
        .catch(err => { console.error(err) })
    },
    // 机器人复位
    // resetMachine() {
    //   this.$confirm('请确认机器人复位？', '机器人复位', {
    //     confirmButtonText: '确认',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   })
    //     .then(async() => {
    //       const res = await resetRobot()
    //       this.$message({
    //         type: 'success',
    //         message: res.data,
    //       })
    //     })
    //     .catch(err => { console.error(err) })
    // },
    // 检修消防换仓,显示弹窗
    CheckStoreHouse() {
      this.dialogType = 'check'
      this.dialogStoreVisible = true
    },
    FireControlStoreHouse() {
      this.dialogType = 'fire'
      this.dialogStoreVisible = true
    },
    // 检修换仓、消防换仓下发指令
    ChangeStore(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          const params = this.StoreForm
          this.loading=true;
          // 检修换仓
          if (this.dialogType === 'check') {
            mtStore(params).then(res => {
              if (res) {
                this.loading = false
                this.dialogStoreVisible = false
                this.$message({
                  type: 'success',
                  message: res.data,
                })
                this.resetForm()
              } else {
                this.loading = false
                this.dialogStoreVisible = false
                this.resetForm()
              }
            })
          }
          // 消防换仓
          else {
            fireStore(params).then(res => {
              if (res) {
                this.loading = false
                this.dialogStoreVisible = false
                this.$message({
                  type: 'success',
                  message: res.data,
                })
                this.resetForm()
              } else {
                this.loading = false
                this.dialogStoreVisible = false
                this.resetForm()
              }
            })
          }
        }
      })
    },
    // 清空下拉仓位
    resetForm() {
      this.StoreForm.currentStore = null
      this.StoreForm.targetStore = null
    }
  }
}
</script>

<style scoped>
.border-style {
  border: 2px solid rgb(25, 34, 51);
  border-left: none;
  border-right: none;
  padding: 2px;
}

.inner-style {
  background: #121a29;
  padding: 3px;
  display: flex;
  height: 32px;
  flex-direction: row;
}

.white-border {
  width: 28px;
  height: 18px;
  background-image: url("../assets/ico2.png");
  background-size: 100% 100%;
}

.align-center {
  display: flex;
  flex-direction: row;
  margin: 0 5px;
  align-items: center;
}

.name {
  color: white;
  font-size: 1.2rem;
  font-weight: 600;
  text-shadow: 0 0 3px #1753ce, 0 0 4px #1753ce, 0 0 3px #1753ce, 0 0 3px #1753ce;
  margin-left: -4.2%;
}

.storage {
  background: #192b7d;
  width: 20px;
  height: 20px;
  color: white;
  font-size: 16px;
  font-weight: 600;
  border-radius: 50%;
  text-indent: 6px;
  line-height: 22px;
  margin-left: 3px;
}

.statement {
  color: #324264;
  font-size: 0.6rem;
  margin: 0 8px;
  font-weight: 600;
  padding-top: 9px;
}

.exchange-store {
  flex: 1;
  height: 32px;
  position: relative;
  bottom: 3px;
  display: flex;
  justify-content: flex-end;
}

.store-list {
  width: 88px;
  height: 32px;
  text-align: center;
  line-height: 32px;
  border: #414a63 solid 1px;
  color: #84988c;
  font-size: 14px;
  font-weight: 600;
  margin-right: 15px;
  cursor: pointer;
}

.active {
  border: #91fdb9 solid 1px;
  color: #91fdb9;
}
</style>
