<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    width="1000px"
    :size="1000"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      ref="dialogForm"
      :model="form"
      :rules="rules"
      label-width="120px"
    >
      <el-row>
        <el-col :md="12" :sm="24">
          <el-form-item label="批次号">
            <el-input disabled v-if="mode=='edit'" v-model="form.batch"></el-input>
            <el-select 
            v-else
              v-model="form.batch" 
              style="width: 100%"
              :disabled="mode=='edit'">
              <el-option
                v-for="item in wmaterialOptions"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item prop="type" label="操作人">
            <el-input disabled v-if="mode=='edit'" v-model="form.handle_user_name"></el-input>
            <el-select
            v-else
              v-model="form.handle_user"
              placeholder="操作人"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="item in userList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="生产设备">
            <el-select 
              v-model="form.equipment" 
              style="width: 100%">
              <el-option
                v-for="item in equipmentOptions"
                :key="item.id"
                :label="item.full_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="领用数量">
            <el-input-number
              v-model="form.count_use"
              controls-position="right"
              :min="0"
              :step="1"
              :step-strictly="true"
              style="width: 100%"
              placeholder="请输入领用数量"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="加工数量">
            <el-input-number
              v-model="form.count_real"
              controls-position="right"
              :min="0"
              :step="1"
              :step-strictly="true"
              style="width: 100%"
              placeholder="请输入加工数量"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="合格数">
            <el-input-number
              v-model="form.count_ok"
              controls-position="right"
              :min="0"
              :step="1"
              :step-strictly="true"
              style="width: 100%"
              placeholder="请输入合格数"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="班次" prop="material">
            <el-select
              v-model="form.shift"
              placeholder="班次"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="item in shiftOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- <el-col :md="12" :sm="24">
          <el-form-item label="操作日期" prop="handle_date">
            <el-date-picker
              v-model="form.handle_date"
              type="date"
              placeholder="操作日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              style="width: 100%"
            />
          </el-form-item>
        </el-col> -->
      </el-row>
      <div class="formTitle">不合格原因及数量:</div>
      <el-row>
        <el-col :md="12" :sm="24">
          <el-form-item label="椭圆/弯曲">
            <el-input-number
              v-model="form.count_n_wq"
              controls-position="right"
              :min="0"
              :step="1"
              :step-strictly="true"
              style="width: 100%"
              placeholder="椭圆/弯曲"
              @change="countNotOkSun"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="炸纹">
            <el-input-number
              v-model="form.count_n_zw"
              controls-position="right"
              :min="0"
              :step="1"
              :step-strictly="true"
              style="width: 100%"
              placeholder="炸纹"
              @change="countNotOkSun"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="条纹">
            <el-input-number
              v-model="form.count_n_tw"
              controls-position="right"
              :min="0"
              :step="1"
              :step-strictly="true"
              style="width: 100%"
              placeholder="条纹"
              @change="countNotOkSun"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="气泡">
            <el-input-number
              v-model="form.count_n_qp"
              controls-position="right"
              :min="0"
              :step="1"
              :step-strictly="true"
              style="width: 100%"
              placeholder="气泡"
              @change="countNotOkSun"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="断裂">
            <el-input-number
              v-model="form.count_n_dl"
              controls-position="right"
              :min="0"
              :step="1"
              :step-strictly="true"
              style="width: 100%"
              placeholder="断裂"
              @change="countNotOkSun"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="偏壁">
            <el-input-number
              v-model="form.count_n_pb"
              controls-position="right"
              :min="0"
              :step="1"
              :step-strictly="true"
              style="width: 100%"
              placeholder="偏壁"
              @change="countNotOkSun"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="大小头">
            <el-input-number
              v-model="form.count_n_dxt"
              controls-position="right"
              :min="0"
              :step="1"
              :step-strictly="true"
              style="width: 100%"
              placeholder="大小头"
              @change="countNotOkSun"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="结石">
            <el-input-number
              v-model="form.count_n_js"
              controls-position="right"
              :min="0"
              :step="1"
              :step-strictly="true"
              style="width: 100%"
              placeholder="结石"
              @change="countNotOkSun"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="气线">
            <el-input-number
              v-model="form.count_n_qx"
              controls-position="right"
              :min="0"
              :step="1"
              :step-strictly="true"
              style="width: 100%"
              placeholder="气线"
              @change="countNotOkSun"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="其他">
            <el-input-number
              v-model="form.count_n_qt"
              controls-position="right"
              :min="0"
              :step="1"
              :step-strictly="true"
              style="width: 100%"
              placeholder="其他"
              @change="countNotOkSun"
            >
            </el-input-number>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="合计">
            <el-input-number
              v-model="form.count_notok"
              controls-position="right"
              style="width: 100%"
              placeholder="合计"
              disabled
            >
            </el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-footer>
      <el-button type="primary" v-loading="isSaveing" @click="submit"
        >确定</el-button>
      <el-button @click="visible = false">取消</el-button>
    </el-footer>
  </el-dialog>
</template>
<script>
export default {
  emits: ["success", "closed"],
  props: {
    mgroup:{ type: String, default: "" },
    mtask: { type: String, default: "" },
    handledate: { type: String, default: "" },
    material_out: { type: String, default: "" },
  },
  data() {
    return {
      loading: false,
      mode: "add",
      titleMap: {
        add: "新增记录",
        edit: "编辑记录",
      },
      form: {
        shift:'',
        batch:'',
        count_ok: 0,
        count_real: 0,
        count_use: 0,
        count_notok: 0,
        handle_date:'',
        count_n_zw:0,
        count_n_tw:0,
        count_n_qp:0,
        count_n_wq:0,
        count_n_dl:0,
        count_n_pb:0,
        count_n_dxt:0,
        count_n_js:0,
        count_n_qx:0,
        count_n_qt:0,
        handle_user:''
      },
      mlogb: [],
      userList: [],
      batchOptions:[],
      equipmentOptions: [],
      wmaterialOptions: [],
      shiftOptions: [],
      routeOptions:[],
      visible: false,
      isSaveing: false,
      setFiltersVisible: false,
      handle_name: "",
      leader_name: "",
      count_n_all: null,
      rules:{

      },
    };
  },
  mounted() {
    this.getBatchs();
    this.getUserList();
    this.getEquipment();
    this.getWMaterial();
    this.getShiftOptions();
    // let date = new Date();
    // let year = date.getFullYear();
    // let month = date.getMonth() + 1;
    // let day = date.getDate();
    // this.form.handle_date = year + "-" + month + "-" + day;
    this.form.material_out = this.material_out;
    this.form.handle_date = this.handle_date;
    this.form.mgroup = this.mgroup;
  },
  methods: {
    getRouteOptions() {
      this.$API.mtm.route.list.req({ page: 0, mgroup: this.mgroup}).then(res => {
        for (var i = 0; i < res.length; i++) {
          if (res[i].material_in == null) {
            res[i].material_in_name = ''
          }
          if (res[i].material_out == null) {
            res[i].material_out_name = ''
          }
          res[i].label = res[i].material_in_name + '->' + res[i].material_out_name
        }
        this.routeOptions = res;
      })
    },
    getShiftOptions() {
      this.$API.mtm.shift.list.req({ page: 0 }).then((res) => {
        this.shiftOptions = res;
      });
    },
    getWMaterial(){
      this.$API.wpm.wmaterial.list.req({belong_dept__name: '6车间',page:0}).then(res=>{
        if(res.length>0){
          let arr = [];
          res.forEach(item => {
            // if(item.count>0){}
            arr.push(item.batch)
          });
          this.wmaterialOptions = arr;
        }
      })
    },
    //获取车间物料批次号
    getBatchs() {
      let that = this;
      this.$API.wpm.wmaterial.batchs.req({belong_dept_name:"6车间"}).then(res=>{
        that.batchOptions = res;
      });
    },
    getUserList() {
      let that = this;
      this.$API.system.user.list
        .req({ belong_dept__name: '6车间', page: 0 })
        .then((res) => {
          that.userList = res;
        });
    },
    
    //获取设备列表
    getEquipment() {
      this.$API.em.equipment.list.req({ page: 0, type: 10 }).then((res) => {
        this.equipmentOptions = res;
      });
    },
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    countNotOkSun() {
      this.form.count_notok =
      Number(this.form.count_n_zw) +
      Number(this.form.count_n_tw) +
      Number(this.form.count_n_qp) +
      Number(this.form.count_n_wq) +
      Number(this.form.count_n_dl) +
      Number(this.form.count_n_pb) +
      Number(this.form.count_n_dxt) +
      Number(this.form.count_n_js) +
      Number(this.form.count_n_qx) +
      Number(this.form.count_n_qt);
    },
    //提交
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          let sun = this.form.count_ok+this.form.count_notok;
          if(sun==this.form.count_real){
            if (this.mode == "add") {
              this.form.mtask = this.mtask;
              this.form.handle_date = this.handledate;
              this.$API.wpm.mlog.create
                .req(this.form)
                .then((res) => {
                  this.isSaveing = false;
                  this.$emit("success", this.form, this.mode);
                  this.visible = false;
                  this.$message.success("操作成功");
                })
                .catch((err) => {
                  //可以处理校验错误
                  this.isSaveing = false;
                  return err;
                });
            } else if (this.mode == "edit") {
              this.$API.wpm.mlog.update
                .req(this.form.id, this.form)
                .then((res) => {
                  this.isSaveing = false;
                  this.$emit("success", this.form, this.mode);
                  this.visible = false;
                  this.$message.success("操作成功");
                }).catch((err) => {
                  //可以处理校验错误
                  this.isSaveing = false;
                  return err;
                });
            }
          }else{
            this.isSaveing = false;
              this.$message.error("合格总数与不合格总数与加工数量不对等");
          }
          
        }
      });
    },
    //表单注入数据
    setData(data) {
      Object.assign(this.form, data);
      this.getRouteOptions();
    },
    //设置过滤项
    setFilters(filters) {
      this.selectionFilters = filters;
      this.setFiltersVisible = true;
    },
    //
    countChange() {
      if (
        this.form.count_real !== null &&
        this.form.count_real !== "" &&
        this.form.count_real !== 0
      ) {
        if (
          this.form.count_ok !== null &&
          this.form.count_ok !== "" &&
          this.form.count_ok !== 0
        ) {
          this.form.rate =
            (this.form.count_ok / this.form.count_real) * 100 + "%";
        } else {
          this.form.rate = null;
        }
      } else {
        this.form.rate = null;
      }
    },
  },
};
</script>
<style scoped>
.formTitle {
  margin-bottom: 10px;
  font-weight: 600;
}
</style>
  