<template>
  <div class="salesEdit-wrap">
    <a-page-header :ghost="false" :backIcon="false" @back="handleBack" >
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="salesEdit-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
        <span style="margin: 0 15px;color: #666;">客户名称：{{ detailData&&detailData.buyerName }}</span>
        <a-button id="salesEdit-edit-btn" size="small" @click="handleEdit" style="margin-left:10px" key="0">编辑</a-button>
      </template>
      <!-- 操作区，位于 title 行的行尾 -->
      <template slot="extra">
        <a-button key="2" id="salesEdit-preview-btn">打印预览</a-button>
        <a-button key="1" type="primary" id="salesEdit-print-btn">快速打印</a-button>
      </template>
    </a-page-header>
    <a-card size="small" :bordered="false" class="salesEdit-cont">
      <!-- 查询配件列表 -->
      <queryPart @add="insterProduct"></queryPart>
    </a-card>
    <a-card size="small" :bordered="false" class="salesEdit-cont">
      <!-- 总计 -->
      <a-alert style="margin-bottom: 15px;" type="info">
        <div slot="message">总售价：<strong>6</strong>；总款数：<strong>6</strong>；总数量：<strong>6</strong>；赠品总数量：<strong>6</strong>；</div>
      </a-alert>
      <!-- 查询条件 -->
      <a-row :gutter="15">
        <a-col :span="18">
          <a-form-model layout="inline" :model="productForm" @submit="handleSearch">
            <a-form-model-item label="产品编码">
              <a-input v-model="productForm.code" placeholder="输入产品编码" />
            </a-form-model-item>
            <a-form-model-item label="产品名称">
              <a-input v-model="productForm.name" placeholder="输入产品名称" />
            </a-form-model-item>
            <a-form-model-item>
              <a-button style="margin-bottom: 18px;" type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="salesEdit-refresh">查询</a-button>
              <a-button style="margin: 0 0 18px 8px" @click="resetSearchForm" :disabled="disabled" id="salesEdit-reset">重置</a-button>
            </a-form-model-item>
          </a-form-model>
        </a-col>
        <a-col :span="6">
          <div style="float:right;overflow: hidden;">
            <a-button size="small" id="salesEdit-dru">导入明细</a-button>
            <a-button size="small" type="primary" @click="delSalerDetailAll" style="margin-left: 10px" id="salesEdit-del-all">整单删除</a-button>
          </div>
        </a-col>
      </a-row>
      <!-- 已选配件列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="default"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 1560, y: 150 }"
        bordered>
        <!-- 售价 -->
        <template slot="price" slot-scope="text, record">
          <editable-cell
            :text="record.price"
            id="salesEdit-price"
            :max="999999"
            :min="0"
            :precision="2"
            @change="onCellChange(record.id, 'price', $event)" />
        </template>
        <!-- 销售数量 -->
        <template slot="salesNums" slot-scope="text, record">
          <editable-cell
            :text="record.qty"
            id="salesEdit-salesNums"
            :max="999999"
            :min="1"
            :precision="0"
            @change="onCellChange(record.id, 'qty', $event)" />
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button size="small" type="link" @click="handleDel(record)" id="productInfoList-Del">删除</a-button>
        </template>
      </s-table>
    </a-card>
    <!-- 选择客户弹框 -->
    <choose-custom-modal ref="custModal" :show="openModal" @ok="chooseCustomOk" @cancel="openModal=false" />
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import queryPart from './queryPart.vue'
import chooseCustomModal from './chooseCustomModal.vue'
import { salesDetail } from '@/api/sales'
import EditableCell from '@/views/common/editInput.js'
import { salesDetailList, salesDetailInsert, salesDetailUpdatePrice, salesDetailUpdateQty, salesDetailDel, salesDetailDelAll } from '@/api/salesDetail'
export default {
  name: 'SalesDetail',
  components: { STable, VSelect, queryPart, chooseCustomModal, EditableCell },
  data () {
    return {
      orderId: null, // 销售单id
      salesBillSn: null, // 销售单sn
      disabled: false, //  查询、重置按钮是否可操作
      openModal: false, // 客户弹框
      detailData: null, // 订单基础数据
      dataSource: [],
      productForm: {
        code: '',
        name: ''
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'dealerProductEntity.code', width: 140, align: 'center', sorter: true },
        { title: '产品名称', dataIndex: 'dealerProductEntity.name', align: 'center' },
        { title: '原厂编码', dataIndex: 'dealerProductEntity.origCode', width: 140, align: 'center' },
        { title: '品牌', dataIndex: 'dealerProductEntity.productBrandName', width: 100, align: 'center', sorter: true },
        { title: '仓库', dataIndex: 'warehouseEntity.name', width: 100, align: 'center', sorter: true },
        { title: '仓位', dataIndex: 'warehouseLocationEntity.name', width: 100, align: 'center' },
        { title: '售价', scopedSlots: { customRender: 'price' }, width: 150, align: 'center' },
        { title: '销售数量', scopedSlots: { customRender: 'salesNums' }, width: 150, align: 'center' },
        { title: '单位', dataIndex: 'dealerProductEntity.unit', width: 100, align: 'center' },
        { title: '售价小计', dataIndex: 'xj', width: 100, align: 'center' },
        { title: '折后小计', dataIndex: 'zhxj', width: 100, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 170, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return salesDetailList(Object.assign(parameter, this.productForm)).then(res => {
          const data = res.data
          const no = (data.pageNo - 1) * data.pageSize
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
          }
          this.disabled = false
          this.dataSource = data.list
          return data
        })
      }
    }
  },
  methods: {
    onCellChange (id, key, value) {
      console.log(id, key, value)
      const dataSource = [...this.dataSource]
      const row = dataSource.find(item => item.id === id)
      if (row) {
        row[key] = Number(value)
        this.dataSource = dataSource
      }
      // 修改价格
      if (key == 'price') {
        salesDetailUpdatePrice({
          salesBillSn: row.salesBillSn,
          productSn: row.productSn,
          price: row.price
        }).then(res => {
          if (res.status == 200) {
            this.$refs.table.refresh(true)
          }
          this.$message.info(res.message)
        })
      }
      if (key == 'qty') {
        salesDetailUpdateQty({
          id: row.id,
          qty: row.qty
        }).then(res => {
          if (res.status == 200) {
            this.$refs.table.refresh(true)
          }
          this.$message.info(res.message)
        })
      }
    },
    handleSearch () {

    },
    // 选择客户成功
    chooseCustomOk (data) {

    },
    // 编辑客户信息
    handleEdit () {
      this.openModal = true
      this.$refs.custModal.editCust(this.detailData)
    },
    //  重置
    resetSearchForm () {
      this.$refs.table.refresh()
    },
    //  返回
    handleBack () {
      this.$router.push({ path: '/salesManagement/salesQuery/list' })
    },
    // 整单删除
    delSalerDetailAll () {
      salesDetailDelAll({ salesBillSn: this.detailData.salesBillSn }).then(res => {
        if (res.status == 200) {
          this.resetSearchForm()
        }
        this.$message.info(res.message)
      })
    },
    // 删除产品
    handleDel (row) {
      salesDetailDel({ id: row.id }).then(res => {
        if (res.status == 200) {
          this.resetSearchForm()
        }
        this.$message.info(res.message)
      })
    },
    // 添加产品
    insterProduct (row) {
      console.log(row)
      salesDetailInsert({
        productSn: row.productSn,
        price: row.dealerProduct.cityPrice,
        qty: row.salesNums,
        salesBillSn: this.detailData.salesBillSn,
        salesBillNo: this.detailData.salesBillNo,
        warehouseSn: row.warehouseSn,
        warehouseLocationSn: row.warehouseLocationSn,
        stockSn: row.stockSn
      }).then(res => {
        console.log(res)
        if (res.status == 200) {
          this.resetSearchForm()
        }
      })
    },
    //  详情
    getOrderDetail () {
      salesDetail({ id: this.$route.params.id }).then(res => {
        if (res.status == 200) {
          this.detailData = res.data
        } else {
          this.detailData = null
        }
      })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      // 获取销售单详情
      vm.getOrderDetail()
      vm.orderId = vm.$route.params.id
    })
  }
}
</script>

<style lang="less">
  .salesEdit-wrap{
    .salesEdit-cont{
      margin-top: 15px;
    }
  }

</style>
