<!-- 仪器列表 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item :label="$t('page.business.resource.yq.yqbh') + '：'" prop="bh">
        <el-input v-model="queryParams.bh" placeholder="" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('page.business.resource.yq.yqmc') + '：'" prop="mc">
        <el-input v-model="queryParams.mc" placeholder="" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item :label="$t('page.business.resource.yq.xcjzrq') + '：'" prop="jzrq">
        <el-date-picker clearable v-model="dateRange" type="daterange" value-format="yyyy-MM-dd" range-separator="-"
          :start-placeholder="$t('form.startDate')" :end-placeholder="$t('form.endDate')">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('page.business.resource.yq.ssbm') + '：'" prop="bmId">
        <el-input v-model="queryParams.bmId" placeholder="" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('form.search')
        }}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('form.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain @click="handleAdd" v-hasPermi="['business:resource:yq:gxsjk']">{{
          $t('page.business.resource.yq.refresh') }}</el-button>
        <el-button type="primary" plain @click="handleAdd" v-hasPermi="['business:resource:yq:xz']">{{
          $t('page.business.resource.yq.add') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="YqList">
      <el-table-column :label="$t('page.business.resource.yq.yqmc')" align="center" prop="mc" />
      <el-table-column :label="$t('page.business.resource.yq.yqbh')" align="center" prop="bh" />
      <el-table-column :label="$t('page.business.resource.yq.yqxh')" align="center" prop="xh" />
      <el-table-column :label="$t('page.business.resource.yq.yqly')" align="center" prop="ly" />
      <el-table-column :label="$t('page.business.resource.yq.xcjzrq')" align="center" prop="jzrq" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jzrq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('page.business.resource.yq.ssbm')" align="center" prop="bmMc" />
      <el-table-column :label="$t('form.operate')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" @click="handleDetail(scope.row)" v-hasPermi="['business:resource:yq:xq']">{{
            $t('form.detail') }}</el-button>
          <el-button type="text" @click="handleUpdate(scope.row)" v-hasPermi="['business:resource:yq:bj']">{{
            $t('form.edit') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改仪器管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.yq.yqmc')" prop="mc">
              <el-input v-model="form.mc" :placeholder="$t('form.placeholderInput')" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.yq.yqbh')" prop="bh">
              <el-input v-model="form.bh" :placeholder="$t('form.placeholderInput')" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.yq.yqxh')" prop="xh">
              <el-input v-model="form.xh" :placeholder="$t('form.placeholderInput')" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.yq.yqly')" prop="ly">
              <el-input v-model="form.ly" :placeholder="$t('form.placeholderInput')" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.yq.xcjzrq')" prop="jzrq">
              <el-date-picker clearable v-model="form.jzrq" type="date" value-format="yyyy-MM-dd"
                :placeholder="$t('form.placeholderSelect')" style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.yq.ssbm')" prop="bmMc">
              <el-input v-model="form.bmMc" :placeholder="$t('form.placeholderSelect')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('page.business.resource.yq.wc')" prop="wc">
              <BusinessSelect v-model="form.wc" dictType="business_cctj"></BusinessSelect>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.remark')" prop="remark">
              <el-input v-model="form.remark" type="textarea" :placeholder="$t('form.placeholderInput')"
                maxlength="500" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('form.qmyy')" prop="qmyy">
              <el-input v-model="form.qmyy" disabled :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('form.signer')" prop="qmrmc">
              <el-input v-model="nickName" disabled :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('form.signerPsw')" prop="qmrmm">
              <el-input v-model="form.qmrmm" :placeholder="$t('form.placeholderInput')" type="password"
                maxlength="20" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('form.saveConfirm') }}</el-button>
        <el-button @click="cancel">{{ $t('form.cancel') }}</el-button>
      </div>
    </el-dialog>
    <!-- 仪器详情 -->
    <YqDetail ref="YqDetail" />
  </div>
</template>

<script>
import { yq_list, yq_info, yq_save, yq_update } from '@/api/business/yq/yq'
import YqDetail from './comps/detail'
import BusinessSelect from '@/views/business/comps/select/BusinessSelect';
import SelectDept from '@/views/business/comps/select/SelectDept';
import { mapGetters } from 'vuex'
export default {
  name: 'Yq',
  components: { YqDetail, BusinessSelect, SelectDept },

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仪器管理表格数据
      YqList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mc: null,
        bh: null,
        xh: null,
        ly: null,
        jzrq: null,
        bmMc: null,
        wc: null
      },
      // 表单参数
      form: {
        'qmyy': this.$t('page.form.add')
      },
      // 表单校验
      rules: {
        mc: [
          { required: true, message: this.$t('page.business.resource.yq.yqmc') + this.$t('form.notEmpty'), trigger: "change" },
          { min: 0, max: 50, message: this.$t('page.business.resource.yq.yqmc') + this.$t('form.lengthLimit') + '50', trigger: 'blur' }
        ],
        bh: [
          { required: true, message: this.$t('page.business.resource.yq.yqbh') + this.$t('form.notEmpty'), trigger: "change" },
          { min: 0, max: 50, message: this.$t('page.business.resource.yq.yqbh') + this.$t('form.lengthLimit') + '50', trigger: 'blur' }
        ],
        xh: [
          { required: true, message: this.$t('page.business.resource.yq.yqxh') + this.$t('form.notEmpty'), trigger: "change" },
          { min: 0, max: 50, message: this.$t('page.business.resource.yq.yqxh') + this.$t('form.lengthLimit') + '50', trigger: 'blur' }
        ],
        ly: [
          { required: true, message: this.$t('page.business.resource.yq.yqly') + this.$t('form.notEmpty'), trigger: "change" },
          { min: 0, max: 50, message: this.$t('page.business.resource.yq.yqly') + this.$t('form.lengthLimit') + '50', trigger: 'blur' }
        ],
        jzrq: [
          { required: true, message: this.$t('page.business.resource.yq.xcjzrq') + this.$t('form.notEmpty'), trigger: "change" },
        ],
        bmMc: [
          { required: true, message: this.$t('page.business.resource.yq.ssbm') + this.$t('form.notEmpty'), trigger: "change" },
        ],
        wc: [
          { required: true, message: this.$t('page.business.resource.yq.wc') + this.$t('form.notEmpty'), trigger: "change" },
        ],
        qmrmm: [
          { required: true, message: this.$t('form.signerPsw') + this.$t('form.notEmpty'), trigger: "change" },
          { min: 0, max: 20, message: this.$t('form.signerPsw') + this.$t('form.lengthLimit') + '20', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'nickName', 'name'
    ]),
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询仪器管理列表 */
    getList() {
      this.loading = true
      this.queryParams.startDate = this.dateRange[0]
      this.queryParams.endDate = this.dateRange[1]
      yq_list(this.queryParams).then((response) => {
        this.YqList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mc: null,
        bh: null,
        xh: null,
        ly: null,
        jzrq: null,
        bmId: null,
        wc: null,
        qmyy: this.$t('page.business.resource.yq.xzyq'),
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = this.$t('form.add')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      yq_info({ id: id }).then((response) => {
        this.form = response.data
        this.form.qmyy = this.$t('page.business.resource.yq.bjyq')
        this.open = true
        this.title = this.$t('form.edit')
      })
    },
    handleDetail(row) {
      this.$refs.YqDetail.show(row)
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            yq_update(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t('form.operationSuccess'))
              this.open = false
              this.getList()
            })
          } else {
            yq_save(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t('form.operationSuccess'))
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
  }
}
</script>
