<template>
  <el-dialog
    title="检验"
    v-model="visible"
    :size="1200"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-container v-loading="loading">
      <el-main style="padding: 0 20px 20px 20px">
        <el-form
          ref="dialogForm"
          :model="form"
          :rules="rules"
          label-width="140px"
        >
          <!-- 半成品 -->
          <el-row v-if="type=='do_in'&&cate=='halfgood'">
            <el-col :md="12" :sm="24">
              <el-form-item label="检验日期" prop="test_date">
                <el-date-picker
                  v-model="form.test_date"
                  type="date"
                  value-format="YYYY-MM-DD"
                  style="width:100%"
                />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="检验人" prop="test_user">
                <el-select 
                  v-model="form.test_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="不合格数" prop="count_notok">
                <el-input-number v-model="form.count_notok" :min="0" style="width:100%" controls-position="right">
                </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" :min="0" style="width:100%" controls-position="right">
                </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" :min="0" style="width:100%" controls-position="right">
                </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" :min="0" style="width:100%" controls-position="right">
                </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_wq" :min="0" style="width:100%" controls-position="right">
                </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" :min="0" style="width:100%" controls-position="right">
                </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" :min="0" style="width:100%" controls-position="right">
                </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" :min="0" style="width:100%" controls-position="right">
                </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" :min="0" style="width:100%" controls-position="right">
                </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" :min="0" style="width:100%" controls-position="right">
                </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" :min="0" style="width:100%" controls-position="right">
                </el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 成品 -->
          <el-row v-if="type=='do_in'&&cate=='good'">
            <el-col :md="12" :sm="24">
              <el-form-item label="批次号：">
                {{ objitem.batch }}
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="总数：">
                {{ objitem.count }}
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="检验日期" prop="test_date">
                <el-date-picker
                  v-model="form.test_date"
                  type="date"
                  value-format="YYYY-MM-DD"
                  style="width:100%"
                />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="抽检数">
                <el-input-number v-model="form.count" :min="0" style="width:100%" @change="countChange" controls-position="right">
                </el-input-number>
              </el-form-item>
            </el-col>
            <!-- 预制棒 -->
            <sc-form-table
              v-model="form.ftestitems"
              :addTemplate="addTemplate"
              placeholder="暂无数据"
            >
              <el-table-column prop="val" label="抽检编号">
                <template #default="scope">
                  <span v-if="scope.row.id">{{scope.row.name}}</span>
                  <el-input v-else v-model="scope.row.name" placeholder="名称"></el-input>
                </template>
              </el-table-column>
              <el-table-column label="检验记录" align="center">
                <el-table-column prop="test_val" label="长度/mm">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="直径/mm">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="椭圆度/mm">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="锥度/mm">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="结石、沙石、黑点">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="条纹">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="气泡">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
              </el-table-column>
              <el-table-column prop="test_val" label="合格判定" min-width="120" align="center">
                <template #default="scope">
                  <el-select 
                    v-model="scope.row.is_ok"
                    placeholder="是否合格"
                    clearable
                    style="width:100%"
                  >
                    <el-option label="是" value="true"/>
                    <el-option label="否" value="false"/>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column prop="test_user" label="检验人" min-width="120" align="center">
                <template #default="scope">
                  <el-select 
                    v-model="scope.row.test_user"
                    placeholder="检验人"
                    clearable
                    style="width:100%"
                  >
                    <el-option
                    v-for="item in userList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column prop="open" label="操作" width="80" align="center">
                <template #default="scope">
                  <el-button
                    text
                    type="danger"
                    size="small"
                    @click="userPostDel(scope.row.id)"
                    >删除</el-button>
                </template>
              </el-table-column>
            </sc-form-table>
            <!-- 预制管 -->
            <sc-form-table
              v-model="form.ftestitems"
              :addTemplate="addTemplate"
              placeholder="暂无数据"
            >
              <el-table-column prop="val" label="抽检编号">
                <template #default="scope">
                  <span v-if="scope.row.id">{{scope.row.name}}</span>
                  <el-input v-else v-model="scope.row.name" placeholder="名称"></el-input>
                </template>
              </el-table-column>
              <el-table-column label="检验记录" align="center">
                <el-table-column prop="test_val" label="长度/mm">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="内径/mm">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="椭圆度/mm">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="壁厚/mm">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="壁厚偏差/mm">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="锥度/mm">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="气泡(线)">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="结石、杂志">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="条纹">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="划伤">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
                <el-table-column prop="test_val" label="水雾">
                  <template #default="scope">
                    <el-input-number v-model="scope.row.test_val" :min="0" style="width:100%" controls-position="right">
                    </el-input-number>
                  </template>
                </el-table-column>
              </el-table-column>
              <el-table-column prop="test_val" label="合格判定" min-width="120" align="center">
                <template #default="scope">
                  <el-select 
                    v-model="scope.row.is_ok"
                    placeholder="是否合格"
                    clearable
                    style="width:100%"
                  >
                    <el-option label="是" value="true"/>
                    <el-option label="否" value="false"/>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column prop="test_user" label="检验人" min-width="120" align="center">
                <template #default="scope">
                  <el-select 
                    v-model="scope.row.test_user"
                    placeholder="检验人"
                    clearable
                    style="width:100%"
                  >
                    <el-option
                    v-for="item in userList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column prop="open" label="操作" width="80" align="center">
                <template #default="scope">
                  <el-button
                    text
                    type="danger"
                    size="small"
                    @click="userPostDel(scope.row.id)"
                    >删除</el-button>
                </template>
              </el-table-column>
            </sc-form-table>
          </el-row>
          <!-- 入厂检验 -->
          <el-row v-if="type=='pur_in'">
            <el-col :md="12" :sm="24">
              <el-form-item label="产品名称:">
                {{ objitem.material_name }}
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="入厂批次号:">
                {{ objitem.batch }}
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="总袋（桶）数:">
                {{ objitem.count }}
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="抽样数量">
                <el-input v-model="form.weight_kgs_count" disabled placeholder="抽样数量"/>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="称重记录/Kg">
                <el-button type="primary" icon="el-icon-plus" @click="addWeight"></el-button>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24" v-for="(item,ind) in weight_kgs" :key="item" style="position:relative">
              <el-form-item :label-width="50">
                <el-input-number v-model="item.value" :min="0" style="width:90%" controls-position="right" @change="countSun"/>
              </el-form-item>
              <el-button @click="delWeight(ind)" type="danger" icon="el-icon-delete" circle style="position:absolute;right:-10px;top:0"/>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="抽样计算总重量/Kg">
                <el-input-number v-model="form.count_bag_weight_all" :min="0" style="width:100%" controls-position="right">
                </el-input-number>
              </el-form-item>
            </el-col>
            <!-- <el-col :md="12" :sm="24">
              <el-form-item label="合同采购量/Kg">
                <el-input-number v-model="form.count" :min="0" style="width:100%" controls-position="right">
                </el-input-number>
              </el-form-item>
            </el-col> -->
            <el-col :md="12" :sm="24">
              <el-form-item label="检验合格" prop="is_testok">
                <el-select 
                  v-model="form.is_testok"
                  placeholder="检验合格"
                  clearable
                  style="width:100%"
                >
                  <el-option label="是" :value="true"/>
                  <el-option label="否" :value="false"/>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- <el-col :md="12" :sm="24" prop="is_testok">
              <el-form-item label="结论判定" prop="is_testok">
                <el-select 
                  v-model="form.is_testok"
                  placeholder="结论判定"
                  clearable
                  style="width:100%"
                >
                  <el-option label="是" value="true"/>
                  <el-option label="否" value="false"/>
                </el-select>
              </el-form-item>
            </el-col> -->
            <el-col :md="12" :sm="24">
              <el-form-item label="检验日期" prop="test_date">
                <el-date-picker
                  v-model="form.test_date"
                  type="date"
                  value-format="YYYY-MM-DD"
                  style="width:100%"
                />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="检验员" prop="test_user">
                <el-select 
                  v-model="form.test_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-row>
        </el-form>
      </el-main>
      <el-footer v-if="mode=='add'">
        <el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
        <el-button @click="visible = false">取消</el-button>
      </el-footer>
    </el-container>
  </el-dialog>
</template>
<script>
export default {
  emits: ["success", "closed"],
  props: {
    mioitemId: { type: String, default: "" },
    type: { type: String, default: "" },
    cate: { type: String, default: "" },
    objitem: { type: Object, default: () => {}},
	},
  data() {
    return {
      addTemplate: {
        name: "",
        sort: "",
        user: "",
        post: "",
        dept: "",
      },
      loading: false,
      form: {
        weight_kgs:[],
        ftestitems:[],
      },
      rules: {
          test_date: [{required: true, message: "请选择检验日期", trigger: "blur"}],
          test_user: [{required: true, message: "请选择检验人", trigger: "blur"}],
          count_notok: [{required: true, message: "请填写不合格数", trigger: "blur"}],
        },
        mioItem: {},
      weight_kgs:[{value:0}],
      visible: false,
      isSaveing: false,
      userList:[],
      processOptions: [],
      deptOptions: [],
      selectionFilters: [],
      setFiltersVisible: false,
      belong_dept_options: [],
      group: [],
    };
  },
  mounted() {
    this.getUserList();
    let that = this;
    if(that.type=='pur_in'){
      that.form.material_name = that.objitem.material_name;
      that.form.batch = that.objitem.batch;
      that.form.count_bag = that.objitem.count;
    }
  },
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    //获取员工
    getUserList(){
      let that = this;
      this.$API.system.user.list.req({page_size:20,page:1}).then(res=>{
        that.userList = res.results;
      });
    },
    addWeight(){
      this.weight_kgs.push({value:0});
      this.form.weight_kgs_count = this.weight_kgs.length;
    },
    delWeight(index){
      this.weight_kgs.splice(index,1);
      this.form.weight_kgs_count = this.weight_kgs.length;
    },
    countSun(){
      let that = this;
      let sum = 0;
      console.log('change')
      that.weight_kgs.forEach(item=>{
        sum = sum+item.value;
      })
      that.form.count_bag_weight_all = sum/that.weight_kgs.length*that.objitem.count;
    },
    countChange(){

    },
    //提交
    submit() {
      let that = this;
      that.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          that.isSaveing = true;
          try {
            if(that.type=='pur_in'){
              console.log(that.form);
              let arr = [];
              that.weight_kgs.forEach(item=>{
                arr.push(item.value);
              })
              let obj = {};
              obj.test_date = that.form.test_date;
              obj.test_user = that.form.test_user;
              obj.count_bag = that.objitem.count;
              obj.weight_kgs = arr;
              obj.is_testok = that.form.is_testok;
              console.log(obj)
              that.$API.inm.mioitem.testpurin.req(that.mioitemId,obj).then(res=>{
                that.isSaveing = false;
                that.$emit("success");
                that.visible = false;
                that.$message.success("操作成功");
              })
            }else{
              that.$API.inm.mioitem.test.req(that.mioitemId,that.form).then(res=>{
                that.isSaveing = false;
                that.$emit("success");
                that.visible = false;
                that.$message.success("操作成功");
              })
            }
            
          } catch (err) {
            //可以处理校验错误
            this.isSaveing = false;
            return err;
          }
        }
      });
    },
    //表单注入数据
    setData(data) {
      let that = this;
      Object.assign(that.form, data);
      // that.form.test_group = that.form.split(',');
      that.weight_kgs = [];
      console.log(data.weight_kgs)
      if(data.weight_kgs.length>0){
        that.form.weight_kgs_count = data.weight_kgs.length;
       let sum = 0;
        data.weight_kgs.forEach((item)=>{
          sum = sum+item;
          let obj = {};
          obj.value = item;
          that.weight_kgs.push(obj)
        })
        that.form.count_bag_weight_all =sum/data.weight_kgs.length*data.count;
      }

    },
    //设置过滤项
    setFilters(filters) {
      this.selectionFilters = filters;
      this.setFiltersVisible = true;
    },
  },
};
</script>

<style>
</style>
