<template>
  <div class="app-container">
    <div class="filter-container">

      <el-input size="mini" @input="searchData()" v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item" />
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <el-checkbox v-if="isChoose" v-model="searchAllFalg">查询全部</el-checkbox>
      <el-button
        v-if="isChoose == false"
        size="mini"
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加纸张报价档案
      </el-button>
      <el-checkbox v-if="!isChoose" size="mini" @change="checkDisable" v-model="disableLock" label="已禁用" border></el-checkbox>
    </div>
    <div class="table-con">
      <el-table
        :data="tableData"
        v-loading="listLoading"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
      >
        <el-table-column
          label="#"
          align="center"
          type="index"
          :index="indexMethod">
        </el-table-column>
        <el-table-column label="操作" align="left" width="160" class-name="small-padding fixed-width">
          <template slot-scope="{row}">

            <template v-if="!isChoose">
            <el-button @click="handleUpdate(row)" type="primary" size="mini">
              编辑
            </el-button>
            <el-button @click="editStatus(row.id,'1')" type="danger" v-if="row.voidFlag==0" size="mini">
              禁用
            </el-button>
            <el-button @click="editStatus(row.id,'0')" v-else="row.voidFlag==1" size="mini">
              启用
            </el-button>
            </template>
            <template v-else>
              <el-button @click="choosePaper(row)" type="primary" size="mini">
                选择
              </el-button>
            </template>
          </template>
        </el-table-column>

        <el-table-column
          v-if="isChoose == false"
          prop="paperBaseName"
          label="基础纸张名称">
        </el-table-column>
        <el-table-column
          width="120"
          prop="paperBrandName"
          label="纸张品牌名称">
        </el-table-column>

        <el-table-column
          prop="paperQuoteName"
          label="纸张报价名称">
        </el-table-column>
        <el-table-column
          prop="priceType"
          width="90"
          label="计价方式">
          <template slot-scope="{row}">
            <span v-if="row.priceType==0">吨价</span>
            <span v-if="row.priceType==1">平方单价</span>
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          prop="paperPrice"
          label=" 纸张单价(元)">
        </el-table-column>
        <el-table-column
          label="创建时间"
          v-if="isChoose == false"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.createTime">{{scope.row.createTime | getTime}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" class-name="status-col" width="100">
          <template slot-scope="{row}">
            <el-tag v-if="row.voidFlag==0" type="success">
              启用
            </el-tag>
            <el-tag v-else-if="row.voidFlag==1" type="danger">
              禁用
            </el-tag>
          </template>
        </el-table-column>

      </el-table>
      <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getTableInfo" />
    </div>
    <cancledialog @tableRefresh="getTableInfo" ref="confirmDialog"></cancledialog>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="450px">
      <el-form label-width="130px" label-position="right">
        <el-form-item label="选择基础纸张">
          <el-select :disabled="modify" v-model="form.idPaperBaseInfo" filterable placeholder="选择基础纸张" style="width: 160px;margin-top: -20px">
            <el-option
              v-for="item in typeOptions"
              :key="item.id"
              :label="item.paperName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="纸张报价名称">
          <el-input v-model="form.paperQuoteName"></el-input>
        </el-form-item>
        <el-form-item label="选择纸张品牌">
          <el-button :disabled="modify" @click="checkPaperBrandName">{{paperBrandName}}</el-button>
        </el-form-item>
        <el-form-item label="计价方式">
          <el-select v-model="form.priceType" filterable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.title"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="纸张单价">
          <el-input v-model="form.paperPrice"></el-input>
        </el-form-item>
      </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>
    <el-dialog title="选择纸张品牌" width="900px" :visible.sync="outerVisible">
      <div class="address-search">
        <el-input style="width: 120px" size="mini" placeholder="关键字" v-model="search2.keyWord" class="input-with-select"/>
        <el-button size="mini" @click="handleSearch" slot="append" type="primary" icon="el-icon-search">搜索</el-button>

      </div>
      <div style="clear: both;margin-bottom: 20px"></div>
      <el-table
        border
        size="mini"
        ref="multipleTable"
        :data="tableBrandData">
        <el-table-column width="80" center label="操作">
          <template slot-scope="scope">
            <template>
              <el-button @click="chooseThis(scope.row)" type="primary" size="mini">
                选择
              </el-button>
            </template>
          </template>
        </el-table-column>
        <el-table-column
          prop="brandName"
          label="品牌名称">
        </el-table-column>
        <el-table-column
          prop="companyName"
          label="生产公司名称">
        </el-table-column>
        <el-table-column
          label="创建时间"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.createTime">{{scope.row.createTime | getTime}}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="listData2.total>0"
        :total="listData2.total"
        :page.sync="listData2.page"
        :limit.sync="listData2.pageSize"
        @pagination="getBrandInfo"/>
      <div slot="footer" class="dialog-footer">
        <el-button @click="outerVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPaperBrandList,
  getPaperQuoteInfoList,
  addPaperQuoteInfo,
  getPaperBaseInfoListNoPage,
  updatePaperQuoteInfo
} from '@/api/baseInfo/paper/paperInfo.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import Pagination from '@/components/Pagination'
import { updateLogisticsInfo } from '@/api/baseInfo/logisticsInfo'
export default {
  name: 'PaperQuote',
  components: {
    Cancledialog, Pagination
  },
  props: {
    isChoose: {
      type: Boolean,
      default: false
    },
    partId: {
      type: Number || String
    },
    productId: {
      type: Number || String
    },
    parameterId: {
      type: Number || String
    }
  },
  data() {
    return {
      disableLock: false,
      modify: false,
      row: {},
      searchVal: '',
      outerVisible: false,
      options: [
        { id: 0, title: '吨价'},
        { id: 1, title: '平方单价'}
      ],
      paperBrandName: '选择纸张品牌',
      tableBrandData: [],
      typeOptions: [],
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      listLoading: false,
      tableData: [],
      search: {
        keyWord: '',
        voidFlag: "",
        partInfo: {
          id: '',
          idProduct: '',
          idParameter:'',
        }
      },
      searchAllFalg:false,
      search2: {
        keyWord: ''
      },
      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      listData2: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      form: {
        id: "",
        paperQuoteName:"",
        paperBrandName: "", // 纸张品牌,
        paperPrice: "",//纸张单价,
        priceType:"",
        idPaperBaseInfo:""

      }
    }
  },
  mounted() {
    this.getTableInfo()
    this.getDataInfo()
    this.getBrandInfo()
  },
  methods: {
    checkDisable(val) {
      // console.log(val);
      this.listData.page = 1;
      this.getTableInfo()
    },
    // 修改
    editBtn() {
      const _that = this
      var quoteInfo = {
        id: _that.form.id,
        paperQuoteName:_that.form.paperQuoteName,
        priceType: _that.form.priceType, // 计价方式。0吨价，1平方单价。默认吨价,
        paperPrice: _that.form.paperPrice// 纸张单价,
      }
      var data = {
        'quoteInfo': quoteInfo
      }
      // console.log(data);

      updatePaperQuoteInfo(data).then(res => {
        console.log(res)
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '修改成功',
            type: 'success'
          })
          _that.getTableInfo()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },
    // 唤起修改
    handleUpdate(row) {
      const _that = this
      _that.modify = true
      console.log(row)
      _that.form = {
        id: row.id,
        paperQuoteName:row.paperQuoteName,
        paperBrandName: row.paperBrandName, // 纸张品牌,
        paperPrice: row.paperPrice,// 纸张单价,
        priceType:row.priceType,
        idPaperBaseInfo:row.paperBaseName
      }
      // console.log(row.priceType);
      // _that.paperBrandName = row.paperBrandName
      // _that.form.priceType = row.priceType
      // _that.form.idPaperBaseInfo = row.paperBaseName

      _that.dialogTitle = '修改纸张报价档案信息'
      _that.dialogVisible = true
      _that.dialogStatus = '2'
    },
    chooseThis(row) {
      this.row = row
      this.outerVisible = false
      this.paperBrandName = row.brandName
    },
    checkPaperBrandName() {
      // 选择纸张品牌档案
      var _that = this
      _that.outerVisible = true
    },
    // 启用禁用
    editStatus(id, status) {
      var _that = this
      var quoteInfo = {
        id: id,
        voidFlag: status
      }
      var params = {
        'quoteInfo': JSON.stringify(quoteInfo)
      }

      var url = '/doc/voidPaperQuoteInfo'
      _that.$refs.confirmDialog.updataClick(params, url)
    },
    addBtn() {
      const _that = this
      console.log(_that.row)
      console.log(_that.typeOptions)
      if (!_that.form.idPaperBaseInfo) {
        _that.$message('基础纸张不能为空')
        return
      }
      if (!_that.row.brandName) {
        _that.$message('纸张品牌不能为空')
        return
      }
      if (!_that.form.paperPrice) {
        _that.$message('纸张单价不能为空')
        return
      }
      var params = {
        idPaperBaseInfo: _that.form.idPaperBaseInfo, // 基础纸张档案id,
        paperBaseName: _that.typeOptions.find(x => x.id === _that.form.idPaperBaseInfo).paperName, // 基础纸张档案,
        idPaperBrand: _that.row.id, // 纸张品牌档案id,
        paperBrandName: _that.row.brandName, // 纸张品牌,
        priceType: _that.form.priceType, // 计价方式。0吨价，1平方单价。默认吨价,
        paperPrice: _that.form.paperPrice// 纸张单价,
      }

      console.log(params)

      var info = JSON.stringify(params)
      var data = {
        'quoteInfo': info
      }
      addPaperQuoteInfo(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '增加成功',
            type: 'success'
          })
          _that.getTableInfo()
          _that.dialogVisible = false
        } else {
          _that.$message.error(res.msg)
          _that.dialogVisible = false
        }
      }).catch(err => {
        // _that.$message.error(err)
        _that.dialogVisible = false
      })
    },
    // 增加
    handleCreateAdd() {
      const _that = this
      _that.modify = false
      _that.form = {
        idPaperBaseInfo: '', // 基础纸张档案id,
        paperBaseName: '', // 基础纸张档案,
        idPaperBrand: '', // 纸张品牌档案id,
        paperBrandName: '', // 纸张品牌,
        paperQuoteName: '', // 纸张报价名称,
        priceType: '', // 计价方式。0吨价，1平方单价。默认吨价,
        paperPrice: ''// 纸张单价,
      }
      _that.paperBrandName = '选择纸张品牌'
      _that.form.priceType = 0
      _that.dialogTitle = '增加纸张报价档案'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
    },
    // 获取表格数据
    getTableInfo() {
      const _that = this
      _that.listLoading = true
      if(!_that.searchAllFalg){
        _that.search.partInfo.id = _that.partId
        _that.search.partInfo.idProduct = _that.productId
        _that.search.partInfo.idParameter = _that.parameterId
      }else {
        _that.search.partInfo.id = null
        _that.search.partInfo.idProduct = null
        _that.search.partInfo.idParameter = null
      }
      if(_that.isChoose){
        _that.search.voidFlag = 0;
      }else {
        _that.search.voidFlag = -1;
      }
      if(_that.disableLock){
        _that.search.voidFlag = 1;
      }else {
        _that.search.voidFlag = 0;
      }
      var params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }
      // console.log(data);
      getPaperQuoteInfoList(data).then(response => {
        // console.log("tableData",response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        _that.listData.total = response.total
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.listLoading = false
      })
    },
    // 搜索
    searchData() {
      this.listData.page = 1
      this.getTableInfo()
    },
    handleSearch() {
      this.listData.page = 1
      this.getBrandInfo()
    },
    // 序号
    indexMethod(index) {
      return index + 1
    },
    getDataInfo() {
      const _that = this
      var params = {}
      var data = {
        params: JSON.stringify(params)
      }
      getPaperBaseInfoListNoPage(data).then(response => {
        // _that.BreedInfoList = data;
        // console.log("_that.list",response.data);

        // console.log('typeOptions', response.data)
        _that.typeOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    getBrandInfo() {
      const _that = this
      _that.listLoading = true
      var params = _that.search2
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }
      // console.log(data);
      getPaperBrandList(data).then(response => {
        // console.log("tableData",response)
        const data = response.data
        // //  组合表单数据
        _that.tableBrandData = data
        _that.listData2.total = response.total
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.listLoading = false
      })
    },
    // 弹窗内选择操作
    choosePaper(row) {
      // console.log('点击选择');
      this.$emit('choosePaper', row)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-checkbox.is-bordered.is-checked {
  border-color: #F56C6C;
}
::v-deep .el-checkbox__input.is-checked .el-checkbox__inner,
::v-deep .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  border-color: #F56C6C !important;
  background-color: #F56C6C !important;
}

::v-deep .el-checkbox__input.is-checked + .el-checkbox__label {
  color: #F56C6C !important;
}

::v-deep .el-checkbox__inner:hover {
  border-color: #F56C6C !important;
}

::v-deep .el-checkbox__input.is-focus .el-checkbox__inner {
  border-color: #F56C6C !important;
}
.app-container {
  background-color: #fff;
  padding: 30px;
  //min-height: calc(100vh - 84px);
}
.filter-container {
  margin-bottom: 15px;
}
</style>
