<template>
  <!-- 基础页面 -->
  <div class="app-container">
    <el-card v-show="showSearch" style="margin-bottom: 20px">
      <el-form :model="queryParams" ref="queryForm" size="small" inline @submit.native.prevent>
        <el-form-item label="工具专业" prop="fourthParentId">
          <el-select
            v-model="queryParams.fourthParentId"
            placeholder="请选择工具专业"
            clearable
            filterable
            @change="(val) => changeType(val, '2')"
            style="width: 240px"
          >
            <el-option v-for="item in fourthParentList" :key="item.id" :label="item.label" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="施工类型" prop="greatGrandparentId">
          <el-select
            v-model="queryParams.greatGrandparentId"
            placeholder="请选择施工类型"
            clearable
            filterable
            :disabled="!queryParams.fourthParentId"
            @change="(val) => changeType(val, '3')"
            style="width: 240px"
          >
            <el-option v-for="item in greatGrandparentList" :key="item.id" :label="item.label" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="工具类型" prop="grandparentTypeId">
          <el-select
            v-model="queryParams.grandparentTypeId"
            placeholder="请选择工具类型"
            clearable
            filterable
            :disabled="!queryParams.greatGrandparentId"
            @change="(val) => changeType(val, '4')"
            style="width: 240px"
          >
            <el-option v-for="item in grandparentTypeList" :key="item.id" :label="item.label" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="工具名称" prop="parentTypeId">
          <el-select
            v-model="queryParams.parentTypeId"
            placeholder="请选择工具名称"
            clearable
            filterable
            :disabled="!queryParams.grandparentTypeId"
            style="width: 240px"
          >
            <el-option v-for="item in parentTypeList" :key="item.id" :label="item.label" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="规格型号" prop="typeName">
          <el-input
            v-model="queryParams.typeName"
            placeholder="请输入规格型号"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="工具编码" prop="toolCode">
          <el-input
            v-model="queryParams.toolCode"
            placeholder="请输入规格型号"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 240px"
          />
        </el-form-item>

        <!-- 表单按钮 -->
        <el-form-item  style="margin-left:1100px">
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <div style="font-size: 20px; font-weight: 800">编码工具台账列表</div>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
      </el-row>

      <el-table
        v-loading="isLoading"
        :data="tableList"
        highlight-current-row
        border
        stripe
        :max-height="650"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          width="55"
          label="序号"
          align="center"
          :index="(index) => (queryParams.pageNum - 1) * queryParams.pageSize + index + 1"
        />
        <el-table-column
          v-for="(column, index) in tableColumns"
          show-overflow-tooltip
          :key="index"
          :label="column.label"
          :prop="column.prop"
          align="center"
        >
          <!-- render插槽 -->
          <template v-slot="{ row }">
            <component :is="column.render ? { render: (h) => column.render(h, { row }) } : 'span'">
              {{ !column.render ? row[column.prop] : '' }}
            </component>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-button size="mini" type="text" icon="el-icon-zoom-in" @click="handleDialog(row, true)">查看</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDialog(row, false)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 弹框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="60%" v-loading="isLoading">
      <el-form
        ref="dialogForm"
        :model="dialogForm"
        label-width="110px"
        size="small"
        inline
        @submit.native.prevent
        :disabled="isView"
      >
        <el-form-item label="工具专业" prop="fourthParentName">
          <el-input v-model="dialogForm.fourthParentName" placeholder="请输入工具专业" readonly style="width: 240px" />
        </el-form-item>
        <el-form-item label="施工类型" prop="greatGrandparentName">
          <el-input
            v-model="dialogForm.greatGrandparentName"
            placeholder="请输入工具专业"
            readonly
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="工具类型" prop="grandparentTypeName">
          <el-input
            v-model="dialogForm.grandparentTypeName"
            placeholder="请输入工具专业"
            readonly
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="工具名称" prop="parentTypeName">
          <el-input v-model="dialogForm.parentTypeName" placeholder="请输入工具专业" readonly style="width: 240px" />
        </el-form-item>
        <el-form-item label="规格型号" prop="typeName">
          <el-input v-model="dialogForm.typeName" placeholder="请输入工具专业" readonly style="width: 240px" />
        </el-form-item>
        <el-form-item label="工具编码" prop="toolCode">
          <el-input v-model="dialogForm.toolCode" placeholder="请输入工具专业" readonly style="width: 240px" />
        </el-form-item>
        <el-form-item label="计量单位" prop="unitName">
          <el-select v-model="dialogForm.unitName" placeholder="请选择计量单位" clearable style="width: 240px">
            <el-option
              v-for="dict in dict.type.dev_unit_type"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            />
          </el-select>
        </el-form-item>
        <!-- 厂家 -->
        <el-form-item label="生产厂家">
          <el-select v-model="dialogForm.supplierId" placeholder="请选择厂家" clearable style="width: 240px">
            <el-option
              v-for="item in manufacturerSelect"
              :key="item.id"
              :label="item.label"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 出厂日期 -->
        <el-form-item label="出厂日期">
          <el-date-picker
            v-model="dialogForm.productionDate"
            type="date"
            clearable
            placeholder="选择出厂日期"
            value-format="yyyy-MM-dd"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="本次检验时间">
          <el-date-picker
            v-model="dialogForm.lastCheckDate"
            type="date"
            clearable
            placeholder="选择本次检验时间"
            value-format="yyyy-MM-dd"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="资产原值">
          <el-input-number
            v-model="dialogForm.originCost"
            :min="0"
            :max="99999999"
            :controls="false"
            :precision="0"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="原始编码">
          <el-input v-model="dialogForm.identifyCode" maxlength="999" style="width: 240px" />
        </el-form-item>
        <el-form-item label="下次检验时间">
          <el-date-picker
            v-model="dialogForm.nextCheckDate"
            type="date"
            clearable
            placeholder="选择下次检验时间"
            value-format="yyyy-MM-dd"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="工具状态">
          <el-input v-model="dialogForm.identifyCode" readonly style="width: 240px" />
        </el-form-item>
        <div v-for="(item, index) in dialogForm.propertyVoList" :key="index">
          <el-form-item label="特征项">
            <el-input v-model="item.propertyName" placeholder="请输入特征项" style="width: 240px" />
          </el-form-item>
          <el-form-item label="特征值">
            <div style="display: flex; align-items: center">
              <el-input v-model="item.propertyValue" placeholder="请输入特征值" style="width: 240px" />
              <i
                v-if="index == dialogForm.propertyVoList.length - 1 && dialogForm.propertyVoList.length < 9 && !isView"
                class="el-icon-circle-plus-outline"
                @click="handleProperty('add', index)"
                style="color: #67c23a; font-size: 24px; margin-left: 8px"
              />
              <i
                v-if="index != 0 && !isView"
                class="el-icon-remove-outline"
                @click="handleProperty('remove', index)"
                style="color: #f56c6c; font-size: 24px; margin-left: 8px"
              />
            </div>
          </el-form-item>
        </div>
        <el-form-item label="附件" prop="fileList">
          <FileUpload
            v-model="dialogForm.fileList"
            :value="dialogForm.fileList"
            :limit="9"
            :fileType="['doc', 'xls', 'pdf', 'jpg', 'jpeg', 'png']"
            :isView="isView"
            @input="handleImage"
          />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button v-if="!isView" type="primary" @click="submit">确 定</el-button>
        <el-button @click="dialogVisible = false">{{ isView ? '关 闭' : '取 消' }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getListCodeApi, getToolSelectApi, updateByIdApi } from '@/api/toolsManage'
import { getManufacturerSelectApi } from '@/api/EquipmentLedger'
import FileUpload from '@/components/FileUpload'

export default {
  name: 'CodeToolsLedger',
  dicts: ['dev_unit_type'],
  components: { FileUpload },
  data() {
    return {
      isLoading: false,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: '0',
        level: '1',
        fourthParentId: null, // 工具专业
        greatGrandparentId: null, // 施工类型
        grandparentTypeId: null, // 工具类型
        parentTypeId: null, // 工具名称
        typeName: null, // 规格型号
        toolCode: null,
      },
      fourthParentList: [],
      greatGrandparentList: [],
      grandparentTypeList: [],
      parentTypeList: [],
      total: 0, // 总条数
      // 表头
      tableColumns: [
        { label: '工具专业', prop: 'fourthParentName' },
        { label: '施工类型', prop: 'greatGrandparentName' },
        { label: '工具类型', prop: 'grandparentTypeName' },
        { label: '工具名称', prop: 'parentTypeName' },
        { label: '规格型号', prop: 'typeName' },
        { label: '计量单位', prop: 'unitName' },
        { label: '工具编码', prop: 'toolCode' },
        { label: '下次检验时间', prop: 'nextCheckDate' },
        { label: '生产厂家', prop: 'supplierName', width: 200 },
        { label: '出厂日期', prop: 'productionDate' },
        { label: '资产原值', prop: 'originCost' },
        { label: '原始编码', prop: 'identifyCode' },
      ],
      // 表格数据
      tableList: [],
      isView: false,
      dialogTitle: '编码设备管理',
      dialogVisible: false,
      dialogForm: {
        fourthParentName: '',
        greatGrandparentName: '',
        grandparentTypeName: '',
        parentTypeName: '',
        typeName: '',
        unitName: '',
        supplierId: '',
        productionDate: '',
        lastCheckDate: '',
        originCost: null,
        identifyCode: '',
        nextCheckDate: '',
        propertyVoList: [],
        fileList: null,
      },
      manufacturerSelect: [],
    }
  },
  created() {
    this.getList()
    this.getSelectList()
  },
  methods: {
    // 查询
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.queryParams.parentId = '0'
      this.queryParams.level = '1'
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    // 获取列表
    async getList() {
      console.log('列表-查询', this.queryParams)
      this.isLoading = true
      try {
        const params = { ...this.queryParams }
        const res = await getListCodeApi(params)
        this.tableList = res.rows || []
        this.total = res.total || 0
      } catch (error) {
        this.tableList = []
        this.total = 0
      } finally {
        this.isLoading = false
      }
    },
    // 下拉
    async getSelectList() {
      try {
        const params = { ...this.queryParams }
        const res = await getToolSelectApi(params)
        if (this.queryParams.level == '1') {
          this.fourthParentList = res.data || []
        } else if (this.queryParams.level == '2') {
          this.greatGrandparentList = res.data || []
        } else if (this.queryParams.level == '3') {
          this.grandparentTypeList = res.data || []
        } else if (this.queryParams.level == '4') {
          this.parentTypeList = res.data || []
        }
      } catch (error) {
        console.log('🚀 ~ error:', error)
      }
    },
    changeType(val, level) {
      console.log('🚀 ~ val, type:', val, level)
      if (level == '2') {
        this.queryParams.parentId = val
        this.queryParams.greatGrandparentId = null
        this.queryParams.grandparentTypeId = null
        this.queryParams.parentTypeId = null
        this.greatGrandparentList = []
        this.grandparentTypeList = []
        this.parentTypeList = []
      } else if (level == '3') {
        this.queryParams.parentId = this.queryParams.greatGrandparentId
        this.queryParams.grandparentTypeId = null
        this.queryParams.parentTypeId = null
        this.grandparentTypeList = []
        this.parentTypeList = []
      } else if (level == '4') {
        this.queryParams.parentId = this.queryParams.grandparentTypeId
        this.queryParams.parentTypeId = null
        this.parentTypeList = []
      }
      this.queryParams.level = level
      if (!val) return
      this.getSelectList()
    },
    // 获取厂家
    async getManufacturerSelect() {
      try {
        const res = await getManufacturerSelectApi()
        this.manufacturerSelect = res.data
      } catch (error) {}
    },
    handleProperty(type, index) {
      if (type == 'add') {
        this.dialogForm.propertyVoList.push({
          propertyName: '',
          propertyValue: '',
        })
      } else {
        this.dialogForm.propertyVoList.splice(index, 1)
      }
    },
    handleDialog(row, type) {
      this.dialogTitle = type ? '编码设备管理' : '编码设备详情'
      this.isView = type
      this.dialogVisible = true
      this.getManufacturerSelect()

      this.$nextTick(() => {
        this.$refs.dialogForm.resetFields()
        this.dialogForm = { ...this.dialogForm, ...row }
        if (!this.dialogForm.propertyVoList || this.dialogForm.propertyVoList.length == 0) {
          this.dialogForm.propertyVoList = [
            {
              propertyName: '',
              propertyValue: '',
            },
          ]
        }
        console.log('🚀 ~ this.dialogForm:', this.dialogForm)
      })
    },
    handleImage(file) {
      console.log('🚀 ~ file:', file)
      console.log('🚀 ~ file.url:', this.dialogForm.fileList)
    },
    async submit() {
      console.log('🚀 ~ methods.submit:', this.dialogForm)
      this.isLoading = true
      try {
        await updateByIdApi(this.dialogForm)
        this.$message({
          type: 'success',
          message: '操作成功!',
        })
        this.dialogVisible = false
        this.getList()
      } catch (error) {
        console.log('🚀 ~ error:', error)
      } finally {
        this.isLoading = false
      }
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-input-number.is-without-controls .el-input__inner {
  text-align: left;
}
</style>
