<template>
  <el-dialog
    style="padding-top: 0px;"
    :title="crud.status.title"
    append-to-body
    fullscreen
    :before-close="crud.cancelCU"
    :visible.sync="crud.status.cu > 0 || crud.status.view > 0"
  >
    <el-row v-show="crud.status.cu > 0" :gutter="20">
      <el-col :span="20" style="border: 1px solid white">
        <span />
      </el-col>
      <el-col :span="4">
        <span>
          <el-button icon="el-icon-check" size="mini" :loading="crud.cu === 2" type="primary" @click="crud.submitCU">保存</el-button>
          <el-button icon="el-icon-close" size="mini" type="info" @click="crud.cancelCU">关闭</el-button>
        </span>
      </el-col>
    </el-row>

    <el-form ref="form" style="border: 1px solid #cfe0df;margin-top: 10px;padding-top: 10px;" :inline="true" :model="form" :rules="rules" size="mini" label-width="150px" label-suffix=":">
      <el-form-item label="实验序号" prop="formula_code">
        <el-input v-model="form.formula_code" placeholder="系统生成" style="width: 210px" disabled />
      </el-form-item>
      <el-form-item label="物料" prop="material_id">
        <el-select
          v-model="form.material_id"
          placeholder="物料"
          style="width: 210px"
          class="filter-item"
          :disabled="crud.status.view > 0"
          @change="storChange"
        >
          <el-option
            v-for="item in THWs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="批号" prop="inspection_id">
        <el-select
          v-model="form.inspection_id"
          clearable
          size="mini"
          placeholder="批号"
          class="filter-item"
          @change="storChange2"
        >
          <el-option
            v-for="item in PCSNs"
            :key="item.id"
            :label="item.code"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生产厂家" prop="manufactor">
        <el-input v-model="form.manufactor" placeholder="生产厂家" style="width: 210px" disabled />
      </el-form-item>
      <el-form-item label="CT(%)" prop="CJZB00004">
        <label slot="label">CT(%):</label>
        <el-input-number
          v-model="form.CJZB00004"
          :controls="false"
          :precision="3"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="Cr3C2" prop="CJZB00021">
        <label slot="label">Cr3C2:</label>
        <el-input-number
          v-model="form.CJZB00021"
          :controls="false"
          :precision="3"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="氧含量" prop="CJZB00007">
        <label slot="label">氧含量:</label>
        <el-input-number
          v-model="form.CJZB00007"
          :controls="false"
          :precision="3"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>

      <el-form-item label="VC(%)" prop="CJZB00020">
        <label slot="label">VC(%):</label>
        <el-input-number
          v-model="form.CJZB00020"
          :controls="false"
          :precision="3"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="CF(%)" prop="CJZB00006">
        <label slot="label">CF(%):</label>
        <el-input-number
          v-model="form.CJZB00006"
          :controls="false"
          :precision="0"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="FSSS(μm)" prop="CJZB00001">
        <label slot="label">FSSS(μm):</label>
        <el-input-number
          v-model="form.CJZB00001"
          :controls="false"
          :precision="4"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="BET(㎡/g)" prop="CJZB00002">
        <label slot="label">BET(㎡/g):</label>
        <el-input-number
          v-model="form.CJZB00002"
          :controls="false"
          :precision="4"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
    </el-form>
    <el-form ref="form2" style="border: 1px solid #cfe0df;margin-top: 10px;padding-top: 10px;" :inline="true" :model="form" :rules="rules" size="mini" label-width="150px" label-suffix=":">
      <el-form-item label="验收牌号" prop="mark_id">
        <el-select
          v-model="form.mark_id"
          placeholder="验收牌号"
          style="width: 210px"
          class="filter-item"
          :disabled="crud.status.view > 0"
          @change="storChange3"
        >
          <el-option
            v-for="item in boms"
            :key="item.id"
            :label="item.code"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="试验批号" prop="accept_pcsn">
        <label slot="label">试验批号:</label>
        <el-input v-model="form.accept_pcsn" style="width: 210px" disabled class="input-with-select">
          <el-button slot="append" @click="createPcsn">生成</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="试验重量(g)" prop="accept_qty">
        <label slot="label">试验重量(g):</label>
        <el-input-number
          v-model="form.accept_qty"
          :controls="false"
          :precision="3"
          :min="0"
          style="width: 210px"
          @change="storChange4"
        />
      </el-form-item>
      <el-form-item label="碳平衡" prop="c_balance">
        <label slot="label">碳平衡:</label>
        <el-input-number
          v-model="form.c_balance"
          :controls="false"
          :precision="3"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="溶剂量(ml)" prop="liquid_qty">
        <label slot="label">溶剂量(ml):</label>
        <el-input-number
          v-model="form.liquid_qty"
          :controls="false"
          :precision="3"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>

      <el-form-item label="合金球(KG)" prop="ball_qty">
        <label slot="label">合金球(KG):</label>
        <el-input-number
          v-model="form.ball_qty"
          :controls="false"
          :precision="3"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="球磨转速(RPM)" prop="ball_speed">
        <label slot="label">球磨转速(RPM):</label>
        <el-input-number
          v-model="form.ball_speed"
          :controls="false"
          :precision="0"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="研磨时间(H)" prop="ball_time">
        <label slot="label">研磨时间(H):</label>
        <el-input-number
          v-model="form.ball_time"
          :controls="false"
          :precision="4"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="开单人" align="center">
        <el-input v-model="form.create_name" placeholder="开单人" style="width: 210px" disabled />
      </el-form-item>
      <el-form-item label="开单日期" align="center">
        <el-input v-model="form.create_time" placeholder="开单人" style="width: 210px" disabled />
      </el-form-item>
    </el-form>
    <div class="crud-opts2" style="margin-bottom: 5px;">
      <span class="crud-opts-right2">
        <!--左侧插槽-->
        <slot name="left" />
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="insertdtl"
        >
          开单计算
        </el-button>
      </span>
    </div>
    <el-table
      ref="table"
      :data="form.tableData"
      style="width: 100%;"
      size="mini"
      border
      :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
    >
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column prop="material_code" label="物料编码" align="center" />
      <el-table-column prop="material_name" label="物料名称" align="center" />
      <el-table-column prop="formula_qty" label="重量" align="center">
        <template slot-scope="scope">
          <el-input-number
            v-model="scope.row.formula_qty"
            size="mini"
            :controls="false"
            :precision="4"
            :max="100"
            :min="0"
            :disabled="scope.row.edit"
          />
        </template>
      </el-table-column>
      <el-table-column prop="dtl_pcsn" label="批次" align="center">
        <template slot-scope="scope">
          <el-input v-model="scope.row.dtl_pcsn" placeholder="批次" />
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script>

import Date from '@/utils/datetime'

const defaultForm = {
  formula_code: '',
  material_id: '',
  inspection_id: '',
  CJZB00001: 0,
  CJZB00002: 0,
  CJZB00006: 0,
  CJZB00020: 0,
  CJZB00021: 0,
  CJZB00004: 0,
  CJZB00007: 0,
  mark_id: '',
  create_name: '',
  create_time: (new Date()).strftime('%F', 'zh'),
  accept_pcsn: '',
  accept_qty: 1000,
  c_balance: 0,
  ball_rate: 0,
  liquid_rate: 0,
  ball_qty: 0,
  liquid_qty: 0,
  ball_time: 0,
  ball_speed: 0,
  tableData: []
}

import crudacceptformula from '@/api/wms/pf/acceptformula'
import CRUD, { form, crud } from '@crud/crud'

export default {
  name: 'AddDialog',
  components: {},
  mixins: [crud(), form(defaultForm)],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: false,
      dtlMaterShow: false,
      nowrow: {},
      nowindex: '',
      boms: [],
      THWs: [],
      PCSNs: [],
      rules: {
        material_id: [
          { required: true, message: '物料不能为空', trigger: 'blur' }
        ],
        inspection_id: [
          { required: true, message: '批号不能为空', trigger: 'blur' }
        ],
        mark_id: [
          { required: true, message: '验收牌号不能为空', trigger: 'blur' }
        ],
        accept_pcsn: [
          { required: true, message: '试验批号不能为空', trigger: 'blur' }
        ],
        accept_qty: [
          { required: true, message: '试验重量不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    }
  },
  created() {
    crudacceptformula.getBoms().then(res => {
      this.boms = res
    })
    crudacceptformula.getTHWs().then(res => {
      this.THWs = res
    })
  },
  methods: {
    [CRUD.HOOK.beforeSubmit]() {
      if (!this.form.mark_id) {
        this.crud.notify('验收牌号不能为空！', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
      if (!this.form.accept_pcsn) {
        this.crud.notify('试验批号不能为空！', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
      if (this.form.accept_qty <= 0) {
        this.crud.notify('试验重量不能为0！', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
      if (this.form.tableData.length === 0) {
        this.crud.notify('开单明细不能为空', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
    },
    [CRUD.HOOK.afterToEdit]() {
      // 编辑之后获取明细
      crudacceptformula.getDtl({ 'mark_id': this.form.mark_id }).then(res => {
        this.form.tableData = res
      })
    },
    storChange(row) {
      crudacceptformula.getPCSNs({ 'material_id': row }).then(res => {
        this.PCSNs = res
        this.form.inspection_id = ''
        this.form.CJZB00007 = '0'
        this.form.CJZB00001 = '0'
        this.form.CJZB00002 = '0'
        this.form.CJZB00006 = '0'
        this.form.CJZB00021 = '0'
        this.form.CJZB00020 = '0'
        this.form.CJZB00004 = '0'
      })
    },
    storChange2(row) {
      crudacceptformula.getPCSN({ 'inspection_id': row }).then(res => {
        this.form.CJZB00007 = res.CJZB00007
        this.form.CJZB00001 = res.CJZB00001
        this.form.CJZB00002 = res.CJZB00002
        this.form.CJZB00006 = res.CJZB00006
        this.form.CJZB00021 = res.CJZB00021
        this.form.CJZB00020 = res.CJZB00020
        this.form.CJZB00004 = res.CJZB00004
      })
    },
    storChange3(row) {
      crudacceptformula.getBom({ 'mark_id': row }).then(res => {
        this.form.c_balance = res.c_balance
        this.form.ball_rate = res.ball_rate
        this.form.liquid_rate = res.liquid_rate
        this.form.ball_time = res.ball_time
        this.form.ball_speed = res.ball_speed
        this.form.ball_qty = this.form.ball_rate * this.form.accept_qty / 100.0
        this.form.liquid_qty = this.form.liquid_rate * this.form.accept_qty / 100.0
      })
    },
    storChange4(row) {
      this.form.ball_qty = this.form.ball_rate * this.form.accept_qty / 100.0
      this.form.liquid_qty = this.form.liquid_rate * this.form.accept_qty / 100.0
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    close() {
      this.$emit('update:dialogShow', false)
    },
    handleEdit(index, row) {
      // 判断是否可以关闭编辑状态
      if (!row.edit) {
        if (parseFloat(row.standard_rate) <= 0) {
          this.crud.notify('标准值必须大于0！', CRUD.NOTIFICATION_TYPE.INFO)
          return false
        }
        if (!row.material_code) {
          this.crud.notify('物料不能为空！', CRUD.NOTIFICATION_TYPE.INFO)
          return false
        }
      }
      row.edit = !row.edit
      this.form.tableData.splice(index, 1, row) // 通过splice 替换数据 触发视图更新
    },
    insertdtl() {
      this.form.tableData.push({ material_id: '', material_code: '', material_name: '', standard_rate: '0', seqno: '', edit: false })
    },
    createPcsn() {
      crudacceptformula.createPcsn().then(res => {
        this.form.accept_pcsn = res.accept_pcsn
      })
    }
  }
}
</script>

<style scoped>
.crud-opts2 {
  padding: 4px 0;
  display: -webkit-flex;
  display: flex;
  align-items: center;
}

.crud-opts2 .crud-opts-right2 {
  margin-left: auto;
}

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 0px;
}
</style>
