<template>
  <div class="app-container">
    <!-- 钥匙归还弹窗 -->
    <el-dialog :title="$t('page.business.resource.zcg.ysgh')" :visible.sync="open" width="1000px" append-to-body
      :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <template v-if="isBatch">
          <el-alert :title="$t('page.business.resource.zcg.ysghts')" type="error" :closable="false">
          </el-alert>
          <el-row style="margin:10px 0px;">
            <el-col :span="24">
              <SelectList :value="selectList" />
            </el-col>
          </el-row>
        </template>
        <template v-else>
          <el-row>
            <el-col :span="12">
              <el-form-item :label="$t('page.business.resource.zcg.zcg')" prop="mc">
                <el-input type="text" v-model="form.mc" maxlength="50" disabled
                  :placeholder="$t('form.placeholderInput')" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="$t('page.business.resource.zcg.glyj')" prop="glyj">
                <el-input type="text" v-model="form.glyj" maxlength="50" disabled
                  :placeholder="$t('form.placeholderInput')" />
              </el-form-item>
            </el-col>
          </el-row>
        </template>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.zcg.ghr1Id')" prop="ghr1Id">
              <SelectDeptUser v-model="form.ghr1Id" key="ghr1Id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.zcg.ghr1mm')" prop="ghr1mm">
              <el-input type="password" v-model="form.ghr1mm" maxlength="20" :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.zcg.ghr2Id')" prop="ghr2Id">
              <SelectDeptUser v-model="form.ghr2Id" key="ghr2Id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.zcg.ghr2mm')" prop="ghr2mm">
              <el-input type="password" v-model="form.ghr2mm" maxlength="20" :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.zcg.jsr')">
              <el-input type="text" v-model="nickName" maxlength="50" disabled
                :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.zcg.jsrmm')" prop="jsrmm">
              <el-input type="password" v-model="form.jsrmm" maxlength="20" :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('form.qmyy')" prop="qmyy">
              <el-input type="text" :value="$t('page.business.resource.zcg.ysgh')" maxlength="50" disabled
                :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save">{{ $t('form.confirm') }}</el-button>
        <el-button @click="cancel">{{ $t('form.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { zcg_ysgh, zcg_ysghBatch } from "@/api/business/zcg/zcg"
import { mapGetters } from 'vuex'
import SelectList from "./SelectList";
import SelectDeptUser from '@/views/business/comps/select/SelectDeptUser';

export default {
  name: "ZcgYsgh",
  components: { SelectList, SelectDeptUser },
  data() {
    return {
      isBatch: false,
      ids: [],
      selectList: [],
      open: false,
      form: {},
      rules: {
        ghr1Id: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        ghr2Id: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        ghr1mm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        ghr2mm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        jsrmm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
      }
    }
  },
  computed: {
    ...mapGetters([
      'nickName','name'
    ]),
  },
  created() {
  },
  methods: {
    showBatch(val) {
      this.reset()
      this.isBatch = true
      this.form.ids = val.map(item => item.id)
      this.selectList = val
      this.open = true
    },
    cancel() {
      this.open = false
    },
    reset() {
      this.form = {
        id: null,
        ids: null,
        mc: null,
        qmyy: '钥匙归还',
        ghr1Id: null,
        ghr2Id: null,
        ghr1mm: null,
        ghr2mm: null,
        jsrmm: null
      }
      this.resetForm("form")
    },
    show(row) {
      this.reset()
      this.isBatch = false
      this.form.ids = []
      this.selectList = []
      this.form.id = row.id
      this.form.mc = row.mc
      this.form.glyj = row.glyj
      this.open = true
    },
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.isBatch) {
            this.$modal.loading()
            zcg_ysghBatch(this.form).then(response => {
              this.open = false
              this.$emit('callback')
              this.$modal.closeLoading()
            }).finally(() => {
              this.$modal.closeLoading()
            })
          } else {
            this.$modal.loading()
            zcg_ysgh(this.form).then(response => {
              this.open = false
              this.$emit('callback')
              this.$modal.closeLoading()
            }).finally(() => {
              this.$modal.closeLoading()
            })
          }

        }
      })
    }
  }
}
</script>
