<template>
  <div>
    <!-- 处置试剂弹窗 -->
    <el-dialog :title="$t('page.business.resource.sj.cz')" :visible.sync="open" width="1200px" append-to-body
      :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row style="margin:10px 0px;">
          <el-col :span="24">
            <el-table :data="selectList" border>
              <el-table-column :label="$t('page.business.resource.sj.mc')" align="left" prop="mc"
                :show-overflow-tooltip="true" width="120px" />
              <el-table-column :label="$t('page.business.resource.sj.bh')" align="left" prop="bh"
                :show-overflow-tooltip="true" width="120px" />
              <el-table-column :label="$t('page.business.resource.sj.sjczfs')" align="center" width="250px">
                <template slot-scope="scope">
                  <BusinessSelect v-model="scope.row.czfs" dictType="business_czfs"></BusinessSelect>
                </template>
              </el-table-column>
              <el-table-column :label="$t('page.business.resource.sj.yuanyin')" align="center">
                <template slot-scope="scope">
                  <el-input type="text" v-model="scope.row.czyy" maxlength="50"
                    :placeholder="$t('form.placeholderInput')" />
                </template>
              </el-table-column>
              <el-table-column :label="$t('page.business.resource.sj.czl')" align="center">
                <template slot-scope="scope">
                  <el-input type="text" v-model="scope.row.czl" maxlength="50"
                    :placeholder="$t('form.placeholderInput')"> <template slot="append">{{ scope.row.kcdw }}</template>
                  </el-input>
                </template>
              </el-table-column>
              <el-table-column :label="$t('form.operate')" align="center" width="150px">
                <template slot-scope="scope">
                  <el-button type="text" @click="handleSc(scope.row)">{{
                    $t('form.delete') }}</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('form.qmyy')" prop="qmyy">
              <el-input type="text" :value="form.qmyy" maxlength="50" disabled
                :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('form.signer')">
              <el-input type="text" v-model="nickName" maxlength="50" disabled
                :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.password')" prop="qmrmm">
              <div class="sbzdtcma"> <input type="text"></div>
              <el-input type="password" @contextmenu.native.prevent @copy.native.prevent @cut.native.prevent
                v-model="form.qmrmm" maxlength="20" autocomplete="off" auto-complete="new-password" show-password
                :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 { sj_plcz } from "@/api/business/sj/sj"
import { mapGetters } from 'vuex'
import SelectList from "./SelectList";
import BusinessSelect from '@/views/business/comps/select/BusinessSelect';
export default {
  name: "CzBatch",
  components: { SelectList, BusinessSelect },
  data() {
    return {
      ids: [],
      selectList: [],
      open: false,
      form: {},
      rules: {
        czfs: [
          { required: true, message: this.$t('page.business.resource.sj.sjczfs') + this.$t('form.notEmpty'), trigger: "change" },
          { min: 0, max: 50, message: this.$t('page.business.resource.sj.sjczfs') + this.$t('form.lengthLimit') + '50', trigger: 'blur' }
        ],
        czl: [
          { required: true, message: this.$t('page.business.resource.sj.czl') + this.$t('form.notEmpty'), trigger: "change" },
        ],
        remark: [
          { required: true, message: this.$t('page.business.resource.sj.yuanyin') + this.$t('form.notEmpty'), trigger: "change" },
        ],
        qmrmm: [
          { required: true, message: this.$t('form.signerPsw') + this.$t('form.notEmpty'), trigger: "change" },
        ]

      }
    }
  },
  computed: {
    ...mapGetters([
      'nickName', 'name'
    ]),
  },
  created() {
  },
  methods: {
    // 删除
    handleSc(row) {
      const that = this
      this.$modal.confirm(this.$t('form.confirmDelete')).then(() => {
        const _index = _.findIndex(that.selectList, function (o) { return o.id === row.id })
        if (_index > -1) {
          that.selectList.splice(_index, 1)
          that.form.ids = that.selectList.map(item => item.id)
        }
      }).catch(() => { })
    },
    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,
        bh: null,
        qmyy: this.$t('page.business.resource.sj.cz'),
        kcdw: '',
        qmrmm: null
      }
      this.resetForm("form")
    },
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let params = this.form
          params.list = this.selectList
          if (this.selectList.length <= 0) {
            this.$modal.msgError("请选择要操作的试剂")
          } else {
            for (var i = 0; i < params.list.length; i++) {
              if (!params.list[i].czfs || params.list[i].czfs == '') {
                this.$modal.msgError(params.list[i].mc + "处置方式不能为空")
                return
              }
              if (!params.list[i].czyy || params.list[i].czyy == '') {
                this.$modal.msgError(params.list[i].mc + "处置原因不能为空")
                return
              } else {
                params.list[i].remark = params.list[i].czyy
              }
              if (!params.list[i].czl || params.list[i].czl == '') {
                this.$modal.msgError(params.list[i].mc + "处置量不能为空")
                return
              }
            }
            this.$modal.loading()
            sj_plcz(params).then(response => {
              this.open = false
              this.$emit('callback')
              this.$modal.closeLoading()
            }).finally(() => {
              this.$modal.closeLoading()
            })
          }
        }
      })
  }
}
}
</script>
