<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-width="80px"
          label-suffix=":"
        >
          <el-form-item label="质检单号">
            <el-input
              v-model="query.inspection_code"
              clearable
              size="mini"
              placeholder="请输入质检单号"
              style="width: 210px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <el-form-item label="单据类型">
            <el-select
              v-model="query.inspection_type"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              style="width: 210px;"
              @change="hand"
            >
              <el-option
                v-for="item in dict.QC_INV_TYPE_LHD"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="日期">
            <el-date-picker
              v-model="query.createTime"
              type="daterange"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
              @change="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="物料">
            <label slot="label">物&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;料:</label>
            <el-input
              v-model="query.material_code"
              clearable
              size="mini"
              placeholder="请输入物料编码或名称"
              style="width: 210px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <el-form-item label="批号">
            <label slot="label">批&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号:</label>
            <el-input
              v-model="query.pcsn"
              clearable
              size="mini"
              placeholder="请输入物料批号"
              style="width: 210px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <el-form-item label="单据状态">
            <el-select
              v-model="query.bill_status"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              style="width: 210px;"
              @change="hand"
            >
              <el-option
                v-for="item in statusList"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <rrOperation/>
        </el-form>
      </div>

      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-position"
          size="mini"
          :disabled="crud.selections.length !== 1"
          @click="copyAdd(crud.selections[0])"
        >
          复制新增
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="info"
          icon="el-icon-position"
          :disabled="crud.selections.length !== 1"
          size="mini"
          @click="resultEnter(crud.selections[0])"
        >
          结果录入
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-check"
          size="mini"
          :disabled="crud.selections.length !== 1"
          @click="confirm"
        >
          确认
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="warning"
          icon="el-icon-s-tools"
          size="mini"
          :disabled="crud.selections.length !== 1"
          @click="setValid"
        >
          设置有效
        </el-button>
      </crudOperation>
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
          <el-input v-if="false" v-model="form.material_id"/>

          <el-form-item label="质检单号" prop="inspection_code">
            <el-input v-model="form.inspection_code" placeholder="系统生成" disabled style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="物料" prop="material_name">
            <el-input v-model="form.material_name" disabled class="input-with-select">
              <el-button slot="append" icon="el-icon-search" @click="queryMater"/>
            </el-input>
          </el-form-item>
          <el-form-item label="批号" prop="pcsn">
            <el-input v-model="form.pcsn" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="单据类型" prop="inspection_type">
            <el-select
              v-model="form.inspection_type"
              size="mini"
              placeholder="单据类型"
              class="filter-item"
              style="width: 370px;"
              :disabled="true"
            >
              <el-option
                v-for="item in dict.QC_INV_TYPE_LHD"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="form.inspection_type==='30'" label="关联单号" prop="inspection_id_gu">
            <el-input v-model="form.inspection_id_gu" clearable style="width: 370px;" @focus="RelevancyShow=true"/>
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;" rows="2" type="textarea"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="mini"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column v-if="false" prop="inspection_id" label="理化质检单标识"/>
        <el-table-column prop="inspection_code" label="质检单号" width="135"/>
        <el-table-column prop="inspection_type" label="单据类型" :formatter="format_inspection_type"/>
        <el-table-column prop="bill_status" label="单据状态" :formatter="formatStatus"/>
        <el-table-column v-if="false" prop="biz_date" label="业务日期"/>
        <el-table-column v-if="false" prop="material_id" label="物料标识"/>
        <el-table-column prop="material_code" label="物料编码" min-width="120" show-overflow-tooltip/>
        <el-table-column prop="material_name" label="物料名称" min-width="150" show-overflow-tooltip/>
        <el-table-column min-width="110" show-overflow-tooltip prop="pcsn" label="批号"/>
        <el-table-column prop="is_effective" label="是否有效" :formatter="format_is_active"/>
        <el-table-column prop="result" label="结果" :formatter="format_result"/>
        <el-table-column prop="remark" show-overflow-tooltip label="备注"/>
        <el-table-column prop="input_optname" label="创建人"/>
        <el-table-column prop="input_time" label="创建时间" width="135"/>
        <el-table-column prop="check_optname" label="检验人"/>
        <el-table-column prop="check_time" label="检验时间" width="135"/>
        <el-table-column prop="confirm_optname" label="结论判定人"/>
        <el-table-column prop="confirm_time" label="结论判定时间" width="135"/>
        <el-table-column label="操作" width="120px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
              :disabled-edit="canUd1(scope.row)"
              :disabled-dle="canUd2(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>

    <MaterDtl :dialog-show.sync="materShow" :mater-opt-code.sync="materType" @tableChanged2="tableChanged2"/>
    <Dialog1 :visiable1.sync="visiable1" :inspection-id="inspection_id"/>
    <RelevancyDialog :dialog-show.sync="RelevancyShow" @RelevancyChanged="RelevancyChanged"/>

  </div>
</template>

<script>
import crudPhysicalMst from '@/api/wms/ql/physicalMst'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterDtl from '@/views/wms/pub/MaterDialog'
import Dialog1 from './Dialog1'
import crudInspectionsheetmst from '@/api/wms/ql/inspectionsheetmst'
import DateRangePicker from '@/components/DateRangePicker'
import RelevancyDialog from '@/views/wms/ql/physicalMst/RelevancyDialog'
import crudMaterialbase from '@/api/wms/basedata/master/materialbase'

const defaultForm = {
  inspection_id: null,
  inspection_code: null,
  inspection_type: null,
  inspection_id_gu: null,
  material_id: null,
  pcsn: null
}
export default {
  name: 'PhysicalMstConfirm',
  dicts: ['QC_INV_TYPE_LHD', 'IS_OR_NOT', 'QC_RESULT_LHZJD'],
  components: {
    pagination,
    crudOperation,
    rrOperation,
    udOperation,
    MaterDtl,
    DateRangePicker,
    Dialog1,
    RelevancyDialog
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '理化报告单',
      url: 'api/physicalMst',
      idField: 'inspection_id',
      sort: 'inspection_id desc',
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: false,
        reset: true
      },
      crudMethod: { ...crudPhysicalMst }
    })
  },
  data() {
    return {
      materShow: false,
      materType: '11',
      visiable1: false,
      inspection_id: null,
      statusList: [],
      permission: {
        add: ['admin', 'user:add'],
        edit: ['admin', 'user:edit'],
        del: ['admin', 'user:del']
      },
      RelevancyShow: false,
      rules: {
        inspection_type: [
          { required: true, message: '质检单类型不能为空', trigger: 'blur' }
        ],
        biz_date: [
          { required: true, message: '业务日期不能为空', trigger: 'blur' }
        ],
        pcsn: [
          { required: true, message: '批号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    crudInspectionsheetmst.getStatus().then(res => {
      this.statusList = res
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    canUd1(row) {
      return row.bill_status !== '10'
    },
    canUd2(row) {
      return row.bill_status === '99'
    },
    copyAdd(data) {
      crudPhysicalMst.copyAdd(data).then(res => {
        this.crud.addSuccessNotify()
        this.crud.toQuery()
      })
    },
    queryMater() {
      this.materShow = true
    },
    resultEnter(data) {
      this.visiable1 = true
      this.inspection_id = data.inspection_id
    },
    hand(value) {
      this.crud.toQuery()
    },
    format_inspection_type(row, column) {
      return this.dict.label.QC_INV_TYPE_LHD[row.inspection_type]
    },
    formatStatus(row, column) {
      for (const item of this.statusList) {
        if (item.value === row.bill_status) {
          return item.label
        }
      }
    },
    format_is_active(row, column) {
      return this.dict.label.IS_OR_NOT[row.is_effective]
    },
    format_result(row, column) {
      return this.dict.label.QC_RESULT_LHZJD[row.result]
    },
    tableChanged2(row) {
      // 新增一行物料时，给行进行赋值
      this.form.material_id = row.material_id
      this.form.material_code = row.material_code
      this.form.material_name = row.material_name
      const data = {
        'materOpt_code': '01',
        'material_id': row.material_id
      }
      crudPhysicalMst.isMaterType(data).then(res => {
        const param = res.param
        if (param === '10') {
          this.form.inspection_type = '10'
        } else if (param === '20') {
          this.form.inspection_type = '20'
        } else if (param === '30') {
          this.form.inspection_type = '30'
        }
      })
    },
    RelevancyChanged(rows) {
      const row = rows[0]
      this.form.inspection_id_gu = row.inspection_id
    },
    confirm() {
      const _selectData = this.$refs.table.selection
      if (_selectData.length === 0 || _selectData.length > 1) {
        this.crud.notify('请选择一条记录', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
      const data = _selectData[0]
      crudPhysicalMst.confirm(data).then(res => {
        this.crud.notify('确认成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    },
    setValid() {
      const _selectData = this.$refs.table.selection
      if (_selectData.length === 0 || _selectData.length > 1) {
        this.crud.notify('请选择一条记录', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
      const data = _selectData[0]
      crudPhysicalMst.setValid(data).then(res => {
        this.crud.notify('设置成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    }
  }
}
</script>

<style scoped>

</style>
