<template>
  <div>
    <!-- 处置容器弹窗 -->
    <el-dialog :title="isBatch?$t('page.business.resource.gyzj.plczrqt'):$t('page.business.resource.gyzj.czrqt')" :visible.sync="open" width="1200px" append-to-body
      :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules"  v-if="open" :label-width="$i18n.locale === 'zh_CN'?'120px':'170px'">
        <div class="sbzdtcma"> <input type="text"> <input   type="password" show-password> </div>
        <div :style="isBatch ? 'display:block' : 'display:none'">
          <el-row style="margin:10px 0px;">
            <el-col :span="24">
              <el-table :data="selectList" border>
                <el-table-column :label="$t('page.business.resource.gyzj.mc')" align="center" prop="mc"
                  :show-overflow-tooltip="true" width="120px" />
                <el-table-column :label="$t('page.business.resource.gyzj.bh')" align="center" prop="bh"
                  :show-overflow-tooltip="true" width="120px" />
                <el-table-column :label="$t('page.business.resource.gyzj.czfs')" 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.gyzj.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('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>
        </div>
        <div v-if="!isBatch">
          <el-row>
            <el-col :span="12">
              <el-form-item :label="$t('page.business.resource.gyzj.mc')" prop="mc">
                <el-input type="text" v-model="form.mc" maxlength="50" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="$t('page.business.resource.gyzj.bh')" prop="glyj">
                <el-input type="text" v-model="form.bh" maxlength="50" disabled />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item :label="$t('page.business.resource.gyzj.czfs')" prop="czfs">
                    <BusinessSelect v-model="form.czfs" dictType="business_czfs"></BusinessSelect>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item :label="$t('page.business.resource.gyzj.yuanyin')" prop="czyy">
                <el-input type="textarea" v-model="form.czyy" :rows="5" maxlength="500"
                  :placeholder="$t('form.placeholderInput')">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.gyzj.clr')">
              <el-input type="text" v-model="nickName" maxlength="50" disabled />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.gyzj.clr') + $t('form.password')" prop="clrmm">
              <el-input   type="password" show-password v-model="form.clrmm" 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.gyzj.fhr')" prop="fhrId">
              <SelectDeptUser v-model="form.fhrId" key="jdrId" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.gyzj.fhr') + $t('form.password')" prop="fhrmm">
              <el-input   type="password" show-password v-model="form.fhrmm" 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.gyzj.jdr')" prop="jdrId">
              <SelectDeptUser v-model="form.jdrId" key="jdrId" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.gyzj.jdr') + $t('form.password')" prop="jdrmm">
              <el-input   type="password" show-password v-model="form.jdrmm" maxlength="20"
                :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.qmyy')" prop="qmyy">
              <el-input type="text" v-model="form.qmyy" maxlength="20" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save" :disabled="isBatch&&selectList.length==0">{{ $t('form.confirm') }}</el-button>
        <el-button @click="cancel">{{ $t('form.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { czrq, plczrq } from "@/api/business/gyzj/gyzj"
import { mapGetters } from 'vuex'
import SelectDeptUser from '@/views/business/comps/select/SelectDeptUser';
import BusinessSelect from '@/views/business/comps/select/BusinessSelect';

export default {
  name: "Bj",
  components: { SelectDeptUser,BusinessSelect },
  data() {
    return {
      isBatch: false,
      ids: [],
      selectList: [],
      open: false,
      form: {},
      rules: {
        czyy: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        czfs: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        clrId: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        fhrId: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        jdrId: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        clrmm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        fhrmm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        jdrmm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }]
      }
    }
  },
  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.selectList = []
      this.form.ids = val.map(item => item.id)
      let tmp = []
      _.forEach(val, function (item) {
        tmp.push({
          id: item.id,
          mc: item.mc,
          bh: item.bh,
          czyy: null,
          czfs: null,
        })
      })
      this.selectList = tmp
      this.open = true
    },
    cancel() {
      this.open = false
    },
    reset() {
      this.form = {
        id: null,
        ids: [],
        mc: null,
        bh: null,
        czyy: null,
        czfs: null,
        clrId: null,
        fhrId: null,
        jdrId: null,
        qmyy: this.$t('page.business.resource.gyzj.czrq'),
        qmrmm: 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.bh = row.bh
      this.open = true
    },
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let params = this.form
          if (this.isBatch) {
            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
              }
            }
              this.$modal.loading()
              plczrq(params).then(response => {
                this.open = false
                this.$emit('callback')
                this.$modal.closeLoading()
              }).finally(() => {
                this.$modal.closeLoading()
              })
            }
          } else {
            this.$modal.loading()
            czrq(params).then(response => {
              this.open = false
              this.$emit('callback')
              this.$modal.closeLoading()
            }).finally(() => {
              this.$modal.closeLoading()
            })
          }

        }
      })
    }
  }
}
</script>
