<template>
  <div class="app-container">
    <el-card>
        <div>
      <el-input
              v-model="listQuery.search"
              placeholder="物料名称/物料编号"
              style="width: 300px"
              class="filter-item"
              @keyup.enter.native="handleFilter"
            />
            <el-button
              class="filter-item"
              type="primary"
              icon="el-icon-search"
              @click="handleFilter"
              >搜索</el-button
            >
            <el-button
              class="filter-item"
              type="primary"
              icon="el-icon-refresh-left"
              @click="resetFilter"
              >重置</el-button
            >
    </div>
    <div style="margin-top: 10px">
      <el-button type="primary" icon="el-icon-plus" @click="handleCreate"
        >新增物料</el-button
      >
    </div>
    </el-card>
    <el-card style="margin-top: 10px">
      <el-table
        v-loading="listLoading"
        :data="materialList.results"
        border
        fit
        stripe
       highlight-current-row
        max-height="600"
         height="100"
        v-el-height-adaptive-table="{bottomOffset: 50}"
      >
        <el-table-column type="index" width="50" />
         <el-table-column label="物料编号">
          <template slot-scope="scope">{{ scope.row.number }}</template>
        </el-table-column>

       <el-table-column label="物料类别"  :filters="[{ text: '成品', value: 1 }, { text: '半成品', value: 2 },{ text: '主要原料', value: 3 }, { text: '辅助原料', value: 4 }]"
      :filter-method="filterTag"
      filter-placement="bottom-end">
          <template slot-scope="scope">    {{options_[scope.row.type]}}</template>
        </el-table-column>
      
        <el-table-column label="物料名称">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
       
        <el-table-column label="规格型号">
          <template slot-scope="scope">{{ scope.row.specification }}</template>
        </el-table-column>
        
        <el-table-column label="计量单位">
          <template slot-scope="scope">{{ scope.row.unit }}</template>
        </el-table-column>
       <el-table-column label="创建时间">
          <template slot-scope="scope">{{ scope.row.create_time }}</template>
        </el-table-column>

        <el-table-column
          align="center"
          label="操作"
          width="220px"
        >
          <template slot-scope="scope">
            <el-link
              v-if="checkPermission(['material_update'])"
              @click="handlebind(scope)"
              >检查表</el-link
            >
            <el-link
              v-if="checkPermission(['material_update'])"
              @click="handleEdit(scope)"
              >编辑</el-link
            >
            <el-link
              v-if="checkPermission(['material_delete'])"
              type="danger"
              @click="handleDelete(scope)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="materialList.count > 0"
        :total="materialList.count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? '编辑物料' : '新增物料'"
    >
      <el-form
        ref="Form"
        :model="material"
        label-width="80px"
        label-position="right"
        :rules="rule1"
      >
        <el-form-item label="物料名称" prop="name">
          <el-input v-model="material.name" placeholder="物料名称" />
        </el-form-item>
        <el-form-item label="物料编号" prop="number">
          <el-input v-model="material.number" placeholder="物料编号" />
        </el-form-item>
         <el-form-item label="规格型号" prop="specification">
          <el-input v-model="material.specification" placeholder="规格型号" />
        </el-form-item>
        
        <el-form-item label="计量单位" prop="unit">
        <el-select  style="width: 100%" v-model="material.unit" placeholder="请选择">
             <el-option
                v-for="item in unitoptions"
                :key="item.value"
                :label="item.label"
               :value="item.value">
              </el-option>
          </el-select>
        </el-form-item>
      
          <el-form-item label="物料类别" prop="type">
         
           <el-select  style="width: 100%" v-model="material.type" placeholder="请选择" >
             <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
               :value="item.value">
              </el-option>
          </el-select>
        </el-form-item>
      
        <el-form-item label="排序" prop="sort_str">
          <el-input v-model="material.sort_str" placeholder="排序" />
        </el-form-item>
        
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm('Form')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getMaterialList, createMaterial,updateMaterial,deleteMaterial,getProcessList } from "@/api/mtm";
import checkPermission from "@/utils/permission";


import { genTree } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
const defaultmaterial = {
  name: "",
  number: "",
  processes:[],
};
export default {
  components: { Pagination },
  data() {
    return {
      material: defaultmaterial,
      materialList: {
        count: 0,
      },
      processOptions:[],
       options_:{
        
        "1":'成品',
        "2":'半成品',
        "3":'主要原料',
        "4":'辅助原料',
         "5":'加工工具',
        "6":'辅助工具',
       
      },
      options: [{
          value: 1,
          label: '成品'
        }, {
          value: 2,
          label: '半成品'
        }, {
          value: 3,
          label: '主要原料'
        }, {
          value: 4,
          label: '辅助原料'
        }, {
          value: 5,
          label: '加工工具'
        }, {
          value: 6,
          label: '辅助工具'
        }],
        unitoptions:[
          {
          value: '块',
          label: '块'
        }, {
          value: '套',
          label: '套'
        },
        ],
      listQuery: {
        page: 1,
        page_size: 20,
      },
     
      listLoading: true,
      dialogVisible: false,
      dialogType: "new",
      rule1: {
        name: [{ required: true, message: "请输入", trigger: "blur" }],
        number: [{ required: true, message: "请输入", trigger: "blur" }],
       
      },
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getList();
    this.getProcessList();
   
  },
  methods: {
    checkPermission,
    //物料列表
    getList() {
      this.listLoading = true;
      getMaterialList(this.listQuery).then((response) => {
        if (response.data) {
          this.materialList = response.data;
        }
        this.listLoading = false;
      });
    },
    filterTag(value, row) {
        return row.type === value;
        
      },
    //工序清单
 getProcessList() {
      getProcessList().then((res) => {
        this.processOptions = res.data.results;
      });
    },
    //绑定工序
    handlebind(scope)
    {
    
      this.$router.push({name: "MaterialDO", params: { id: scope.row.id }, })
    }
,
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        page_size: 20,
      }
      this.getList();
    },
    handleCreate() {
      this.material = Object.assign({}, defaultmaterial);
      this.dialogType = "new";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
   
    handleEdit(scope) {
      this.material = Object.assign({}, scope.row); // copy obj
      this.dialogType = "edit";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    handleDelete(scope) {
      this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(async () => {
          await deleteMaterial(scope.row.id);
          this.getList();
          this.$message.success("成功");
        })
        .catch((err) => {
          console.error(err);
        });
    },
   
    async confirm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const isEdit = this.dialogType === "edit";
          if (isEdit) {
            updateMaterial(this.material.id, this.material).then((res) => {
              if (res.code >= 200) {
                this.getList();
                this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
          } else {
            createMaterial(this.material).then((res) => {
              if (res.code >= 200) {
                this.getList();
                 this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>
