<template>
  <div>
    <i-page-header class="product_tabs" title="服务内容">
    </i-page-header>
    <Card :bordered="false" dis-hover class="ivu-mt">
	  <div style="height: 30px;"></div>
      <Form ref="formValidate" :model="formValidate" :label-width="50">
        <Row type="flex"  :gutter="24">
          <Col v-bind="grid">
            <FormItem label="搜索："  label-for="status2">
              <Input search enter-button placeholder="请输入名称" v-model="formValidate.title" @on-search="userSearchs"/>
            </FormItem>
          </Col>
        </Row>
        <Row type="flex">
          <Col v-bind="grid">
            <Button type="primary" @click="tapAdd"  icon="md-add">添加服务</Button>
          </Col>
        </Row>
      </Form>
      <Table :columns="columns1" :data="tableData" class="mt25" no-userFrom-text="暂无数据" no-filtered-userFrom-text="暂无筛选结果"  :loading="loading" highlight-row>

        <template slot-scope="{ row, index }" slot="status">
          <el-tag v-if="row.status == 1">开启</el-tag>
          <el-tag type="info" v-else>关闭</el-tag>
        </template>
       
        <template slot-scope="{row,index}" slot="img">
          <img v-lazy="row.img" :src="row.img" v-image-preview style="width: 60px;cursor: pointer;"/>
        </template>
		<template slot-scope="{ row, index }" slot="action">
		  <a @click="tapEdit(row)">编辑</a>
		</template>
      </Table>

      <div class="acea-row row-right page">
        <Page :total="page.count" show-elevator show-total @on-change="tapPage"
              :page-size="page.pageSize"/>
      </div>
    </Card>


    <Modal v-model="dialogVisible" :title="title" @on-cancel="editClose">

      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="服务名称" prop="title" :rules="[{ required: true, message: '请输入服务名称'}]">
          <el-input placeholder="请输入服务名称" autocomplete="off" v-model="form.title"></el-input>
        </el-form-item>
		<el-form-item label="服务描述" prop="content" :rules="[{ required: true, message: '请输入服务描述'}]">
		  <el-input placeholder="请输入服务描述" autocomplete="off" v-model="form.content" type="textarea" :rows="5"></el-input>
		</el-form-item>
        <el-form-item label="服务图标" prop="img">
          <ui-upload :upUrl="upUrl" :img ="form.img" fileName="服务图标" :headers="upHeaders" :updata="{isz:0}" @onUpload="onUpload" ></ui-upload>
        </el-form-item>
        <el-form-item label="排序">
          <el-input  placeholder="排序越高越前面" v-model="form.seq"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" style="margin-bottom: 0;">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">显示</el-radio>
            <el-radio :label="0">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="subAdmin">确 定</el-button>
        </span>
    </Modal>

  </div>
</template>

<script>
    import IPageHeader from "@/layouts/system/page-header/index";
    import { mapState, mapActions } from 'vuex';
    import UiUpload from "@/ui/upload/index";
    import Setting from "@/setting";
    import util from "@/libs/util";
    import {SystemServiceType,SystemServiceTypeAdd,SystemServiceTypeDel} from "../../../api/system/showTemplate";
    export default {
        name: "system_showTemplate_serviceType",
        components: { UiUpload, IPageHeader},
        computed: {
        },
        data() {
            return {
                title:"服务类型",
                dialogVisible:false,
                upHeaders:{},
                upUrl:"",
                grid: {
                    xl: 7,
                    lg: 7,
                    md: 12,
                    sm: 24,
                    xs: 24
                },

                columns1: [
					{
					    title: 'ID',
					    key: 'id',
					    maxWidth: 80
					},
                    {
                        title: '标题',
                        key: 'title',
                        maxWidth: 150
                    },
					{
					    title: '服务描述',
					    key: 'content',
					},
                    {
                        title: '图标',
                        key: 'img',
                        slot:"img",
                        maxWidth: 150
                    },
                   
                    {
                        title: '排序',
                        key: 'seq',
                        maxWidth: 100,
                    },
					
                    {
                        title: '状态',
                        key: 'status',
                        slot:"status",
                        maxWidth: 100,
                    },
                    {
                        title: '操作',
                        key: 'action',
                        slot: 'action',
                        fixed: 'right',
                        maxWidth: 120
                    }
                ],
                loading: true,
                tableData: [],
                page : {
                    count : 0,//一共多少数量
                    pageSize : 0,//每一页码数量
                    page : 1,//当前页码
                },
                formValidate:{title:""},
                form:{
                    title     : "",
                    img       : "",
                    seq      : 0,
                    status   : 1,
                },
            }
        },
        mounted(){
            this.initView();
        },
        methods: {
            initView : function(){
                var upHeaders = {};
                this.upUrl = Setting.apiBaseURL + "/systemv1/upload/index";
                const token = this.$utils.util.cookies.get('system_token');
                if (token) {
                    upHeaders['SYSTEM-ACC-TOKEN'] = token;
                }
                this.upHeaders = upHeaders;
                this.getData();
            },
            /**
             * 获取数据
             */
            getData:function () {
                var data = {
                    title : this.formValidate.title,
                    page : this.page.page,
                };
                SystemServiceType(data)
                    .then(res=>{
                        this.loading = false;
                        if(res.code == 200) {
                            this.tableData = res.data.list;
                            this.page.pageSize = res.data.pageSize;
                            this.page.count = res.data.pageCount;
                        } else {
                            this.$alert(res.msg);
                        }
                    })
                    .catch(err=>{
                        this.loading = false;
                        this.$utils.loadingClose();
                        this.$alert("网络繁忙，加载失败，请稍等片刻在尝试!", '系统提示');
                    });
            },
            userSearchs () {
                this.page.page = 1;
                this.loading = true;
                this.getData();
            },

            //关闭
            editClose:function(){
                this.dialogVisible = false;
            },
            /**
             * 添加服务
             */
            tapAdd:function(sassid){
                this.dialogVisible = true;
                this.$refs['form'].resetFields();
                this.form = this.$utils.resetFields(this.form, {status:0,seq:0});
            },

            /**
             * 分页
             */
            tapPage:function(index){
                this.page.page = index;
                this.getData();
            },
            /**
             * 修改数据
             */
            subAdmin:function(e){
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        let loading = this.$loading("提交数据中...");
                        var data = {};
                        for(var i in this.form) {
                            data[i] = this.form[i];
                        }
                        data['data'] = JSON.stringify(this.dataAr);
                        SystemServiceTypeAdd(data)
                            .then(res => {
                                loading.close();
                                if (res.code == 200) {
                                    this.dialogVisible = false;
                                    this.$Message.success("操作成功");
                                    this.getData();
                                } else {
                                    this.$alert(res.msg, '系统提示');
                                }
                            })
                            .catch(err => {
                                loading.close();
                                this.$alert("网络繁忙，加载失败，请稍等片刻在尝试!", '系统提示');
                            });
                    }
                });
            },
            /**
             * 上传文件
             * @param res
             */
            onUpload:function (res) {
                if(res.code == -1) {
                    Notice.error({ title : "系统提示", content:res.msg});
                } else {
                    this.form.img = res.data.img;
                }
            },
			tapEdit:function(row){
				this.form = JSON.parse(JSON.stringify(row));
				this.dialogVisible = true;
				this.title = "编辑服务";
			},
            // 删除
            tapDel (row) {
                this.$confirm('此操作将永久删除该服务, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$utils.loading(this, "删除数据中...");
                    SystemServiceTypeDel({id: row.id})
                        .then(res => {
                            this.$utils.loadingClose();
                            this.loading = true;
                            if (res.code == 200) {
                                this.getData();
                            } else {
                                this.$alert(res.msg);
                            }
                        })
                        .catch(err => {
                            this.$utils.loadingClose();
                            this.$alert("网络繁忙，加载失败，请稍等片刻在尝试!", '系统提示');
                        });
                }).catch((e)=>{});
            },
        }
    }
</script>
<style scoped>
  .platform-img{width: 20px;height: 20px;background: #f2f2f2; margin-right: 4px;}
  .platform-img img{ width: 100%;}
</style>
