<template>
  <div>
    <el-button :disabled="data.status !=='新建'" type="primary" size="mini" @click="onShow(drawer = true)">编辑</el-button>
    <el-drawer
    :visible.sync="drawer"
    direction="rtl"
    append-to-body
    size="80%">
    <div slot="title">
      <div class="flex-align-center">
        <p><span style="font-size:14px">订单合计:</span><span style="color:red;font-size:16px;"><b>¥&nbsp;{{tool.formatAmount(data.amount,2)}}</b></span></p>
        <label style="font-size:14px;margin-left:10px" for="">备注:</label>
        <input class="order__note__input" v-model="defaultData.remarks" type="text" placeholder="请输入订单备注">
      </div>
    </div>
    <div class="drawer__panel">
      <div class="flex-align-center normal-margin">
        <div style="flex:1;margin-right:10px">
          <div class="mt-10">
            <p class="normal-title inline-16">收货信息</p>
            <el-popover
              placement="bottom"
              title="选择地址"
              trigger="click"
              v-model="visible">
              <el-table
                :data="receiveAddresslist"
                style="width: 100%"
                size="mini">
                <el-table-column
                  prop="name"
                  label="联系人"
                  width="90">
                </el-table-column>
                <el-table-column
                  prop="phonenumber"
                  label="电话"
                  width="120">
                </el-table-column>
                <el-table-column
                  label="地址"
                  width="360">
                  <template slot-scope="scope">
                    {{scope.row.province}}{{scope.row.city}}{{scope.row.county}}{{scope.row.address}}
                  </template>
                </el-table-column>
                <el-table-column
                  label="操作"
                  width="90">
                  <template slot-scope="scope">
                    <el-button type="text" @click="(defaultData.re_info = scope.row,visible = false)" size="mini">选 择</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <el-button slot="reference" type="text" size="mini">选择其他地址</el-button>
            </el-popover>
          </div>
          <div class="reveive__panel normal-margin">
            <p><span class="inline-16"><b>{{defaultData.re_info.name}}</b></span><small>{{defaultData.re_info.phonenumber}}</small></p>
            <small>{{defaultData.re_info.province}}{{defaultData.re_info.city}}{{defaultData.re_info.county}}{{defaultData.re_info.address?defaultData.re_info.address:'--'}}</small>
          </div>
        </div>
        <div style="flex:1">
          <div class="mt-10">
            <p class="normal-title inline-16">物流公司</p>
            <el-popover
              placement="bottom"
              title="选择公司"
              trigger="click"
              v-model="visible1">
              <el-table
                :data="logisticcomlist"
                style="width: 100%"
                size="mini">
                <el-table-column
                  prop="abbreviation"
                  label="公司简称"
                  width="120">
                </el-table-column>
                <el-table-column
                  prop="kd100id"
                  label="快递100ID"
                  width="120">
                </el-table-column>
                <el-table-column
                  label="操作"
                  width="90">
                  <template slot-scope="scope">
                    <el-button type="text" @click="(defaultData.logist_info = scope.row,visible1 = false)" size="mini">选 择</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <el-button slot="reference" type="text" size="mini">选择其他公司</el-button>
            </el-popover>
          </div>
          <div class="reveive__panel normal-margin">
            <p><span class="inline-16"><b>{{defaultData.logist_info.abbreviation}}</b></span></p>
            <small>快递100ID: {{defaultData.logist_info.kd100id}}</small>
          </div>
        </div>
      </div>
      <div class="mt-10">
        <p class="normal-title inline-16">选择账户</p>
      </div>
      <div class="flex-align-center-wrap flex-between normal-margin">
        <div class="account__panel" :class="defaultData.accountclass.sa_accountclassid === item.sa_accountclassid?'act':''" v-for="(item) in accountlist" :key="item.index" @click="defaultData.accountclass = item">
          <p><b>¥{{tool.formatAmount(item.balance,2)}}</b></p>
          <small>{{item.accountname}}</small>
        </div>
      </div>
      <div class="mt-10">
        <p class="normal-title inline-16">选择财务信息</p>
        <el-popover
          placement="bottom"
          title="选择公司"
          trigger="click"
          v-model="visible2">
          <el-table
            :data="financiallist"
            style="width: 100%"
            size="mini">
            <el-table-column
              prop="enterprisename"
              label="公司抬头"
              width="120">
            </el-table-column>
            <el-table-column
              prop="address"
              label="开票地址"
              width="120">
            </el-table-column>
            <el-table-column
              prop="bank"
              label="开户行"
              width="120">
            </el-table-column>
            <el-table-column
              prop="bankcardno"
              label="开户账号"
              width="120">
            </el-table-column>
            <el-table-column
              label="操作"
              width="90">
              <template slot-scope="scope">
                <el-button type="text" @click="(defaultData.fin_info = scope.row,visible2 = false)" size="mini">选 择</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-button slot="reference" type="text" size="mini">选择其他</el-button>
        </el-popover>
      </div>
      <div class="reveive__panel normal-margin">
        <p><small>抬头:&nbsp;</small><span class="inline-16"><b>{{defaultData.fin_info.enterprisename}}</b></span></p>
        <p><small>开票地址:&nbsp;</small>{{defaultData.fin_info.address}}</p>
        <p><small>开户行:&nbsp;</small> {{defaultData.fin_info.bank}} &emsp;<small>开户账号:&nbsp;</small> {{defaultData.fin_info.bankcardno}}</p>
      </div>
      <div class="mt-10">
        <div>
          <p class="normal-title inline-16">订单产品</p>
          <el-button type="text" size="mini" @click="addMoreProduct">添加更多产品</el-button>
        </div>
        <el-row :gutter="10">
          <el-col :span="setcol">
            <product-list ref="prolist" :type="'edit'" @onSuccess="onDelSuccess"></product-list>
          </el-col>
          <el-col :span="24 - setcol">
            <add-product ref="addpro" @onConfirm="onConfirm"></add-product>
          </el-col>
        </el-row>
      </div>
    </div>
    <div class="fixed__btn__panel">
      <el-button size="small" @click="drawer = false" class="normal-btn-width">取 消</el-button>
      <el-button size="small" type="primary" @click="onSubmit" class="normal-btn-width">保存修改</el-button>
    </div>
  </el-drawer>
  </div>
</template>

<script>
import previewImage from '@/components/previewImage/index.vue'
import productList from './productlist.vue'
import addProduct from '@/template/orderCanUseProduct/index.vue'
export default {
  props:['data'],
  components:{
    previewImage,
    productList,
    addProduct
  },
  data () {
    return {
      visible:false,
      visible1:false,
      visible2:false,
      visible3:false,
      drawer:false,
      receiveAddresslist:[],
      accountlist:[],
      logisticcomlist:[],
      financiallist:[],
      defaultData:{
        re_info:{},
        logist_info:{},
        fin_info:{},
        accountclass:{},
        account_index:0
      },
      agnetInfo:{},
      total:0,
      setcol:24
    }
  },
  methods:{
    onShow () {
      this.queryAgentiInfo()
    },
    async queryAgentiInfo () {
      const res = await this.$api.requested({
        "id": "20221022165203",
        "content": {}
      })
      this.agnetInfo = res.data

      this.receiveAddress()

      this.queryAccount()

      this.queryLogisticcom()

      this.queryFinancial()
    },
    // 收货信息:合作企业联系人
    async receiveAddress () {
      const res = await this.$api.requested({
        "id": "20221009155803",
        "content": {
        "sys_enterpriseid":this.agnetInfo.sys_enterpriseid,
          "where":{
            "condition":"",
            "workaddress":0
          }
        }
      })
      this.receiveAddresslist = res.data
      this.defaultData.re_info = res.data[0]
    },

    // 账号信息
    async queryAccount () {
      const res = await this.$api.requested({
        "id": "20221008134803",
        "content": {
          "pageNumber": 1,
          "pageSize": 100
        }
      })
      this.accountlist = res.data
      this.defaultData.accountclass = this.data.accountclass
    },

    // 物流企业
    async queryLogisticcom () {
      const res = await this.$api.requested({
        "id": "20221121135804",
        "content": {
          "pageNumber": 1,
          "pageSize": 100
        }
      })
      this.logisticcomlist = res.data
      this.defaultData.logist_info = this.data.logiscomp
    },
    // 财务信息
    async queryFinancial () {
      const res = await this.$api.requested({
        "id": "20221013160602",
        "content": {
          "sys_enterpriseid":this.agnetInfo.sys_enterpriseid,
          "pageNumber": 1,
          "pageSize": 100
        }
      })
      this.financiallist = res.data
      this.defaultData.fin_info = this.data.finance
    },
    async onSubmit() {
      console.log(this.data)
      const res = await this.$api.requested({
        "id": 20221108111402,
        "content": {
          "sa_orderid": this.$route.query.id,
          "sys_enterpriseid": 1, //订货企业id
          "sa_accountclassid": this.defaultData.accountclass.sa_accountclassid, //营销账户类型ID
          "sa_brandid": this.data.sa_brandid, //品牌ID
          // "sa_contractid":this.data.sa_contractid, //合同ID，标准订单不传
          // "sa_projectid": this.data.sa_projectid, //工程项目表ID，标准订单不传
          "sys_enterprise_financeid": this.defaultData.fin_info.sys_enterprise_financeid, //合作企业财务信息ID（开票信息）
          "sa_logiscompid": this.defaultData.logist_info.sa_logiscompid, //物流公司档案ID
          "rec_contactsid": this.defaultData.re_info.contactsid, //合作企业联系人表ID（收货信息）
          "type": this.data.type, //订单类型
          "typemx":this.data.typemx, //明细分类，可选
          "remarks": this.data.remarks, //可选
          // "saler_hrid":this.data.saler_hrid//销售人员hrid,业务员hrid
        },
      })
      this.tool.showMessage(res,()=>{
        this.$emit('onSuccess')
        this.drawer = false
      })
    },
    onDelSuccess () {
      this.$refs.addpro.listData()
    },
    addMoreProduct () {
      this.setcol = 12
    },
    // 订单添加商品
    async onConfirm (data) {
      const res = await this.$api.requested({
        "id": 20221109093602,
        "content": {
          "sa_orderid": this.$route.query.id, //订单ID
          "sys_enterpriseid": this.data.sys_enterpriseid, //企业ID
          "type": "标准订单", //订单类型
          "items": data.map(e=>{
            return {
              sa_orderitemsid:0,
              itemid:e.itemid,
              qty:e.orderminqty
            }
          })
        },
      })
      this.tool.showMessage(res,()=>{
        this.setcol = 24
        this.$refs['prolist'].listData()
      })
    }
  },
  mounted () {
  }
}

</script>
<style>
</style>
<style scoped>
.reveive__panel{
  font-size: 14px;
  line-height: 30px;
  padding: 10px;
  background: #d9ecff;
  border: 1px dashed #0676e7;
}
.account__panel small,.reveive__panel small{
  color:#666
}
.account__panel{
  width: calc(33% - 26px);
  padding: 10px;
  margin-bottom:10px;
  border: 1px dashed #666;
}
.act{
  background: #d9ecff;
  border: 1px dashed #0676e7;
}
.image {
  width:40px;height:40px;margin:0px auto;
}
.order__note__input{
  border: none;
  outline: none;
  /* margin-left: 10px; */
  /* border-bottom: 1px solid #f1f2f3; */
  flex:1
}
</style>