<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
	  draggable
      @close="close"
      @keyup.enter.native="doSubmit"
      v-model="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
               label-width="125px" @submit.native.prevent>
        <el-row  :gutter="15">
          <el-col :span="12">
            <el-form-item label="公司名称" prop="companyName"
                          :rules="[
                          {required: true, message:'公司名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.companyName" placeholder="请填写公司名称"></el-input>
            </el-form-item>
          </el-col>




			<el-col :span="12">
				<el-form-item label="公司类型" prop="companyTypeIdList" :rules="[
					{required: true, message:'公司类型不能为空', trigger:'blur'}
				]">
					<el-select
						v-model="inputForm.companyTypeIdList"
						style="width: 100%"
						multiple
						clearable
						placeholder="请选择"
					>
						<el-option
							v-for="item in companyTypeList"
							:key="item.id"
							:label="item.name"
							:value="item.id"
						>
						</el-option>
					</el-select>
				</el-form-item>


			</el-col>




          <el-col :span="12">
            <el-form-item label="联系人姓名" prop="linkmanName"
                          :rules="[
                          {required: true, message:'联系人姓名不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.linkmanName" placeholder="请填写联系人姓名"></el-input>
            </el-form-item>
          </el-col>

			<el-col :span="12">
				<el-form-item label="联系人电话" prop="linkmanPhone"
							  :rules="[
                          {required: true, message:'联系人电话不能为空', trigger:'blur'}
                 ]">
					<el-input v-model="inputForm.linkmanPhone" placeholder="请填写联系人电话"></el-input>
				</el-form-item>
			</el-col>

			<el-col :span="12">
				<el-form-item label="是否为电商小铺" prop="commerceShop"
					:rules="[
							{required: true,message: '必须选择',trigger: 'blur',},
						]"
				>
					<el-radio-group v-model="inputForm.commerceShop">
						<el-radio
							v-for="item in this.$dictUtils.getDictList(
									'yes_no'
								)"
							:label="item.value"
							:key="item.id"
						>{{ item.label }}</el-radio
						>
					</el-radio-group>
				</el-form-item>
			</el-col>

			<el-col :span="12">
				<el-form-item label="入驻产品" prop="merchantProductIdList" :rules="[
					{required: true, message:'入驻产品不能为空', trigger:'blur'}
				]">
					<el-select
						v-model="inputForm.merchantProductIdList"
						style="width: 100%"
						multiple
						clearable
						placeholder="请选择"
					>
						<el-option
							v-for="item in merchantProductList"
							:key="item.id"
							:label="item.name"
							:value="item.id"
						>
						</el-option>
					</el-select>
				</el-form-item>


			</el-col>


			<!--<el-col :span="12">
				<el-form-item label="从业人员(人)" prop="practitionerCount"
							  :rules="[
                          {required: true, message:'从业人员不能为空', trigger:'blur'}
                 ]">
					<el-input v-model="inputForm.practitionerCount" placeholder="请填写从业人员"></el-input>
				</el-form-item>
			</el-col>-->

			<el-col :span="12">
				<el-form-item label="年销售额(万元)" prop="annualSales"
							  :rules="[
                          {required: true, message:'年销售额不能为空', trigger:'blur'}
                 ]">
					<el-input v-model="inputForm.annualSales" placeholder="请填写年销售额"></el-input>
				</el-form-item>
			</el-col>

			<!--<el-col :span="12">
				<el-form-item label="网络零售额(元)" prop="onlineRetailSales"
							  :rules="[
                          {required: true, message:'网络零售额不能为空', trigger:'blur'}
                 ]">
					<el-input v-model="inputForm.onlineRetailSales" placeholder="请填写网络零售额"></el-input>
				</el-form-item>
			</el-col>-->
			<el-col :span="12">
				<el-form-item label="入驻平台" prop="settledPlatformIdList">
					<el-select
						v-model="inputForm.settledPlatformIdList"
						style="width: 100%"
						multiple
						placeholder="请选择"
					>
						<el-option
							v-for="item in settledPlatformList"
							:key="item.id"
							:label="item.name"
							:value="item.id"
						>
						</el-option>
					</el-select>
				</el-form-item>
			</el-col>


          <el-col :span="24">
            <el-form-item label="备注" prop="remarks"
                          :rules="[
                 ]">
              <el-input type="textarea" maxlength="255" v-model="inputForm.remarks" placeholder="请填写备注"  show-word-limit   ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
		<template #footer>
			<span class="dialog-footer">
			  <el-button @click="close()" icon="el-icon-circle-close">关闭</el-button>
			  <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
			</span>
		</template>
    </el-dialog>
  </div>
</template>

<script>
  import merchantCompanyService from '@/api/wdt/merchantCompany/MerchantCompanyService'
  import settledPlatformService from '@/api/wdt/settledPlatform/SettledPlatformService'
  import merchantProductService from '@/api/wdt/merchantProduct/MerchantProductService'
  import merchantCompanyTypeService from '@/api/wdt/merchantCompanyType/MerchantCompanyTypeService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
		  settledPlatformList: [],
		  merchantProductList: [],
		  companyTypeList: [],
        inputForm: {
			companyName: '',
			companyType: '',
			linkmanName: '',
			linkmanPhone: '',
			commerceShop: '',
			merchantProductIdList: [],
			practitionerCount: '',
			annualSales: '',
			onlineRetailSales: '',
         	remarks: '',
			settledPlatformIdList: [],
			companyTypeIdList: [],

        }
      }
    },
    created () {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm = {
          name: '',
          sort: '',
          remarks: '',
          parentId: ''
        }
        if (method === 'add') {
          this.title = `新增公司信息`
        } else if (method === 'edit') {
          this.inputForm.id = id
          this.title = '修改公司信息'
        } else if (method === 'view') {
          this.inputForm.id = id
          this.title = '查看公司信息'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
			if (this.$refs.inputForm !== undefined) {
				this.$refs.inputForm.resetFields();
			}

			Promise.all([
				settledPlatformService.list({ current: 1, size: -1 }),
				merchantProductService.list({ current: 1, size: -1 }),
				merchantCompanyTypeService.list({ current: 1, size: -1 }),
			]).then((result) => {
				this.settledPlatformList = result[0].records;
				this.merchantProductList = result[1].records;
				this.companyTypeList = result[2].records;
				console.log(result)
			});

          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            merchantCompanyService.queryById(this.inputForm.id).then((data) => {
            	console.log(data)
              this.inputForm = this.recover(this.inputForm, data)
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            merchantCompanyService.save(this.inputForm).then((data) => {
              this.close()
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      close () {
        this.$refs.inputForm.resetFields()
        this.visible = false
      }
    }
  }
</script>
