<template>
  <div class="app-container">
    <el-card>
      <div>
          <el-button type="primary" icon="el-icon-plus" @click="handleCreate"
        >新增设备</el-button
      >
      <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>
  
    </el-card>
    <el-card style="margin-top: 2px">
      <el-table
        v-loading="listLoading"
        :data="equipmentList.results"
        border
        fit
        stripe
        highlight-current-row
       height="100"
        v-el-height-adaptive-table="{bottomOffset: 50}"
      >
        <el-table-column type="index" width="50" />
        <el-table-column label="设备名称" width="120"  show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
       
        <el-table-column label="设备编号" width="120">
          <template slot-scope="scope">{{ scope.row.number }}</template>
        </el-table-column>
         <el-table-column label="型号规格" width="120">
          <template slot-scope="scope">{{ scope.row.model }}</template>
        </el-table-column>
          <el-table-column label="生产厂及国别" width="120" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.factory }}</template>
        </el-table-column>
         <el-table-column label="生产日期"  width="120">
          <template slot-scope="scope">{{ scope.row.production_date }}</template>
        </el-table-column>
        <el-table-column label="购置日期"  width="120">
          <template slot-scope="scope">{{ scope.row.buy_date }}</template>
        </el-table-column>
         <el-table-column label="数量" width="120">
          <template slot-scope="scope">{{ scope.row.count }}</template>
        </el-table-column>
          <el-table-column label="状态" width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.state===0" type="success">
            {{ state_[scope.row.state] }}
            </el-tag>
            <el-tag v-else-if="scope.row.state===1" type="warning">
            {{ state_[scope.row.state] }}
            </el-tag>
            <el-tag v-else-if="scope.row.state===2" >
            {{ state_[scope.row.state] }}
            </el-tag>
             <el-tag v-else type="danger">
            {{ state_[scope.row.state] }}
            </el-tag>
            </template>
        </el-table-column>
         <el-table-column label="技术指标"  width="120" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.parameter }}</template>
        </el-table-column>
        <el-table-column label="保管人"  width="120">
          <template slot-scope="scope" v-if="scope.row.keeper_">{{ scope.row.keeper_.username }}</template>
        </el-table-column>
         <el-table-column label="存放位置"  width="120" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.place }}</template>
        </el-table-column>
          <el-table-column label="绑定工序" width="120" show-overflow-tooltip>
           <template slot-scope="scope" v-if="scope.row.step_">
            <el-tag v-for="item in scope.row.step_"
                    :key="item.number"
                    :label="item.name"
                    :value="item.number">{{item.name}}</el-tag>


          </template>
        </el-table-column>
        <el-table-column label="备注" width="120" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.description }}</template>
        </el-table-column>
        

        <el-table-column
          align="center"
          label="操作"
          width="120px"
            fixed="right"
        >
          <template slot-scope="scope">
           
            <el-link
              v-if="checkPermission(['equipment_update'])"
              @click="handleEdit(scope)"
              >编辑</el-link
            >
            <el-link
              v-if="checkPermission(['equipment_delete'])"
              type="danger"
              @click="handleDelete(scope)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="equipmentList.count > 0"
        :total="equipmentList.count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :title="dialogType === 'edit' ? '编辑设备' : '新增设备'"
    >
      <el-form
        ref="Form"
        :model="equipment"
        label-width="100px"
        label-position="right"
        :rules="rule1"
      >
      <el-row>
        <el-col :span="12">
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="equipment.name" placeholder="设备名称" />
        </el-form-item>
        </el-col>
         <el-col :span="12">
        <el-form-item label="设备编号" prop="number">
          <el-input v-model="equipment.number" placeholder="设备编号" />
        </el-form-item>
         </el-col>
         </el-row>
          <el-row>
        <el-col :span="12">
         <el-form-item label="型号规格" prop="model">
          <el-input v-model="equipment.model" placeholder="规格型号规格" />
        </el-form-item>
         </el-col>
           <el-col :span="12">
               <el-form-item label="数量" prop="count">
          <el-input v-model="equipment.count" placeholder="数量" />
        </el-form-item>
              </el-col>
         </el-row>
          <el-form-item label="厂商及国别" prop="factory">
          <el-input v-model="equipment.factory" placeholder="生产厂商及国别" />
        </el-form-item>
         <el-row>
        <el-col :span="12">
         <el-form-item label="生产日期" prop="production_date">
          <el-date-picker
            v-model="equipment.production_date"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width:100%"
          >
          </el-date-picker>
        </el-form-item>
         </el-col>
         <el-col :span="12">
        <el-form-item label="购置日期" prop="buy_date">
          <el-date-picker
            v-model="equipment.buy_date"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width:100%"
          >
          </el-date-picker>
        </el-form-item>
          </el-col>
         </el-row>
         <el-form-item label="状态" prop="state">
         
           <el-select  style="width: 100%" v-model="equipment.state" 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="parameter">
            
          <el-input
            type="textarea"
            :rows="4"
            v-model="equipment.parameter"
            placeholder="技术指标"
          />
        </el-form-item>
          <el-form-item label="存放位置" prop="place">
          <el-input v-model="equipment.place" placeholder="存放位置" />
        </el-form-item>
          <el-row>
        <el-col :span="12">
           <el-form-item label="所属部门" prop="belong_dept">
        <el-cascader :options="depOptions" :props="{ checkStrictly: true,emitPath:false }" ref="demoCascader"   style="width:100%" v-model="equipment.belong_dept"></el-cascader>
               
        </el-form-item>
          </el-col>
         <el-col :span="12">
        <el-form-item label="保管人" prop="keeper">
           
               <el-select
              v-model="equipment.keeper"
              filterable
              style="width: 100%"
              allow-create
              default-first-option
              placeholder="请选择">
              <el-option
                 v-for="item in keeperOptions"
                  :key="item.value"
                  :label="item.label"
                 :value="item.value">
               </el-option>
           </el-select>
            
         
        </el-form-item>
 </el-col>
         </el-row>
        <el-form-item label="设备备注" prop="description">
          <el-input
            type="textarea"
            :rows="4"
            v-model="equipment.description"
            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 { getpEquipmentList, createEquipment,updateEquipment,deleteEquipment } from "@/api/equipment";
import { getUserList } from "@/api/user";
import { getOrgList } from "@/api/org";
import checkPermission from "@/utils/permission";


import { genTree } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
const defaultequipment = {
  name: "",
  number: "",
};
export default {
  components: { Pagination },
  data() {
    return {
      equipment: defaultequipment,
      equipmentList: {
        count: 0,
      },
      state_:{
        0:'完好',
        1:'限用',
        2:'在修',
        3:'禁用',
      },
      options: [{
          value: 0,
          label: '完好'
        }, {
          value: 1,
          label: '限用'
        }, {
          value: 2,
          label: '在修'
        }, {
          value: 3,
          label: '禁用'
        }],
      listQuery: {
        page: 1,
        page_size: 20,
      },
      keeperOptions:[],
      depOptions:[],
      listLoading: true,
      dialogVisible: false,
      dialogType: "new",
      rule1: {
        name: [{ required: true, message: "请输入", trigger: "blur" }],
        number: [{ required: true, message: "请输入", trigger: "blur" }],
          type: [{ required: true, message: "请输入", trigger: "blur" }],
        model: [{ required: true, message: "请输入", trigger: "blur" }],
        state: [{ required: true, message: "请选择", trigger: "blur" }],
        belong_dept:[{ required: true, message: "请选择", trigger: "blur" }]
      },
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getList();
     this.getUserList();
     this.getOrgList();
  },
  methods: {
    checkPermission,
    //设备列表
    getList() {
      this.listLoading = true;
      this.listQuery.type=1;
      getpEquipmentList(this.listQuery).then((response) => {
        if (response.data) {
          this.equipmentList = response.data;
        }
        this.listLoading = false;
      });
    },
     //组员列表
    getUserList() {
      getUserList({pageoff:true}).then((res) => {
        this.keeperOptions = genTree(res.data);
      });
    },
  //部门列表
    getOrgList() {
      getOrgList({pageoff:true}).then((res) => {
        this.depOptions = genTree(res.data);
      });
    },

    
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        page_size: 20,
      }
      this.getList();
    },
    handleCreate() {
      this.equipment = Object.assign({}, defaultequipment);
      this.dialogType = "new";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
   
    handleEdit(scope) {
      this.equipment = 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 deleteEquipment(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) {
            updateEquipment(this.equipment.id, this.equipment).then((res) => {
              if (res.code >= 200) {
                this.getList();
                this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
          } else {
            this.equipment.type=1;
            createEquipment(this.equipment).then((res) => {
              if (res.code >= 200) {
                this.getList();
                 this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>
