<!-- 产品管理 -->
<template>
  <div class="product-management">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
      <el-form-item label="名称">
        <el-input v-model="dataForm.productName" placeholder="产品名称" clearable/>
      </el-form-item>
      <el-form-item label="产品类别">
        <el-select
          v-model="dataForm.productType"
          remote
          placeholder="请选择">
          <el-option
            v-for="item in optionsType"
            :key="item.code"
            :label="item.value"
            :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="search()">查询</el-button>
        <el-button v-if="isAuth('pro:product:save')" type="primary" @click="addOrUpdateHandle(0, false)">创建新产品</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        label="序号"
        type="index"
        width="50"
        align="center">
      </el-table-column>
      <el-table-column
        prop="productCode"
        header-align="center"
        align="center"
        min-width="100"
        label="产品编码">
      </el-table-column>
      <el-table-column
        prop="productName"
        header-align="center"
        align="center"
        min-width="120"
        label="产品名称">
      </el-table-column>
      <el-table-column
        prop="productType"
        header-align="center"
        align="center"
        :formatter="typeFormat"
        label="产品类别">
      </el-table-column>
      <el-table-column
        prop="isCompose"
        header-align="center"
        align="center"
        :formatter="composeFormat"
        label="是否组合产品">
      </el-table-column>
      <el-table-column
        prop="sourceName"
        header-align="center"
        align="center"
        min-width="200"
        :show-overflow-tooltip="true"
        label="来源">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        min-width="160"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="creatorName"
        header-align="center"
        align="center"
        min-width="100"
        label="创建人">
      </el-table-column>
      <el-table-column
        prop="notes"
        header-align="center"
        align="center"
        min-width="180"
        :show-overflow-tooltip="true"
        label="备注">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="配料清单">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="materialDetails(scope.row.coId, true)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="对应图纸">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="drawDetails(scope.row.coId, true)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="对应工艺">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="techDetails(scope.row.coId, true)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('pro:product:info')" type="text" size="small" @click="detailHandle(scope.row.productId)">查看</el-button>
          <el-button v-if="isAuth('pro:product:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.productId, false)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <detail v-if="detailVisible" ref="detail"/>
  </div>
</template>

<script>
  import AddOrUpdate from './product-add-or-update'
  import Detail from './product-detail'
  import { getDictList } from '@/api/dict'
  import { getProductList } from '@/api/product'
  export default {
    name: 'product-management',
    components: {
      AddOrUpdate, Detail
    },
    data () {
      return {
        addOrUpdateVisible: false,
        detailVisible: false,
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        optionsType: []
      }
    },
    created () {
      this.getTypeList()
      // this.getStateList()
      this.getDataList()
    },
    methods: {
      // 获取产品类别字典
      getTypeList () {
        getDictList({type: 'product_type'}).then(({data}) => {
          if (data) {
            this.optionsType = data
          }
        })
      },
      // 查询
      search () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.addOrUpdateVisible = false
        let params = {
          'current': this.pageIndex,
          'size': this.pageSize,
          'productName': this.dataForm.productName ? this.dataForm.productName : null,
          'productType': this.dataForm.productType ? this.dataForm.productType : null
        }
        getProductList(params).then(({data}) => {
          if (data && data.code === '200') {
            this.dataList = data.data.records
            this.totalPage = Number(data.data.total)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      deleteHandle (id) {
        if (!id) return
        let ids = []
        ids.push(id)
        this.$confirm(`确定删除?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/biz-service/product/delete`),
            method: 'DELETE',
            data: ids
          }).then(({data}) => {
            if (data && data.code === '200') {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id, disable) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, disable)
        })
      },
      // 转换属性“产品类别”
      typeFormat (row) {
        if (this.optionsType) {
          for (let i = 0; i < this.optionsType.length; i++) {
            if (this.optionsType[i].code === row.productType) {
              return this.optionsType[i].value
            }
          }
        }
      },
      // 转换属性“是否组合产品”
      composeFormat (row) {
        if (row.isCompose === 1) {
          return '是'
        } else return '否'
      },
      // 详情
      detailHandle (id) {
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.detail.init(id)
        })
      }
    }
  }
</script>

<style scoped>

</style>
