<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="equipmentrecordList.results"
        border
        fit
        stripe
        highlight-current-row
        max-height="600"
      >
       
        <el-table-column type="index" width="50" />
        <el-table-column label="设备名称">
          <template slot-scope="scope">{{ scope.row.equipment_.name }}</template>
        </el-table-column>
         <el-table-column label="设备编号">
          <template slot-scope="scope">{{ scope.row.equipment_.number }}</template>
        </el-table-column>
         <el-table-column label="型号规格">
          <template slot-scope="scope">{{ scope.row.equipment_.model }}</template>
        </el-table-column>
         <el-table-column label="设备状态">
          <template slot-scope="scope">{{ state_[scope.row.equipment_.statedm] }}</template>
        </el-table-column>
         <el-table-column label="最近一次校准/检定日期">
          <template slot-scope="scope">{{ scope.row.recentlydate }}</template>
        </el-table-column>
         <el-table-column label="下次应校准或检定日期">
          <template slot-scope="scope">{{ scope.row.nextdate }}</template>
        </el-table-column>
         <el-table-column label="备注">
          <template slot-scope="scope">{{ scope.row.description }}</template>
        </el-table-column>

        <el-table-column
          align="center"
          label="操作"
          width="120px"
        >
          <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="equipmentrecordList.count > 0"
        :total="equipmentrecordList.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="equipmentrecord"
        label-width="120px"
        label-position="right"
        :rules="rule1"
      >
    
        <el-form-item label="校准或检定设备" prop="equipment">
          <el-select  style="width: 100%" v-model="equipmentrecord.equipment" placeholder="请选择">
             <el-option
                v-for="item in equipmentoptions"
                :key="item.value"
                :label="item.label"
               :value="item.value">
              </el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="校准或检定状态" prop="state">
         
           <el-select  style="width: 100%" v-model="equipmentrecord.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-row>
        <el-col :span="12">
     <el-form-item label="最近一次校准或检定日期" prop="recentlydate">
         <el-date-picker
            v-model="equipmentrecord.recentlydate"
            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="nextdate">
         <el-date-picker
            v-model="equipmentrecord.nextdate"
            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="description">
          <el-input
            type="textarea"
            :rows="4"
            v-model="equipmentrecord.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,
getEquipmentrecordList, createEquipmentrecord,updateEquipmentrecord,deleteEquipmentrecord } 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 = {
};
export default {
  components: { Pagination },
  data() {
    return {
      equipmentrecord: defaultequipment,
      equipmentrecordList: {
        count: 0,
      },
      
      listQuery: {
        page: 1,
        page_size: 20,
      },
      equipmentoptions:[],
       state_:{
          0:'合格',
          1:'准用',
          2:'限用',
          3:'禁用',
          4:'停用',
          5:'封存',
        },
       options: [{
          value: 0,
          label: '合格'
        }, {
          value: 1,
          label: '准用'
        }, {
          value: 2,
          label: '限用'
        },
        {
          value: 3,
          label: '禁用'
        }, {
          value: 4,
          label: '停用'
        }, {
          value: 5,
          label: '封存'
        }],
      listLoading: true,
      dialogVisible: false,
      dialogType: "new",
      rule1: {
        equipment: [{ required: true, message: "请输入", trigger: "blur" }],
        state: [{ required: true, message: "请输入", trigger: "blur" }],
       
      },
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getequipmentList();
      this.getList();
  },
  methods: {
    checkPermission,
    //设备
    getequipmentList() {
      getpEquipmentList({pageoff:true,type:2}).then((res) => {
        this.equipmentoptions = genTree(res.data);
      });
    },
      getList() {
      this.listLoading = true;
      getEquipmentrecordList(this.listQuery).then((response) => {
        if (response.data) {
          this.equipmentrecordList = response.data;
        }
        this.listLoading = false;
      });
    },

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