<template>
  <div class="normal-panel">
    <p class="normal-title container">编辑报价单</p>
<!--    <el-button size="mini" :disabled="data.status !== '新建'"  :type="btnType?btnType:'text'"  @click="onShow" >编 辑</el-button>-->
<!--    <el-button size="mini" :disabled="data.status !== '新建'"  :type="data.status === '新建'?primary:text"  @click="onShow" >编 辑</el-button>-->
    <div class="drawer__panel" style="margin-bottom: 0px">
      <el-row style="margin-top: 10px">
        <el-form :model="form" :rules="rules" ref="form"  size="mini" label-position="right">
          <el-col :span="8">
            <el-form-item label="报价类型:" label-width="100px" >
              <el-input v-model="form.quotedpricetype" autocomplete="off" placeholder="报价类型" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col  :span="8">
            <el-form-item label="是否特价:" label-width="100px" >
              <el-input  v-model="form.specialoffer === 0?'否':'是'" autocomplete="off" placeholder="是否特价" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col  :span="8" v-if="isQuotedPrice">
            <el-form-item label="项目名称:" label-width="100px" prop="projectname">
              <el-popover
                  placement="bottom"
                  width="700"
                  trigger="click"
                  v-model="projectShow"
                  @show="projectList">
                <el-input
                    style="width:300px;margin-bottom: 10px"
                    placeholder="请输入内容"
                    v-model="projectParam.content.where.condition"
                    clearable
                    @clear="projectList(projectParam.content.pageNumber = 1)"
                    size="mini"
                    @keyup.enter.native="projectList(projectParam.content.pageNumber = 1)">
                  <i slot="prefix" class="el-icon-search" @click="projectList(projectParam.content.pageNumber = 1)"></i>
                </el-input>
                <el-table :data="project.projectData" @row-click="projectOpen" height="396px" :header-cell-style="{background:'#EEEEEE',color:'#333'}" size="mini">
                  <el-table-column
                      label="项目编号"
                      width="180">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{ scope.row.projectnum?scope.row.projectnum:'--' }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                      label="项目名称"
                      width="180">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{ scope.row.projectname?scope.row.projectname:'--' }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                      label="地址"
                      >
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{ scope.row.address?scope.row.address:'--' }}</span>
                    </template>
                  </el-table-column>
                </el-table>
                <div class="container normal-panel" style="text-align:right">
                  <el-pagination
                      background
                      small
                      @size-change="handleSizeChangeProject"
                      @current-change="handleCurrentChangeProject"
                      :page-sizes="[10,20,50,100,]"
                      layout="total,sizes, prev, pager, next, jumper"
                      :current-page="project.currentPage"
                      :total="project.total">
                  </el-pagination>
                </div>
                <el-input slot="reference"  :readonly="true" v-model="form.projectname" autocomplete="off" placeholder="请选择项目"  @input="selectProject"></el-input>
              </el-popover>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="isQuotedPrice">
            <el-form-item label="项目编号:" label-width="100px" >
              <el-input  v-model="form.projectnum" autocomplete="off" placeholder="项目编号" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户名称:" label-width="100px" prop="enterprisename">
              <el-popover
                  placement="bottom"
                  width="700"
                  trigger="click"
                  v-model="customerShow"
                  @show="customerList">
                <el-input
                    style="width:300px;margin-bottom: 10px"
                    placeholder="请输入内容"
                    v-model="customerParam.content.where.condition"
                    clearable
                    @clear="customerList(customerParam.content.pageNumber = 1)"
                    size="mini"
                    @keyup.enter.native="customerList(customerParam.content.pageNumber = 1)">
                  <i slot="prefix" class="el-icon-search" @click="customerList(customerParam.content.pageNumber = 1)"></i>
                </el-input>
                <el-table :data="customer.customerData" @row-click="customerData" height="396px" :header-cell-style="{background:'#EEEEEE',color:'#333'}" size="mini">
                  <el-table-column
                      label="客户编号"
                      width="180">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{ scope.row.sa_customersid?scope.row.sa_customersid:'--' }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                      label="客户名称"
                      width="200">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{ scope.row.enterprisename?scope.row.enterprisename:'--'}}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                      label="地址"
                      >
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{ scope.row.address?scope.row.address:'--' }}</span>
                    </template>
                  </el-table-column>
                </el-table>
                <div class="container normal-panel" style="text-align:right">
                  <el-pagination
                      background
                      small
                      @size-change="handleSizeChangeCustomer"
                      @current-change="handleCurrentChangeCustomer"
                      :page-sizes="[10,20,50,100,]"
                      layout="total,sizes, prev, pager, next, jumper"
                      :current-page="customer.currentPage"
                      :total="customer.total">
                  </el-pagination>
                </div>
                <el-input slot="reference"  :readonly="true" v-model="form.enterprisename" autocomplete="off" placeholder="请选择客户"  @input="selectCustomer"></el-input>
              </el-popover>
            </el-form-item>
          </el-col>
          <el-col  :span="8">
            <el-form-item label="电话:" label-width="100px" >
              <el-input  v-model="form.telephone" autocomplete="off" placeholder="输入电话" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人:" label-width="100px" prop="contactsname">
              <el-popover
                  placement="bottom"
                  width="700"
                  trigger="click"
                  v-model="contactsShow"
                  @show="contactsList">
                <el-input
                    style="width:300px;margin-bottom: 10px"
                    placeholder="请输入内容"
                    v-model="contactsParam.content.where.condition"
                    clearable
                    @clear="contactsList(contactsParam.content.pageNumber = 1)"
                    size="mini"
                    @keyup.enter.native="contactsList(contactsParam.content.pageNumber = 1)">
                  <i slot="prefix" class="el-icon-search" @click="contactsList(contactsParam.content.pageNumber = 1)"></i>
                </el-input>
                <el-table :data="contacts.contactsData" @row-click="contactsData" height="396px" :header-cell-style="{background:'#EEEEEE',color:'#333'}" size="mini">
                  <el-table-column
                      label="姓名"
                      width="180">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{ scope.row.name?scope.row.name:'--' }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                      label="手机号"
                      width="180">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{ scope.row.phonenumber?scope.row.phonenumber:'--' }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                      label="邮箱"
                      width="180">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{ scope.row.email?scope.row.email:'--' }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column
                      label="地址"
                  >
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{ scope.row.address?scope.row.address:'--' }}</span>
                    </template>
                  </el-table-column>
                </el-table>
                <div class="container normal-panel" style="text-align:right">
                  <el-pagination
                      background
                      small
                      @size-change="handleSizeChangeContacts"
                      @current-change="handleCurrentChangeContacts"
                      :page-sizes="[10,20,50,100,]"
                      layout="total,sizes, prev, pager, next, jumper"
                      :current-page="contacts.currentPage"
                      :total="contacts.total">
                  </el-pagination>
                </div>
                <el-input slot="reference"  :readonly="true" v-model="form.contactsname" autocomplete="off" placeholder="请选择联系人"  @input="selectContacts"></el-input>
              </el-popover>
            </el-form-item>
          </el-col>
          <el-col  :span="8">
            <el-form-item label="联系人手机号:" label-width="100px" >
              <el-input  v-model="form.contactsphonenumber" autocomplete="off" placeholder="手机号" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col  :span="8">
            <el-form-item label="报价日期:" label-width="90px">
              <el-date-picker
                  v-model="form.billdate"
                  style="width: 100%"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期"
                  :picker-options="pickerOptions">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="有效期:" label-width="100px" prop="date">
              <el-date-picker
                  v-model="form.data"
                  style="width: 100%"
                  type="daterange"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :picker-options="pickerOptions">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col  :span="8" >
            <el-form-item label="产品系列:" label-width="100px" prop="itemtype">
              <el-select
                  ref="sle"
                  style="width:100%;"
                  v-model="form.itemtype"
                  multiple
                  placeholder="请选择产品系列"
                  size="mini"
              >
                <el-option
                    v-for="item in itemtype"
                    :key="item.index"
                    :label="item.value"
                    :value="item.value"
                >
                  <span style="float: left">{{ item.value }}</span>
                  <span style="float: right; color: #8492a6; font-size: 12px">{{ item.remarks?item.remarks:'暂无描述' }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col  :span="8">
            <el-form-item label="备注:" label-width="100px" prop="remarks">
              <el-input  v-model="form.remarks" autocomplete="off" placeholder="请输入备注信息" type="textarea" autosize></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </div>
    <div >
      <el-divider></el-divider>
    </div>
    <div style="margin:20px 20px 20px 20px">
      <quotedPriceProduct :sa_quotedpriceid="form.sa_quotedpriceid" :sa_projectid="form.sa_projectid" :quotedpricetype="form.quotedpricetype" ref="quoterPrice" @checkForm="onSubmit" @productData="productDataChange"></quotedPriceProduct>
    </div>
    <div class="fixed__btn__panel">
      <el-button size="small" @click="onClose" class="normal-btn-width">取 消</el-button>
      <el-button size="small" type="warning" @click="getSubmit" class="normal-btn-width">保 存</el-button>
    </div>
  </div>
</template>

<script>
import quotedPriceProduct from '@/SDrpManagement/QuotedPrice/components/edit/quotedPriceProduct'
export default {
  name: "edit",
  props:['btnType'],
  components:{quotedPriceProduct},
  data(){
    return {
      projectShow:false,
      customerShow:false,
      contactsShow:false,
      data:'',
      primary:'primary',
      text:'',
      projectParam:{
        "id": 20221020143502,
        "content": {
          "pageNumber": 1,
          "pageSize": 10,
          "type":0, //  1：我负责的；2：我参与的；3：我下属负责的；4：我下属参与的
          "where": {
            "condition": "",
            "startdate": "",
            "enddate": "",
            "status":"",//跟进中、已成交、已失败
            "projecttype":"",
            "stagename":"",
            "tag":""
          }
        },
      },
      project:{
        projectData:[],
        total:0,
        currentPage:0,
      },
      customerParam:{
        "id": 20220920083901,
        "content": {
          "pageNumber": 1,
          "pageSize": 10,
          "where": {
            "condition": "",
            "type":4,
            "sa_projectid":""
          }
        }
      },
      customer:{
        customerData:[],
        total:0,
        currentPage:0,
      },
      contactsParam:{
        "id": "20221022165503",
        "version": 1,
        "content": {
          "sys_enterpriseid":"",
          "where": {
            "condition": "",
            "workaddress": 0//0表示人，1表示地址
          }
        }
      },
      contacts:{
        contactsData:[],
        total:0,
        currentPage:0,
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() + 3600 * 1000 * 24 < Date.now() ;
        },
      },
      drawer:true,
      isQuotedPrice:false,
      date:[],
      clearData:[],
      form:{
        quotedpricetype:'',
        totalPrice:0,
        type:'',
        projectnum:'',
        enterprisename:'',
        name:'',
        phonenumber:'',
        fax:'',
        telephone:'',
        specialOffer:'否',
        sa_quotedpriceid:0,     //sat_notice_classid<=0时 为新增
        sys_enterpriseid: "",
        contactsid:8,
        sa_projectid: "0",
        discountrate: 0,
        remarks: "",
        invaliddate: "",
        projectname: "",
        address: "",
        billdate:"",
        enddate:'',
        begdate:'',
        date:[],
        itemtype:[],
        quotedpricenotes: [
          "1、报价为含税13%税率, 含运费 ",
          "2、报价有限期 2023-02-02 13:16:47至2023-02-22 13:16:47",
        ]
      },
      rules:{
        projectname:[
          { required: true, message: '请选择项目', trigger: 'change'},
        ],
        enterprisename:[
          { required: true, message: '请选择客户', trigger: 'change'},
        ],
        itemtype:[
          { required: true, message: '请选择产品系列', trigger: 'change'},
        ],
        date:[
          { required: true, message: '请选择有效期', trigger: 'change'},
        ],
        /*contactsname:[
          { required: true, message: '请选择联系人', trigger: 'change'},
        ]*/
      },
      itemtype:[]
    }
  },
  methods:{
    onShow(){
      this.drawer = true
      this.data = this.$route.query.data
      this.form = Object.assign({},this.form,this.$route.query.data)
      this.form.date[0] = this.form.begdate
      this.form.date[1] = this.form.enddate
      if (this.form.quotedpricetype === '项目报价'){
        this.isQuotedPrice = true
      }else {
        this.isQuotedPrice = false
      }
    },
    onClose(){
      this.$store.dispatch('changeDetailDrawer',false)
      this.$emit('onSuccess')
    },
    onSubmit(){
     if (this.form.data.length !== 0){
       this.form.begdate = this.form.date[0]
       this.form.enddate = this.form.date[1]
       this.form.quotedpricenotes[1] = "2、报价有限期" + this.form.begdate + "至" + this.form.enddate
     }else {
       this.form.begdate = ''
       this.form.enddate = ''
     }
    this.$refs.form.validate(async (valid) => {
      if (!valid) return false
      const res = await this.$api.requested({
        "id": 20221020164803,
        "content": this.form
      })
    })
    },
    getSubmit(){
      if (this.form.date.length === 0){
        this.form.begdate = ''
        this.form.enddate = ''
      }else {
        this.form.begdate = this.form.date[0]
        this.form.enddate = this.form.date[1]
        this.form.quotedpricenotes[1] = "2、报价有限期" + this.form.begdate + "至" + this.form.enddate
      }
      this.$refs.form.validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": 20221020164803,
          "version":1,
          "content": this.form
        })
        this.tool.showMessage(res,() => {
          this.drawer = false
          this.changeData(this.productObj)

        })
      })
    },
    /*修改后的产品数据*/
    productDataChange(val){
      let obj = val.map(e=>{
        return {
          sa_quotedprice_itemsid:e.sa_quotedprice_itemsid,
          itemid:e.itemid,
          oldprice:e.marketprice,
          price:e.price,
          discountrate:(e.discountrate/100).toFixed(4),
          qty:e.qty
        }
      })
      this.productObj = obj
    },
    async changeData(obj){
      const res = await this.$api.requested({
        "id": 20221021095403,
        "content": {
          "sa_quotedpriceid":this.form.sa_quotedpriceid,     //sat_notice_classid<=0时 为新增
          "items":obj
        }
      })
      this.$emit('onSuccess')
      this.$store.dispatch('changeDetailDrawer',false)
    },
    /*刷新总金额*/
    queryTotalPrice(val){
      this.form.totalPrice = val
    },
    async projectList(){
      const res = await this.$api.requested(this.projectParam)
      this.project.projectData = res.data
      this.project.total = res.total
      this.project.currentPage = res.pageNumber
    },
    async selectProject(){
      this.projectShow = true
      this.projectParam.content.where.condition = this.form.projectname
      this.projectList()
    },
    async customerList(){
      if (this.form.quotedpricetype === '客户报价'){
        this.customerParam.content.where.sa_projectid = 0
        this.customerParam.content.where.type = 2
      }else {
        this.customerParam.content.where.type = 4
        this.customerParam.content.where.sa_projectid = this.form.sa_projectid
      }
      const res = await this.$api.requested(this.customerParam)
      this.customer.customerData = res.data
      this.customer.total = res.total
      this.customer.currentPage = res.pageNumber
    },
    selectCustomer(){
      this.customerParam.content.where.condition = this.form.enterprisename
      this.customerList()
    },
    async contactsList(){
      this.contactsParam.content.sys_enterpriseid = this.form.sys_enterpriseid
      const res = await this.$api.requested(this.contactsParam)
      this.contacts.contactsData = res.data
      this.contacts.total = res.total
      this.contacts.currentPage = res.pageNumber
    },
    selectContacts(){
      this.contactsShow = true
      this.contactsParam.content.where.condition = this.form.name
      this.contactsList()
    },
    /*项目选择信息*/
    projectData(val){
      this.form.sa_projectid = val.sa_projectid
      this.form.projectname = val.projectname
      this.form.projectnum = val.projectnum
      this.form.sys_enterpriseid = ''
      this.form.enterprisename = ''
      this.form.contactsid = 0
      this.form.contactsname = ''
      this.form.contactsphonenumber = ''
      this.form.fax = ''
      this.form.telephone = ''
      this.projectShow = false
    },
    /*更改项目前提示*/
    projectOpen(val) {
      this.$confirm('修改项目后将清空产品配置, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.productList()
        this.projectData(val)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    /*获取产品明细*/
    async productList(){
      const res = await this.$api.requested({
        "id": 20221021095503,
        "content": {
          "sa_quotedpriceid":this.form.sa_quotedpriceid     //sat_notice_classid<=0时 为新增
        }
      })
      console.log(res,"产品明细")
      this.clearData = []
      res.data.forEach((item,index)=>{
        console.log(item)
        this.clearData[index] = item.sa_quotedprice_itemsid
      })
      console.log(this.clearData,"需要删除的产品明细")
      this.productClear()
    },
    /*清空产品明细*/
    async productClear(){
      const res = await this.$api.requested({
        "id": 20221021095603,
        "content": {
          "sa_quotedprice_itemsids":this.clearData     //sat_notice_classid<=0时 为新增
        }
      })
      this.$refs.quoterPrice.productData()
    },
    /*客户选择信息*/
    customerData(val){
      this.form.sys_enterpriseid = val.sys_enterpriseid
      this.form.enterprisename = val.enterprisename
      this.form.contactsid = 0
      this.form.contactsname = ''
      this.form.contactsphonenumber = ''
      this.form.fax = ''
      this.form.telephone = ''
      this.customerShow = false
    },
    /*联系人选择信息*/
    contactsData(val){
      this.form.contactsid = val.contactsid
      this.form.contactsname = val.name
      this.form.contactsphonenumber = val.phonenumber
      this.form.fax = val.fax
      this.form.telephone = val.telephone
      this.contactsShow = false
    },
    /*项目翻页*/
    handleSizeChangeProject(val) {
      // console.log(`每页 ${val} 条`);
      this.projectParam.content.pageSize = val
      this.projectList()
    },
    handleCurrentChangeProject(val) {
      // console.log(`当前页: ${val}`);
      this.projectParam.content.pageNumber = val
      this.projectList()
    },
    /*客户翻页*/
    handleSizeChangeCustomer(val) {
      // console.log(`每页 ${val} 条`);
      this.projectParam.content.pageSize = val
      this.customerList()
    },
    handleCurrentChangeCustomer(val) {
      // console.log(`当前页: ${val}`);
      this.projectParam.content.pageNumber = val
      this.customerList()
    },
    /*联系人翻页*/
    handleSizeChangeContacts(val) {
      // console.log(`每页 ${val} 条`);
      this.projectParam.content.pageSize = val
      this.contactsList()
    },
    handleCurrentChangeContacts(val) {
      // console.log(`当前页: ${val}`);
      this.projectParam.content.pageNumber = val
      this.contactsList()
    },
    /*查询产品系列*/
    queryItemType(){
      this.$store.dispatch('optiontypeselect','itemtype').then(res => {
        this.itemtype = res.data
      })
    }
  },
  mounted() {
    this.queryItemType()
  },
  created() {
    this.onShow()
    this.data = this.$route.query.data
  }
}
</script>

<style scoped>
>>> .el-divider--horizontal {
  height: 1px;
  width: 100%;
  margin: 0px;
}
/deep/.el-input__prefix {
  display: flex;
  align-items: center;
}

</style>