<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :exportList="exportList"
    :operation="operation()"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
    :optionsEvensGroup="optionsEvensGroup"
    :columnParsing="columnParsing"
  >
    <!-- 弹窗 -->
    <el-dialog
      title="产品信息"
      :visible.sync="showDialogForm"
      width="70%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="diaLogForm" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="24" :offset="0">
            <el-form-item label="产品名称">
              <el-select placeholder="选择名称" filterable @change="hanleName" v-model="diaLogForm.materialId">
                <el-option v-for="item in materialLis" :label="item.name" :value="item.id" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="产品型号">
              <el-input v-model="diaLogForm.productModel"></el-input>
            </el-form-item>
            <el-form-item label="套机编码">
              <el-input v-model="diaLogForm.productNumber"></el-input>
            </el-form-item>
            <el-form-item label="产品体积">
              <el-input v-model="diaLogForm.productVolume"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24" :offset="0">
            <el-row type="flex" justify="space-between">
              <el-col :span="20" type="flex">
                <el-form-item label="类型">
                  <el-select v-model="diaLogForm.type" @change="hanleType" filterable placeholder="请选择类型">
                    <el-option
                      v-for="item in DistType"
                      :key="item.dictCode"
                      :label="item.dictValue"
                      :value="item.dictCode"
                    >
                    </el-option>
                  </el-select> </el-form-item
              ></el-col>
              <el-col :span="3" style="text-align: right">
                <el-button type="primary" @click="hanleAddData">+</el-button></el-col
              >
            </el-row>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0" v-for="(item, index) in diaLogForm.items" :key="index">
            <div>
              <el-form-item label="编码">
                <el-select v-model="item.number" placeholder="请选择编码" filterable @change="hanleCode($event, index)">
                  <el-option v-for="item in machineList" :key="item.id" :label="item.number" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="名称" prop="">
                <el-input v-model="item.name" placeholder=""></el-input>
              </el-form-item>
              <el-form-item label="型号">
                <el-input v-model="item.model" placeholder=""></el-input>
              </el-form-item>
              <el-form-item label="体积">
                <el-input v-model="item.volume" placeholder=""></el-input>
              </el-form-item></div
          ></el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="hanleInfo">确 定</el-button>
      </div>
    </el-dialog>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import add_callback_mixin from '@/components/template/add_callback_mixin.js'
import Popu from '@/components/template/popu.vue'
import { downloadFiles } from '@/utils/util'
import {
  getDistList,
  getMachineLlist,
  getMaterialList,
  getProductAdd,
  getProductDel,
  getProductDetail,
  getProductEdit,
  getProductListV2,
  exportProductListV2
} from '@/api/basic_data/material'
export default {
  components: { TemplatePage, Popu },
  mixins: [import_mixin, add_callback_mixin],
  data() {
    return {
      visible: false,
      // 事件组合
      // 事件组合
      optionsEvensGroup: [
        [
          [
            {
              name: '新增',
              click: this.addOn(() => {
                this.showDialogForm = true
                this.type = 1

                this.hanleMachine()
              }),
              isRole: this.$checkBtnRole('add', this.$route.meta.roles)
            }
          ]
        ],
        [
          [
            {
              name: '批量删除',
              click: async () => {
                if (this.recordSelected.length === 0) {
                  this.$message.error('请选择需要删除的数据')
                  return
                }
                let ids = this.recordSelected.map(v => {
                  return v.id
                })
                let params = { ids: ids.join() }
                await getProductDel(params)
                this.$refs.pageRef.refreshList()
                this.$message.success('批量删除成功')
              },
              isRole: this.$checkBtnRole('del', this.$route.meta.roles)
            }
          ]
        ]
        // [
        //   [
        //     {
        //       name: '导入',
        //       render: this.importButton(importCustomerPtV2)
        //     }
        //   ]
        // ],
      ],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: true
      }, // 关闭新增弹窗

      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      options: [
        {
          value: '选项1',
          label: '1'
        },
        {
          value: '选项2',
          label: '2'
        },
        {
          value: '选项3',
          label: '3'
        },
        {
          value: '选项4',
          label: '4'
        },
        {
          value: '选项5',
          label: '5'
        }
      ],
      diaLogForm: {
        adminCompanyId: null,
        productNumber: '',
        id: null,
        items: [
          {
            id: '',
            innerOutsideMachineId: '',
            machineConfigureId: '',
            parentId: '',
            model: '',
            name: '',
            number: '',
            type: '',
            volume: ''
          }
        ],
        materialId: null,
        productModel: '',
        productVolume: 0,
        type: ''
      },
      showDialogForm: false,
      dataList: [],
      screenForm: {
        innerName: '',
        innerNumber: '',
        innerType: '',
        name: '',
        number: '',
        outsideName: '',
        outsideNumber: '',
        outsideType: '',
        type: '',
        volume: ''
      },
      type: null,
      machineList: [],
      machinePei: {
        currentPage: 1,
        pageSize: 10,
        name: '',
        number: '',
        type: '',
        volume: ''
      },
      materialLis: {},
      typeList: [],
      DistType: [],
      isCollapse: true,
      machineConfigureId: ''
    }
  },
  created() {
    this.getMaterMacList()
  },
  methods: {
    // 列表请求函数
    getList(...p) {
      this.recordSelected = []
      return getProductListV2(...p)
    },
    // 列表导出函数
    exportList: exportProductListV2,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return (h, { row, index, column }) => {
        return (
          <div class="operation-btns">
            {this.$checkBtnRole('edit', this.$route.meta.roles) ? (
              <el-button
                type="text"
                size="mini"
                class="textColor"
                onClick={() => {
                  this.editFn(row.id, row, index)
                }}
              >
                编辑
              </el-button>
            ) : null}
            {this.$checkBtnRole('del', this.$route.meta.roles) ? (
              <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                
                title="内容确定删除吗？"
                onOnConfirm={() => this.hanleDelete(row.id)}
              >
                <el-button slot="reference"  size="mini" type="text" class="textColor el-popover-left">
                  删除
                </el-button>
              </el-popconfirm>
            ) : null}
          </div>
        )
      }
    },
    handleClose() {
      this.addOff(() => {
        this.showDialogForm = false
        this.resetInfo()
      })()
    },
    hanleAddData() {
      this.diaLogForm.items.push({
        id: '',
        innerOutsideMachineId: '',
        machineConfigureId: '',
        parentId: '',
        model: '',
        name: '',
        number: '',
        type: '',
        volume: ''
      })
    },
    getMaterMacList() {
      const params = {
        pageNum: 1,
        pageSize: -1,
        keyword: ''
      }

      getMaterialList(params).then(res => {
        this.materialLis = res.data.records
      })
      const params2 = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        keyword: ''
      }
      getMachineLlist(params2).then(res => {
        this.typeList = res.data.records
      })
    },

    hanleType(e) {
      console.log(e, this.DistType)
      this.getMachineLlist(e)
    },
    getMachineLlist(e) {
      let params2 = {
        pageNum: 1,
        pageSize: -1,
        model: this.machinePei.model,
        name: this.machinePei.name,
        number: this.machinePei.number,
        type: e,
        volume: this.machinePei.volume
      }
      getMachineLlist(params2).then(res => {
        this.machineList = res.data.records
      })
    },
    hanleCode(e, i) {
      console.log(e, 'code')
      this.machineList.find(l => {
        if (l.id == e) {
          this.$set(this.diaLogForm.items, i, {
            id: '',
            machineConfigureId: l.id,
            parentId: '',
            innerOutsideMachineId: l.id,
            model: l.model,
            name: l.name,
            number: l.number || '',
            type: l.type,
            volume: l.volume
          })
        }
      })
    },
    hanleName(e) {
      this.materialLis.find(k => {
        if (k.id == e) {
          this.diaLogForm.productModel = k.specification
          this.diaLogForm.productNumber = k.number
          this.diaLogForm.volume = k.volume
        }
      })
    },

    editFn(id, row, index) {
      console.log(row, '45465')
      let dateils = null
      this.diaLogForm = {
        ...row,
        items: [
          {
            id: '',
            machineConfigureId: row.id,
            parentId: '',
            innerOutsideMachineId: row.id,
            model: row.model,
            name: row.name,
            number: row.number || '',
            type: row.type,
            volume: row.volume
          }
        ]
      }
      getProductDetail({ id: id }).then(res => {
        dateils = res.data
        console.log(dateils)
        this.diaLogForm.materialId = dateils.materialId
        this.materialLis.length &&
          this.materialLis.find(k => {
            if (k.id == dateils.materialId) {
              console.log(k, '5555')
              this.diaLogForm.productModel = k.specification
              this.diaLogForm.productNumber = k.number
              this.diaLogForm.productVolume = k.volume
            }
          })

        getDistList({ sysDictEnum: 'MACHINE_CONFIGURE' }).then(disType => {
          this.DistType = disType.data
        })
        this.showDialogForm = true
      })
    },
    hanleInfo() {
      if (this.type === 1) {
        this.diaLogForm.id = null
        this.diaLogForm.adminCompanyId = null
        getProductAdd({ ...this.diaLogForm }).then(res => {
          this.$successMsg('保存成功')
          this.resetInfo()
          this.$refs.pageRef.refreshList()
        })
      } else if (this.type === 2) {
        const params = { ...this.diaLogForm }
        getProductEdit(params).then(res => {
          this.$successMsg('编辑成功')
          this.resetInfo()
          this.$refs.pageRef.refreshList()
        })
      }

      this.showDialogForm = false
    },
    hanleDelete(id) {
    
        getProductDel({ ids: id }).then(res => {
          this.$successMsg('删除成功')
          this.$refs.pageRef.refreshList()
        })
     
    },
    handleExport() {
      let screenData = {
        customerName: this.screenForm.customerName,
        freeDay: this.screenForm.freeDay,
        toll: this.screenForm.toll
      }
      downloadFiles('product-compose/export', screenData)
    },
    resetInfo() {
      this.diaLogForm = {
        id: null,
        composeNumber: '',
        id: '',
        items: [
          {
            id: '',
            innerOutsideMachineId: '',
            machineConfigureId: '',
            parentId: '',
            model: '',
            name: '',
            number: '',
            type: '',
            volume: ''
          }
        ],
        materialId: '',
        orgNumber: 0,
        productModel: '',
        productVolume: ''
      }
    },
    hanleMachine() {
      let params = {
        pageNum: 1,
        pageSize: -1,
        keyword: ''
      }
      getMaterialList(params).then(res => {
        this.materialLis = res.data.records
      })
      getDistList({ sysDictEnum: 'MACHINE_CONFIGURE' }).then(res => {
        console.log(res, 'type')
        this.DistType = res.data
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-select {
  width: 100%;
}
</style>
