<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" @click="handleCreateAdd">
        增加部件档案
      </el-button>

      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="getPartInfo" />
        </el-tooltip>
      </div>
    </div>
    <el-table :data="tableData" v-loading="listLoading" row-key="id" border>
      <el-table-column prop="idProduct" label="产品档案id">
      </el-table-column>
      <el-table-column prop="partName" label="部件名称">
      </el-table-column>
      <el-table-column prop="partBlood" label="出血">
      </el-table-column>
      <el-table-column
        prop="paperTypeName"
        label="默认面数">
        <template slot-scope="{row}">
          <span v-if="row.partFace == 1">单面</span>
          <span v-else-if="row.partFace == 2">双面</span>
        </template>
      </el-table-column>
      <el-table-column prop="productUnit" label="部件参数列表" width="150px">
        <template slot-scope="{row}">
          <el-button @click="partParameterList(row.idProduct,row.id,productName,row.partName)" type="success" size="mini">
            部件参数列表
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="productUnit" label="部件参数列表" width="150px">
        <template slot-scope="{row}">
          <el-button @click="partWorkProcessList(row.idProduct,row.id,productName,row.partName)" type="success" size="mini">
            部件工序列表
          </el-button>
        </template>
      </el-table-column>
<!--      <el-table-column label="状态" width="100">-->
<!--        <template slot-scope="{row}">-->
<!--          <el-tag v-if="row.voidFlag==0" type="success">-->
<!--            启用-->
<!--          </el-tag>-->
<!--          <el-tag v-else="row.voidFlag==1" type="danger">-->
<!--            禁用-->
<!--          </el-tag>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" width="230" >
        <template slot-scope="{row}">
          <el-button @click="handleUpdate(row)" type="primary" size="mini">
            编辑
          </el-button>
          <el-button @click="deleteInfo(row)" type="danger" size="mini">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
v-show="listData.total>0"
:total="listData.total"
:page.sync="listData.page"
:limit.sync="listData.pageSize"
      @pagination="getPartInfo" />

    <el-dialog :close-on-click-modal="false" :title="dialogTitle" :visible.sync="dialogVisible" width="800px">

      <!--内部弹窗-->
      <el-dialog
        title="选择纸张"
        :visible.sync="paperDialogVisible"
        :close-on-click-modal="false"
        append-to-body>
        <div class="filter-container">
          <el-input v-model="paperSearch.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item" />
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchPaper">
            搜索
          </el-button>
        </div>
        <el-table
          :data="paperList"
          style="width: 100%;margin-bottom: 20px;"
          row-key="id"
          border
          default-expand-all>
          <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
            <template slot-scope="{row}">
              <el-button @click="chooseThis(row)" type="primary" size="mini">
                选择
              </el-button>
            </template>
          </el-table-column>
          <el-table-column
            prop="paperTypeCode"
            label="分类编码">
          </el-table-column>
          <el-table-column
            prop="paperTypeName"
            label="分类名称">
          </el-table-column>

        </el-table>
        <pagination v-show="typeListData.total>0" :total="typeListData.total" :page.sync="typeListData.page" :limit.sync="typeListData.pageSize" @pagination="getPaperList" />

      </el-dialog>
      <el-form label-width="100px" label-position="right">
      <el-row :gutter="20">
        <el-col :span="16">
          <el-form-item label="部件名称">
            <el-input v-model="form.partName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
        <el-row :gutter="20">
          <el-col :span="13">
            <el-form-item label="销售单位">
              <el-input v-model="form.saleUnit" style="width: 270px"></el-input> =
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-input type="number" v-model="form.transformCoefficient"></el-input>
          </el-col>
          <el-col :span="8">
          <el-form-item label="印刷单位">
            <el-input v-model="form.printUnit"></el-input>
          </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="默认面数">
              <el-select v-model="form.partFace" placeholder="请选择">
                <el-option
                  v-for="type in options"
                  :key="type.value"
                  :label="type.label"
                  :value="type.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="默认出血">
              <el-input v-model="form.partBlood"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="默认纸张">
              <el-button @click="choosePaper">{{form.paperTypeName}}</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="关键描述">
              <el-input v-model="form.partDesc"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" >
            <el-form-item label="主体部件">
              <el-switch v-model="delivery">主体部件</el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="16" :offset="3" >
            1.克重 2.覆膜 3.纸张类型 4.尺寸 5.面数 6.特级 7.数量 8.模数
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus==='1'?addBtn():editBtn()">确 定</el-button>
      </div>
    </el-dialog>
    <!--    <servicedialog @tableRefresh="tableRefresh" ref="menuDialog" :dialogStatus="dialogStatus" :menuId="menuId" :titleDialog="titleDialog" :dialogVisible="dialogVisible"/>-->
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
import {
  getPartInfoList,
  addPartInfo,
  updatePartInfo,
  getPartInfoById
} from '@/api/productFile/partInfo.js'
import {
  getPaperTypeList
} from '@/api/baseInfo/paper/paperType.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'

const form = {
  mainFlag: 1,
  idProduct: '',
  partName: '', // 部件名称
  partBlood: '', // 出血
  saleUnit: '',
  printUnit: '',
  transformCoefficient: '',
  partFace: '',
  idPaperType: '',
  partDesc: '',
  paperTypeName: '请选择'
}

export default {
  components: {
    Cancledialog
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      delivery: true,
      productName: '',
      idProduct: '',
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      search: {
        keyWord: ''
      },
      form: Object.assign({}, form),
      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },

      options: [
        {
          value: 1,
          label: '单面'
        },
        {
          value: 2,
          label: '双面'
        }
      ],

      confirmdialog: false,
      listLoading: false,
      tableData: [],
      menuId: '',

      // 内部弹窗
      paperDialogVisible: false,
      paperSearch: {
        keyWord: ''
      },
      paperList: [],
      typeListData: {
        total: 0,
        page: 1,
        pageSize: 10
      }

    }
  },
  mounted() {
    this.idProduct = this.$route.query.idProduct
    this.productName = this.$route.query.productName
    this.form.idProduct = this.$route.query.idProduct
    this.getPartInfo()
    this.tempRoute = Object.assign({}, this.$route)
    this.setTagsViewTitle()
  },
  methods: {
    // 设置标签名称
    setTagsViewTitle() {
      const title = '部件档案'

      // console.log("tempRoute2",this.tempRoute);
      const route = Object.assign({}, this.tempRoute, { title: `${this.productName}-${title}` })
      // console.log("route",route)
      this.$store.dispatch('tagsView/updateVisitedView', route)
    },

    // 获取列表
    getPartInfo() {
      const _that = this
      _that.listLoading = true

      var params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var data = {
        'idProduct': _that.idProduct
      }

      getPartInfoList(data).then(response => {
        console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        // console.log(_that.tableData)
        _that.listData.total = response.total
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },

    choosePaper() {
      var _that = this
      _that.paperDialogVisible = true
      _that.getPaperList()
    },
    // 获取纸张
    getPaperList() {
      var _that = this
      var params = _that.paperSearch
      var page = {
        pageIndex: _that.typeListData.page,
        pageSize: _that.typeListData.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }
      getPaperTypeList(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {
          _that.paperList = res.data
          _that.typeListData.total = res.total
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },

    searchPaper() {
      this.typeListData.page = 1
      this.getPaperList()
    },

    // 选择纸张
    chooseThis(row) {
      var _that = this
      _that.form.idPaperType = row.id
      _that.form.paperTypeName = row.paperTypeName
      _that.paperDialogVisible = false
    },

    // 增加部件档案
    handleCreateAdd() {
      const _that = this
      _that.form = Object.assign({}, form)
      _that.form.idProduct = _that.idProduct
      _that.dialogTitle = '增加部件档案'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
    },
    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getPartInfo()
    },
    // 增加部件档案
    addBtn() {
      const _that = this
      if (_that.delivery == true) {
        _that.form.mainFlag = 1
      } else if (_that.delivery == false) {
        _that.form.mainFlag = 0
      }
      console.log(_that.form)
      var printerTypeInfo = JSON.stringify(_that.form)
      var data = {
        'partInfo': printerTypeInfo
      }
      // console.log(data);
      // return

      addPartInfo(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '增加成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },

    // 唤起修改
    handleUpdate(row) {
      const _that = this
      _that.form = Object.assign({}, form),
      _that.form.idProduct = _that.idProduct
      _that.dialogTitle = '修改部件档案信息'
      _that.dialogVisible = true
      _that.dialogStatus = '2'

      var idPrint = row.idProduct
      var data = {
        idProduct: idPrint,
        idPartInfo: row.id
      }

      console.log(data)
      getPartInfoById(data).then(res => {
        console.log(res)
        const data = res.data

        _that.form = {
          id: data.id,
          idProduct: data.idProduct,
          partName: data.partName, // 部件名称
          partBlood: data.partBlood, // 出血
          saleUnit: data.saleUnit,
          printUnit: data.printUnit,
          transformCoefficient: data.transformCoefficient,
          partFace: data.partFace,
          idPaperType: data.idPaperType,
          paperTypeName: data.paperTypeName,
          partDesc: data.partDesc,
          mainFlag: data.mainFlag
        }
        if (_that.form.mainFlag == 1) {
          _that.delivery = true
        } else if (_that.form.mainFlag == 0) {
          _that.delivery = false
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 修改部件档案
    editBtn() {
      const _that = this

      if (_that.delivery == true) {
        _that.form.mainFlag = 1
      } else if (_that.delivery == false) {
        _that.form.mainFlag = 0
      }
      var printerTypeInfo = JSON.stringify(_that.form)
      var data = {
        'partInfo': printerTypeInfo
      }

      updatePartInfo(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '修改成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },
    // 查看部件参数列表
    partParameterList(idProduct, idPartInfo, productName, partName) {
      this.$router.push({
        path: '/productFile/partParameterInfo/' + idPartInfo,
        query: {
          'idProduct': idProduct,
          'idPartInfo': idPartInfo,
          'productName': productName,
          'partName': partName
        }
      })
    },

    // 查看部件工序列表
    partWorkProcessList(idProduct, idPartInfo, productName, partName) {
      this.$router.push({
        path: '/productFile/partWorkProcessInfo/' + idPartInfo,
        query: {
          'idProduct': idProduct,
          'idPartInfo': idPartInfo,
          'productName': productName,
          'partName': partName
        }
      })
    },

    // 启用禁用
    deleteInfo(row) {
      var _that = this
      var partInfo = {
        id: row.id,
        idProduct: row.idProduct
      }
      console.log(partInfo)
      var params = {
        'partInfo': JSON.stringify(partInfo)
      }

      var url = '/product/deletePartInfo'
      _that.$refs.confirmDialog.updataClick(params, url)
    },

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getPartInfo()
    }
  }
}
</script>

<style lang="scss" scoped>
  .filter-container {
    margin-bottom: 15px;
  }
</style>
