<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-input>
      </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"
        >
          <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"
        >
          <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-input>
      </el-form-item>
      <el-form-item label="CT(%)" prop="c_balance">
        <label slot="label">CT(%):</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="Cr3C2" prop="c_balance">
        <label slot="label">Cr3C2:</label>
        <el-input-number
          v-model="form.liquid_rate"
          :controls="false"
          :precision="3"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="氧含量" prop="c_balance">
        <label slot="label">氧含量:</label>
        <el-input-number
          v-model="form.ball_rate"
          :controls="false"
          :precision="3"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>

      <el-form-item label="VC(%)" prop="c_balance">
        <label slot="label">VC(%):</label>
        <el-input-number
          v-model="form.ball_speed"
          :controls="false"
          :precision="3"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="CF(%)" prop="c_balance">
        <label slot="label">CF(%):</label>
        <el-input-number
          v-model="form.ball_time"
          :controls="false"
          :precision="0"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="FSSS(μm)">
        <label slot="label">FSSS(μm):</label>
        <el-input-number
          v-model="form.total_value"
          :controls="false"
          :precision="4"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="BET(㎡/g)">
        <label slot="label">BET(㎡/g):</label>
        <el-input-number
          v-model="form.total_value2"
          :controls="false"
          :precision="4"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="明细数">
        <el-input v-model="form.tableData.length" :disabled="true" style="width: 210px;" />
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <label slot="label">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注:</label>
        <el-input v-model="form.remark" style="width: 380px;" rows="2" type="textarea" :disabled="crud.status.view > 0" />
      </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="standard_rate" label="标准值%" align="center">
        <template slot-scope="scope">
          <el-input-number
            v-model="scope.row.standard_rate"
            size="mini"
            :controls="false"
            :precision="4"
            :max="100"
            :min="0"
            :disabled="scope.row.edit"
          />
        </template>
      </el-table-column>
      <el-table-column prop="seqno" label="顺序号" align="center" />
    </el-table>
  </el-dialog>
</template>

<script>

const defaultForm = {
  formula_code: '',
  material_id: '',
  inspection_id: '',
  c_balance: 0,
  liquid_rate: 0,
  ball_rate: 0,
  ball_speed: 0,
  ball_time: 0,
  detail_count: 0,
  manufactor: '',
  total_value: 0,
  total_value2: 0,
  remark: '',
  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: {},
      boms: [],
      THWs: [],
      PCSNs: [],
      nowindex: '',
      rules: {
        material_id: [
          { required: true, message: '牌号不能为空', trigger: 'blur' }
        ],
        inspection_id: [
          { required: true, message: '批次不能为空', trigger: 'blur' }
        ],
        c_balance: [
          { required: true, message: '碳平衡不能为空', trigger: 'blur' }
        ],
        liquid_rate: [
          { required: true, message: '液料比不能为空', trigger: 'blur' }
        ],
        ball_rate: [
          { required: true, message: '球料比不能为空', trigger: 'blur' }
        ],
        ball_speed: [
          { required: true, message: '球磨转速不能为空', trigger: 'blur' }
        ],
        ball_time: [
          { 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
    })
    crudacceptformula.getPCSNs().then(res => {
      this.PCSNs = res
    })
  },
  methods: {
    [CRUD.HOOK.beforeSubmit]() {
      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
      })
    },
    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() {
    }
  }
}
</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>
