<template>
  <div class="app-container">
    <el-card>
      <el-input
            v-model="listQuery.search" 
            placeholder="输入公司名称、设备名称、规格型号、生产厂家、检测参数、采购时间搜索" 
            style="width: 600px;" 
            class="filter-item"
        />
        <el-button 
        class="filter-item" 
        type="primary" 
        icon="el-icon-search" 
        @click="handleSearch">搜索</el-button>
        <el-button type="primary" 
        @click="importExcel" 
        icon="el-icon-upload" 
        :disabled="checkPermission['device_import']"
          >导入Excel</el-button>
    </el-card>
    <el-card style="margin-top: 10px">
      <el-table
        v-loading="listLoading"
        :data="contentList.results"
        border
        fit
        stripe
        highlight-current-row
        max-height="700"
      >
        <el-table-column type="index" width="50" />
        <el-table-column align="left" label="公司名称">
          <template slot-scope="scope">{{ scope.row.company_name }}</template>
        </el-table-column>
        <el-table-column align="left" label="设备名称">
          <template slot-scope="scope">{{ scope.row.device_name }}</template>
        </el-table-column>
        <el-table-column align="left" label="设备编号">
          <template slot-scope="scope">{{ scope.row.device_number }}</template>
        </el-table-column>
        <el-table-column align="left" label="规格型号">
          <template slot-scope="scope">{{ scope.row.spec }}</template>
        </el-table-column>
        <el-table-column align="left" label="生产厂家">
          <template slot-scope="scope">{{ scope.row.manufactor }}</template>
        </el-table-column>
        <el-table-column align="left" label="检测参数">
          <template slot-scope="scope">{{ scope.row.dec_parameter }}</template>
        </el-table-column>
        <el-table-column align="left" label="量程">
          <template slot-scope="scope">{{ scope.row.range }}</template>
        </el-table-column>
        <el-table-column align="left" label="精度">
          <template slot-scope="scope">{{ scope.row.precision }}</template>
        </el-table-column>
        <el-table-column align="left" label="其他技术参数">
          <template slot-scope="scope">{{ scope.row.other_parameter }}</template>
        </el-table-column>
        <el-table-column align="left" label="使用部门">
          <template slot-scope="scope">{{ scope.row.department }}</template>
        </el-table-column>
        <el-table-column align="left" label="采购时间">
          <template slot-scope="scope">{{ scope.row.procurement_time }}</template>
        </el-table-column>
        <el-table-column align="left" label="资产原值">
          <template slot-scope="scope">{{ scope.row.original_price }}</template>
        </el-table-column>
        <el-table-column align="left" label="资产净值">
          <template slot-scope="scope">{{ scope.row.current_price }}</template>
        </el-table-column>
        <el-table-column align="left" label="原值是否含基建">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_infrastructure" type="success">是</el-tag>
            <el-tag v-else type="danger">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="left" label="基建原值占比(%)">
          <template slot-scope="scope">{{ scope.row.infras_percentage }}</template>
        </el-table-column>
        <el-table-column align="left" label="是否有说明书">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_instructions" type="success">是</el-tag>
            <el-tag v-else type="danger">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="left" label="保管人">
          <template slot-scope="scope">{{ scope.row.custodian }}</template>
        </el-table-column>
        <el-table-column align="left" label="存放地点">
          <template slot-scope="scope">{{ scope.row.depositor }}</template>
        </el-table-column>
        <el-table-column align="left" label="设备联系人">
          <template slot-scope="scope">{{ scope.row.contacts }}</template>
        </el-table-column>
        <el-table-column align="left" label="联系电话">
          <template slot-scope="scope">{{ scope.row.tel }}</template>
        </el-table-column>
        <el-table-column align="left" label="备注">
          <template slot-scope="scope">{{ scope.row.remark }}</template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="contentList.count > 0"
        :total="contentList.count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
    >
      <el-form
        ref="Form"
        :model="Content"
        label-width="80px"
        label-position="right"
        :rules="rule1">

        <el-form-item label="文档上传" prop="file" v-if="dialogVisible">
          <el-upload
          ref="upload"
          :action="upUrl"
          :on-preview="handlePreview"
          :on-success="handleUpSuccess"
          :on-remove="handleRemove"
          :headers="upHeaders"
          :file-list="fileList"
          :limit="1"
          accept=".doc,.docx,.xls,.xlsx,.ppt,.pptx,.pdf,.zip"
        >
          <el-button size="small" type="primary">上传文件</el-button>
        </el-upload>
        </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()">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getDeviceList,
  DeviceImp,
} from "@/api/device";
import checkPermission from "@/utils/permission";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { upUrl, upHeaders } from "@/api/file";
const defaultContent = {
  excel_path:''
};
export default {
  components: { Pagination, Treeselect },
  data() {
    return {
      upHeaders: upHeaders(),
      upUrl: upUrl(),
      fileList:[],
      Content: defaultContent,
      listLoading:false,
      dialogVisible: false,
      listQuery: {
        page: 1,
        search:'',
        page_size: 20,
      },
       contentList: {
        count:0
      },
      dialogType: "new",
      rule1: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
    };
  },
  computed: {},
  watch: {
    filterOrgText(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
  
  },
  methods: {
    handleSearch(){
      this.listQuery.page = 1
      this.getList(); 
    },
    handlePreview(file) {
      if ("url" in file) {
        window.open(file.url);
      } else {
        window.open(file.response.data.path);
      }
    },
    handleUpSuccess(res, file, filelist) {
        let that =this;
            if (res.code == 201){
              that.Content.excel_path = res.data.path; 
        }else{
            that.$message.error(res.msg);
            that.Content.excel_path = null;
        }
    },
    importExcel(){
        this.dialogVisible = true;
      },
    handleRemove(file, filelist){
      this.Content.file = null;
    },
    checkPermission,
    getList() {
      getDeviceList(this.listQuery).then((response) => {
        if (response.data) {
          this.contentList = response.data;
        }
      });
    },
    async confirm(){
      let that = this;
      this.dialogVisible = false;
      DeviceImp(that.Content).then(res => {
                if (res.code >= 200) {
                    that.getList(listQuery)
                    that.$message({
                      type: "success",
                      message: "上传成功"
                    })
                    this.dialogVisible = false;
                }
              }
            )
          },
  },
};
</script>
