<template>
  <div>
    <el-button size="mini" type="primary" @click="editBtn">编 辑</el-button>
    <el-drawer title="新 建" append-to-body :show-close="false" :visible.sync="dialogFormVisible" size="50%">
      <div slot="title" style="font-size: 15px">
        编辑服务申请单
      </div>
      <div class="drawer__panel">
        <el-row :gutter="20">
          <el-form :model="form" :rules="rules" ref="form" label-width="140px" label-position="right" size="mini">
            <el-col :span="12">
              <el-form-item label="经销商:" prop="enterprisename">
                  <selectAgent :customParam="agentParam" ref="agent" @selectRow="agentChange">
                    <el-input readonly :disabled="accountInfo.usertype!=1 && accountInfo.usertype!=0" type="text" slot="input" size="mini" @focus="$refs.agent.visible=true" v-model="form.enterprisename"></el-input>
                  </selectAgent>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="服务类型" prop="servicetype">
                <el-select class="inline-24" v-model="form.servicetype" @change="classChange" placeholder="请选择状态" size="mini" clearable>
                  <el-option v-for="(item,index) in serveClassList" :key="index" :label="item.value" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.servicetype == '售中' || form.servicetype == '售后'">
              <el-form-item label="订单号:" prop="sonum">
                <selectOrder ref="order" @selectRow="orderChange">
                    <el-input readonly :disabled="!form.sys_enterpriseid" type="text" slot="input" size="mini" @focus="$refs.order.listData(),$refs.order.visible=true" v-model="form.sonum"></el-input>
                </selectOrder>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单据日期" prop="billdate">
                <el-date-picker
                  v-model="form.billdate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="省市县" prop="province">
                <el-cascader 
                  size="mini" 
                  v-model="form.province" 
                  :options="basicData.data().areaData"
                  ref="cascader">
                </el-cascader>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="地址" prop="address">
                <el-input v-model="form.address" placeholder="请输入地址"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="服务联系人" prop="scenecontact">
                <el-input v-model="form.scenecontact" placeholder="请输入服务联系人"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系人角色" prop="scenecontactrole">
                <el-input v-model="form.scenecontactrole" placeholder="请输入联系人角色"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="服务联系电话" prop="scenecontactphonenumber">
                <el-input v-model="form.scenecontactphonenumber" placeholder="请输入服务联系电话"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="应用系统" prop="class1">
                <el-select class="inline-24" v-model="form.class1" @change="classChange" placeholder="请选择应用系统" size="mini" clearable>
                  <el-option v-for="(item,index) in systemapp" :key="index" :label="item.value" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客诉大类" prop="class2">
                <el-select class="inline-24" v-model="form.class2" @change="classChange" placeholder="请选择客诉大类" size="mini" clearable>
                  <el-option v-for="(item,index) in customerclass" :key="index" :label="item.value" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请原因" prop="reason">
                <el-input v-model="form.reason" placeholder="请输入申请原因"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="需求服务开始日期" prop="begdate">
                <el-date-picker
                  v-model="form.begdate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="需求服务结束日期" prop="enddate">
                <el-date-picker
                  v-model="form.enddate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="remarks">
                <el-input type="textarea" v-model="form.remarks" placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div class="fixed__btn__panel">
        <el-button size="small" @click="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="warning" @click="onSubmit" class="normal-btn-width btn-warning">确 定</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import selectAgent from './selectAgent'
import selectOrder from './selectOrder'
import { log } from '@antv/g2plot/lib/utils'

export default {
  name: "add",
  props:['data'],
  components:{selectAgent,selectOrder},
  data(){
    return {
      dialogFormVisible:false,
      serveClassList:[],
      customerclass:[],
      systemapp:[],
      form:{
        "sa_serviceorderid":0,     //sa_brandid<=0时 为新增
        "servicetype":"",
        "sa_orderid":'',
        "sonum":"",
        "sys_enterpriseid":"",
        "enterprisename":"",
        "remarks":"",
        "class1":"",
        "class2":"",
        "reason":"",
        "province":"",
        "city":"",
        "county":"",
        "address":"",
        "scenecontact":"",
        "scenecontactrole":"",
        "scenecontactphonenumber":"",
        "billdate":new Date(),
        "begdate":"",
        "enddate":""
      },
      rules:{
        servicetype:[
          { required: true, message: '请选择服务类型', trigger: 'change'},
        ],
        enterprisename:[
          { required: true, message: '请选择经销商', trigger: 'change'}
        ],
        sonum:[
          { required: true, message: '请选择订单', trigger: 'change'},
        ],
        scenecontactphonenumber: [
          { required: true, message: '请填写手机号码', trigger: 'blur'},
          { pattern:/^1[3-9][0-9]\d{8}$/, message: '请输入正确手机号码',trigger: 'blur' }
        ],
        province:[
          { required: true, message: '请选择省市县', trigger: 'change'},
        ],
        address:[
          { required: true, message: '请填写地址', trigger: 'blur'},
        ],
        scenecontact:[
          { required: true, message: '服务联系人', trigger: 'blur'},
        ],
        scenecontactrole:[
          { required: true, message: '联系人角色', trigger: 'blur'},
        ],
        class1:[
          { required: true, message: '应用系统', trigger: 'change'},
        ],
        class2:[
          { required: true, message: '客诉大类', trigger: 'change'},
        ],
        reason:[
          { required: true, message: '申请原因', trigger: 'blur'},
        ],
        begdate:[
          { required: true, message: '需求服务开始日期', trigger: 'change'},
        ],
        enddate:[
          { required: true, message: '需求服务结束日期', trigger: 'change'},
        ],
      },
      agentParam: {
        "id":20221216145803,
        "content": {

        }
      }
    }
  },
  methods:{
    editBtn () {
      this.dialogFormVisible = true
      this.form = Object.assign({},this.form,this.data)
      this.form.province = [this.data.province, this.data.city, this.data.county]
    },
    onSubmit(){
      console.log(this.form)
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        if (this.form.province) {
          this.form.city = this.form.province[1]
          this.form.county = this.form.province[2]
          this.form.province = this.form.province[0]
        }
        const res = await this.$api.requested({
          "id": "20230206091403",
          "version":1,
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit('onSuccess')
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false
        })
      })
    },
    async getServeClass () {
      let res = await this.$api.requested({
        "id":20230206112003,
        "content": {
        }
      })
      this.serveClassList = res.data
      console.log(this.serveClassList);
    },
    orderChange (data) {
      console.log(data);
      
      this.form.sa_orderid = data.sa_orderid
      this.form.sonum = data.sonum
      this.$refs.order.visible = false
      console.log(this.form.sonum);
      
    },
    agentChange (data) {
      console.log(data);
      this.form.sys_enterpriseid = data.sys_enterpriseid
      this.form.enterprisename = data.enterprisename
      this.$refs.agent.visible = false

      this.form.sa_orderid = ''
      this.form.sonum = ''
    },
    classChange () {
      if (this.form.servicetype == '售中' || this.form.servicetype == '售后') {
        this.$refs.agent.param.content.sys_enterpriseid = this.form.sys_enterpriseid
      }
    }
  },
  async created () {
    this.getServeClass()

    const res = await this.$store.dispatch('optiontypeselect','customerclass')
    this.customerclass = res.data
    const res1 = await this.$store.dispatch('optiontypeselect','systemapp')
    this.systemapp = res1.data
    console.log(this.customerclass,this.systemapp);
    
  }
}
</script>

<style scoped>
.dialog-footer {
  margin-top: 0;
}
.el-select,.el-input,.el-cascader {
  width: 100% !important;
}
</style>